Skip to content

Commit

Permalink
Merge pull request #6 from rd2/dev
Browse files Browse the repository at this point in the history
Fixes/docs one-liner method outputs
  • Loading branch information
brgix authored Jul 31, 2022
2 parents 50228ea + 157b1e9 commit 802b600
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 31 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ return M.hashkey("faces", faces, :area, "sum") unless faces.key?(:area)
If the _Hash_ `faces` does not hold `:area` as one of its keys, then __hashkey__ would generate the following DEBUG log message (before returning _nil_):

```
"'faces' Hash: no key 'area' (sum)"
"Missing 'area' key in 'faces' Hash (sum)"
```

Similar to __mismatch__, the method __hashkey__ requires 4x arguments (a _Hash_ ID, a valid Ruby _Hash_, the missing _key_, and the calling method ID). There are also 2x optional _terminal_ arguments, e.g. `M::ERROR, false)`.
Expand Down Expand Up @@ -198,8 +198,8 @@ M.zero("area", "sum", M::FATAL, false) if area.abs < TOL
... generating the following FATAL log message (before returning _false_):

```
"'area' ~zero (sum)"
"'area' ~zero (sum)"
"Zero 'area' (sum)"
"Zero 'area' (sum)"
```

And again, the first 2x arguments are required; the last 2x are optional.
Expand All @@ -214,7 +214,7 @@ M.negative("area", "sum", M::FATAL, false) if area < 0
... generating this FATAL log message (before returning _false_):

```
"'area' negative (sum)"
"Negative 'area' (sum)"
```

You guessed it: the first 2x arguments are required; the last 2x as optionals.
Expand Down
42 changes: 21 additions & 21 deletions lib/oslg/oslog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ def log(level = DEBUG, message = "")
##
# Log template 'invalid object' message and return user-set object.
#
# @param id [String] empty object identifier
# @param id [String] invalid object identifier
# @param mth [String] calling method identifier
# @param ord [String] calling method argument order number of obj (optional)
# @param lvl [Integer] DEBUG, INFO, WARN, ERROR or FATAL (optional)
# @param res [Object] what to return (optional)
#
# @return [Object] res if specified by user
# @return [Nil] nil if return object is invalid
# @return [Object] return object if specified by user
# @return [Nil] nil if return object missing or invalid
def invalid(id = "", mth = "", ord = 0, lvl = DEBUG, res = nil)
return nil unless defined?(res)
return res unless defined?(id ) && id
Expand All @@ -200,15 +200,15 @@ def invalid(id = "", mth = "", ord = 0, lvl = DEBUG, res = nil)
##
# Log template 'instance/class mismatch' message and return user-set object.
#
# @param id [String] empty object identifier
# @param id [String] mismatched object identifier
# @param obj [Object] object to validate
# @param cl [Class] target class
# @param mth [String] calling method identifier
# @param lvl [Integer] DEBUG, INFO, WARN, ERROR or FATAL (optional)
# @param res [Object] what to return (optional)
#
# @return [Object] res if specified by user
# @return [Nil] nil if return object is invalid
# @return [Object] return object if specified by user
# @return [Nil] nil if return object missing or invalid
def mismatch(id = "", obj = nil, cl = nil, mth = "", lvl = DEBUG, res = nil)
return nil unless defined?(res)
return res unless defined?(id ) && id
Expand All @@ -233,15 +233,15 @@ def mismatch(id = "", obj = nil, cl = nil, mth = "", lvl = DEBUG, res = nil)
##
# Log template 'missing hash key' message and return user-set object.
#
# @param id [String] empty object identifier
# @param id [String] Hash identifier
# @param hsh [Hash] hash to validate
# @param key [Object] target key
# @param key [Object] missing key
# @param mth [String] calling method identifier
# @param lvl [Integer] DEBUG, INFO, WARN, ERROR or FATAL (optional)
# @param res [Object] what to return (optional)
#
# @return [Object] res if specified by user
# @return [Nil] nil if not specified by user (or invalid)
# @return [Object] return object if specified by user
# @return [Nil] nil if return object missing or invalid
def hashkey(id = "", hsh = {}, key = "", mth = "", lvl = DEBUG, res = nil)
return nil unless defined?(res)
return res unless defined?(id ) && id
Expand All @@ -257,7 +257,7 @@ def hashkey(id = "", hsh = {}, key = "", mth = "", lvl = DEBUG, res = nil)
return res if id.empty?
return mismatch(id, hsh, Hash, mth, lvl, res) unless hsh.is_a?(Hash)
return res if hsh.key?(key)
msg = "'#{id}' Hash: no key '#{key}' (#{mth})"
msg = "Missing '#{key}' key in '#{id}' Hash (#{mth})"
lvl = lvl.to_i unless lvl.is_a?(Integer)
log(lvl, msg) if lvl >= DEBUG && lvl <= FATAL
res
Expand All @@ -271,8 +271,8 @@ def hashkey(id = "", hsh = {}, key = "", mth = "", lvl = DEBUG, res = nil)
# @param lvl [Integer] DEBUG, INFO, WARN, ERROR or FATAL (optional)
# @param res [Object] what to return (optional)
#
# @return [Object] res if specified by user
# @return [Nil] nil if return object is invalid
# @return [Object] return object if specified by user
# @return [Nil] nil if return object missing or invalid
def empty(id = "", mth = "", lvl = DEBUG, res = nil)
return nil unless defined?(res)
return res unless defined?(id ) && id
Expand All @@ -293,13 +293,13 @@ def empty(id = "", mth = "", lvl = DEBUG, res = nil)
##
# Log template 'near zero' message and return user-set object.
#
# @param id [String] empty object identifier
# @param id [String] zero object identifier
# @param mth [String] calling method identifier
# @param lvl [Integer] DEBUG, INFO, WARN, ERROR or FATAL (optional)
# @param res [Object] what to return (optional)
#
# @return [Object] res if specified by user
# @return [Nil] nil if return object is invalid
# @return [Object] return object if specified by user
# @return [Nil] nil if return object missing or invalid
def zero(id = "", mth = "", lvl = DEBUG, res = nil)
return nil unless defined?(res)
return res unless defined?(id ) && id
Expand All @@ -311,7 +311,7 @@ def zero(id = "", mth = "", lvl = DEBUG, res = nil)
id = id.to_s.strip
id = id[0...60] + " ..." if id.length > 60
return res if id.empty?
msg = "'#{id}' ~zero (#{mth})"
msg = "Zero '#{id}' (#{mth})"
lvl = lvl.to_i unless lvl.is_a?(Integer)
log(lvl, msg) if lvl >= DEBUG && lvl <= FATAL
res
Expand All @@ -320,13 +320,13 @@ def zero(id = "", mth = "", lvl = DEBUG, res = nil)
##
# Log template 'negative' message and return user-set object.
#
# @param id [String] empty object identifier
# @param id [String] negative object identifier
# @param mth [String] calling method identifier
# @param lvl [Integer] DEBUG, INFO, WARN, ERROR or FATAL (optional)
# @param res [Object] what to return (optional)
#
# @return [Object] res if specified by user
# @return [Nil] nil if return object is invalid
# @return [Object] return object if specified by user
# @return [Nil] nil if return object missing or invalid
def negative(id = "", mth = "", lvl = DEBUG, res = nil)
return nil unless defined?(res)
return res unless defined?(id ) && id
Expand All @@ -338,7 +338,7 @@ def negative(id = "", mth = "", lvl = DEBUG, res = nil)
id = id.to_s.strip
id = id[0...60] + " ..." if id.length > 60
return res if id.empty?
msg = "'#{id}' negative (#{mth})"
msg = "Negative '#{id}' (#{mth})"
lvl = lvl.to_i unless lvl.is_a?(Integer)
log(lvl, msg) if lvl >= DEBUG && lvl <= FATAL
res
Expand Down
2 changes: 1 addition & 1 deletion lib/oslg/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

module OSlg
VERSION = "0.2.3".freeze
VERSION = "0.2.4".freeze
end
11 changes: 6 additions & 5 deletions spec/oslg_tests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@
expect(cls2.hashkey("x", {bar: 0}, "k", "foo")).to be(nil)
expect(cls2.logs.size).to eq(1)
expect(cls2.logs.first.key?(:message))
expect(cls2.logs.first[:message]).to eq("'x' Hash: no key 'k' (foo)")
str = "Missing 'k' key in 'x' Hash (foo)"
expect(cls2.logs.first[:message]).to eq(str)

expect(cls2.clean!).to eq(cls2::DEBUG)
expect(cls2.hashkey("x", {foo: 0}, :foo, "bar")).to be(nil)
Expand Down Expand Up @@ -120,13 +121,13 @@
expect(cls2.zero("x", "foo")).to be(nil)
expect(cls2.logs.size).to eq(1)
expect(cls2.logs.first.key?(:message))
expect(cls2.logs.first[:message]).to eq("'x' ~zero (foo)")
expect(cls2.logs.first[:message]).to eq("Zero 'x' (foo)")

expect(cls2.clean!).to eq(cls2::DEBUG)
expect(cls2.zero({foo: 0}, :foo)).to be(nil)
expect(cls2.logs.size).to eq(1)
expect(cls2.logs.first.key?(:message))
expect(cls2.logs.first[:message]).to eq("'{:foo=>0}' ~zero (foo)")
expect(cls2.logs.first[:message]).to eq("Zero '{:foo=>0}' (foo)")

expect(cls2.clean!).to eq(cls2::DEBUG)
expect(cls2.zero(nil, 0)).to be(nil)
Expand All @@ -136,13 +137,13 @@
expect(cls2.negative("x", "foo")).to be(nil)
expect(cls2.logs.size).to eq(1)
expect(cls2.logs.first.key?(:message))
expect(cls2.logs.first[:message]).to eq("'x' negative (foo)")
expect(cls2.logs.first[:message]).to eq("Negative 'x' (foo)")

expect(cls2.clean!).to eq(cls2::DEBUG)
expect(cls2.negative({foo: 0}, :foo)).to be(nil)
expect(cls2.logs.size).to eq(1)
expect(cls2.logs.first.key?(:message))
expect(cls2.logs.first[:message]).to eq("'{:foo=>0}' negative (foo)")
expect(cls2.logs.first[:message]).to eq("Negative '{:foo=>0}' (foo)")

expect(cls2.clean!).to eq(cls2::DEBUG)
expect(cls2.negative(nil, 0)).to be(nil)
Expand Down

0 comments on commit 802b600

Please sign in to comment.