Module: Visor::Common::Extensions::Hash
- Defined in:
- lib/common/extensions/hash.rb
Overview
Extending Hash class
Instance Method Summary collapse
-
#assert_exclusion_keys(*exclude_keys) ⇒ Object
Validate non-inclusion of some keys in a hash.
-
#assert_inclusion_keys(*inclusion_keys) ⇒ Object
Validate inclusions of some keys in a hash.
-
#assert_valid_keys(*valid_keys) ⇒ Object
Validate all keys in a hash match *valid keys.
-
#assert_valid_values_for(key, *valid_values) ⇒ Object
Validate value of some key in a hash.
-
#set_blank_keys_value_to(keys_to_set, keys_to_ignore, to_value) ⇒ Object
Set some keys in a hash to a given value, possibly ignoring some keys.
-
#stringify_keys ⇒ Object
Return a new hash with all keys converted to strings.
-
#stringify_keys! ⇒ Object
Destructively convert all keys to strings.
-
#symbolize_keys ⇒ Object
Return a new hash with all keys converted to symbols.
-
#symbolize_keys! ⇒ Object
Destructively convert all keys to symbols.
-
#to_openstruct ⇒ OpenStruct
Convert a Hash to a OpenStruct, so it can be accessed as options like: h => h.key.
Instance Method Details
#assert_exclusion_keys(*exclude_keys) ⇒ Object
Validate non-inclusion of some keys in a hash.
77 78 79 80 |
# File 'lib/common/extensions/hash.rb', line 77 def assert_exclusion_keys(*exclude_keys) exc = exclude_keys.flatten & keys raise ArgumentError, "These fields are read-only: #{exc.join(', ')}" unless exc.empty? end |
#assert_inclusion_keys(*inclusion_keys) ⇒ Object
Validate inclusions of some keys in a hash.
66 67 68 69 |
# File 'lib/common/extensions/hash.rb', line 66 def assert_inclusion_keys(*inclusion_keys) inc = inclusion_keys.flatten - keys raise ArgumentError, "These fields are required: #{inc.join(', ')}" unless inc.empty? end |
#assert_valid_keys(*valid_keys) ⇒ Object
Validate all keys in a hash match *valid keys.
55 56 57 58 |
# File 'lib/common/extensions/hash.rb', line 55 def assert_valid_keys(*valid_keys) unknown_keys = keys - valid_keys.flatten raise ArgumentError, "Unknown fields: #{unknown_keys.join(", ")}" unless unknown_keys.empty? end |
#assert_valid_values_for(key, *valid_values) ⇒ Object
Validate value of some key in a hash.
89 90 91 92 93 |
# File 'lib/common/extensions/hash.rb', line 89 def assert_valid_values_for(key, *valid_values) unless valid_values.flatten.include?(self[key.to_sym]) || self[key].nil? raise ArgumentError, "Invalid #{key.to_s} '#{self[key]}', available options: #{valid_values.join(', ')}" end end |
#set_blank_keys_value_to(keys_to_set, keys_to_ignore, to_value) ⇒ Object
Set some keys in a hash to a given value, possibly ignoring some keys.
103 104 105 |
# File 'lib/common/extensions/hash.rb', line 103 def set_blank_keys_value_to(keys_to_set, keys_to_ignore, to_value) keys_to_set.each { |k| self.merge!(k => to_value) unless self[k] || keys_to_ignore.include?(k) } end |
#stringify_keys ⇒ Object
Return a new hash with all keys converted to strings.
17 18 19 20 21 22 23 24 |
# File 'lib/common/extensions/hash.rb', line 17 def stringify_keys inject({}) do |acc, (k, v)| key = Symbol === k ? k.to_s : k value = Hash === v ? v.stringify_keys : v acc[key] = value acc end end |
#stringify_keys! ⇒ Object
Destructively convert all keys to strings.
28 29 30 |
# File 'lib/common/extensions/hash.rb', line 28 def stringify_keys! self.replace(self.stringify_keys) end |
#symbolize_keys ⇒ Object
Return a new hash with all keys converted to symbols.
34 35 36 37 38 39 40 41 |
# File 'lib/common/extensions/hash.rb', line 34 def symbolize_keys inject({}) do |acc, (k, v)| key = String === k ? k.to_sym : k value = Hash === v ? v.symbolize_keys : v acc[key] = value acc end end |
#symbolize_keys! ⇒ Object
Destructively convert all keys to symbols.
45 46 47 |
# File 'lib/common/extensions/hash.rb', line 45 def symbolize_keys! self.replace(self.symbolize_keys) end |
#to_openstruct ⇒ OpenStruct
Convert a Hash to a OpenStruct, so it can be accessed as options like: h => h.key
111 112 113 114 115 |
# File 'lib/common/extensions/hash.rb', line 111 def to_openstruct mapped = {} each { |key, value| mapped[key] = value.to_openstruct } OpenStruct.new(mapped) end |