Class: HashKit::Helper
- Inherits:
-
Object
- Object
- HashKit::Helper
- Defined in:
- lib/hash_kit/helper.rb
Overview
Hash kit Helper class
Instance Method Summary collapse
-
#from_hash(hash, klass, transforms = []) ⇒ Object
Return an object of type klass from the values in the given hash.
-
#indifferent!(hash) ⇒ Object
This method is called to make a hash allow indifferent access (it will accept both strings & symbols for a valid key).
- #indifferent_array!(array) ⇒ Object
-
#stringify(hash) ⇒ Object
This method is called to convert all the keys of a hash into strings to allow consistent usage of hashes within your Ruby application.
-
#symbolize(hash) ⇒ Object
This method is called to convert all the keys of a hash into symbols to allow consistent usage of hashes within your Ruby application.
-
#to_hash(obj) ⇒ Hash
Convert an object to a hash representation of its instance variables.
Instance Method Details
#from_hash(hash, klass, transforms = []) ⇒ Object
Return an object of type klass from the values in the given hash
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/hash_kit/helper.rb', line 84 def from_hash(hash, klass, transforms = []) obj = klass.new return obj if hash.nil? || hash.empty? raise ArgumentError, "#{hash.inspect} is not a hash" unless hash.is_a?(Hash) hash.each do |k, v| next unless obj.respond_to?(k) transform = transforms.detect { |t| t.key.to_sym == k.to_sym } if !transform.nil? if v.is_a?(Hash) child = from_hash(v, transform.klass, transforms) obj.instance_variable_set("@#{k}", child) elsif v.is_a?(Array) items = v.map do |i| from_hash(i, transform.klass, transforms) end obj.instance_variable_set("@#{k}", items) end else obj.instance_variable_set("@#{k}", v) end end obj end |
#indifferent!(hash) ⇒ Object
This method is called to make a hash allow indifferent access (it will accept both strings & symbols for a valid key).
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/hash_kit/helper.rb', line 8 def indifferent!(hash) return unless hash.is_a?(Hash) # Set the default proc to allow the key to be either string or symbol if # a matching key is found. hash.default_proc = proc do |h, k| if h.key?(k.to_s) h[k.to_s] elsif h.key?(k.to_sym) h[k.to_sym] else nil end end # Recursively process any child hashes hash.each do |key,value| unless hash[key].nil? if hash[key].is_a?(Hash) indifferent!(hash[key]) elsif hash[key].is_a?(Array) indifferent_array!(hash[key]) end end end hash end |
#indifferent_array!(array) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/hash_kit/helper.rb', line 37 def indifferent_array!(array) return unless array.is_a?(Array) array.each do |i| if i.is_a?(Hash) indifferent!(i) elsif i.is_a?(Array) indifferent_array!(i) end end end |
#stringify(hash) ⇒ Object
This method is called to convert all the keys of a hash into strings to allow consistent usage of hashes within your Ruby application.
59 60 61 62 63 |
# File 'lib/hash_kit/helper.rb', line 59 def stringify(hash) {}.tap do |h| hash.each { |key, value| h[key.to_s] = map_value_string(value) } end end |
#symbolize(hash) ⇒ Object
This method is called to convert all the keys of a hash into symbols to allow consistent usage of hashes within your Ruby application.
51 52 53 54 55 |
# File 'lib/hash_kit/helper.rb', line 51 def symbolize(hash) {}.tap do |h| hash.each { |key, value| h[key.to_sym] = map_value_symbol(value) } end end |
#to_hash(obj) ⇒ Hash
Convert an object to a hash representation of its instance variables.
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/hash_kit/helper.rb', line 67 def to_hash(obj) return nil unless obj return obj if obj.is_a?(Hash) hash = {} obj.instance_variables.each do |key| hash[key[1..-1].to_sym] = deeply_to_hash(obj.instance_variable_get(key)) end hash end |