Module: Collins::Util
- Includes:
- Logging
- Included in:
- Address, Api, Api::Logging, Api::Tag, Asset, AssetState, AssetType, Client, Ipmi, Power, PowerUnit, Profile, SimpleCallback
- Defined in:
- lib/collins/util.rb,
lib/collins/logging.rb
Overview
General purpose util methods
Methods in Util can be accessed by.…
* `Collins::Util.method_name`
* `ClassIncludingCollinsUtil.method_name`
* `instance_of_class_including_collins_util.method_name`
Other than #get_asset_or_tag most of these methods are not collins specific
Defined Under Namespace
Modules: Logging
Constant Summary
Constants included from Logging
Class Method Summary collapse
Instance Method Summary collapse
-
#deep_copy_hash(hash) ⇒ Hash
Create a deep copy of a hash.
-
#get_asset_or_tag(asset_or_tag) ⇒ Collins::Asset
Resolve an asset from a string tag or collins asset.
-
#require_non_empty(value, message, return_value = false) ⇒ NilClass, Object
Require that a value not be empty.
-
#require_that(guard, message, return_guard = false) ⇒ Object
Require that a guard condition passes.
-
#stringify_hash(hash, options = {}) ⇒ Object
Given a hash, convert all keys to strings.
-
#symbolize_hash(hash, options = {}) ⇒ Object
Given a hash, rewrite keys to symbols.
Methods included from Logging
Class Method Details
Instance Method Details
#deep_copy_hash(hash) ⇒ Hash
All keys and values must be serializable, Proc for instance will fail
Create a deep copy of a hash
This is useful for copying a hash that will be mutated
28 29 30 31 |
# File 'lib/collins/util.rb', line 28 def deep_copy_hash hash require_that(hash.is_a?(Hash), "deep_copy_hash requires a hash be specified, got #{hash.class}") Marshal.load Marshal.dump(hash) end |
#get_asset_or_tag(asset_or_tag) ⇒ Collins::Asset
This is perhaps the only collins specific method in Util
Resolve an asset from a string tag or collins asset
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/collins/util.rb', line 80 def get_asset_or_tag asset_or_tag asset = case asset_or_tag when Collins::Asset then asset_or_tag when String then Collins::Asset.new(asset_or_tag) when Symbol then Collins::Asset.new(asset_or_tag.to_s) else = "Expected Collins::Asset, String or Symbol. Got #{asset_or_tag.class}" raise ExpectationFailedError.new() end if asset.nil? || asset.tag.nil? then raise ExpectationFailedError.new("Empty asset tag, but a tag is required") end asset end |
#require_non_empty(value, message, return_value = false) ⇒ NilClass, Object
Require that a value not be empty
If the value is a string, ensure that once stripped it’s not empty. If the value responds to ‘:empty?`, ensure that it’s not. Otherwise ensure the value isn’t nil.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/collins/util.rb', line 43 def require_non_empty value, , return_value = false guard_value = if return_value == true then value elsif return_value != false then return_value else false end if value.is_a?(String) then require_that(!value.strip.empty?, , guard_value) elsif value.respond_to?(:empty?) then require_that(!value.empty?, , guard_value) else require_that(!value.nil?, , guard_value) end end |
#require_that(guard, message, return_guard = false) ⇒ Object
Require that a guard condition passes
Simply checks that the guard is truthy, and throws an error otherwise
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/collins/util.rb', line 64 def require_that guard, , return_guard = false if not guard then raise ExpectationFailedError.new() end if return_guard == true then guard elsif return_guard != false then return_guard end end |
#stringify_hash(hash, options = {}) ⇒ Object
Given a hash, convert all keys to strings
121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/collins/util.rb', line 121 def stringify_hash hash, = {} (raise ExpectationFailedError.new("stringify_hash called without a hash")) unless hash.is_a?(Hash) hash.inject({}) do |result, (k,v)| key = [:downcase] ? k.to_s.downcase : k.to_s if v.is_a?(Hash) then result[key] = stringify_hash(v) elsif v.is_a?(Regexp) && [:rewrite_regex] then result[key] = v.inspect[1..-2] else result[key] = v end result end end |
#symbolize_hash(hash, options = {}) ⇒ Object
Given a hash, rewrite keys to symbols
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/collins/util.rb', line 103 def symbolize_hash hash, = {} return {} if (hash.nil? or hash.empty?) (raise ExpectationFailedError.new("symbolize_hash called without a hash")) unless hash.is_a?(Hash) hash.inject({}) do |result, (k,v)| key = [:downcase] ? k.to_s.downcase.to_sym : k.to_s.to_sym if v.is_a?(Hash) then result[key] = symbolize_hash(v) elsif v.is_a?(Regexp) && [:rewrite_regex] then result[key] = v.inspect[1..-2] else result[key] = v end result end end |