1+ # frozen_string_literal: true
2+
13module HashKit
4+ # Hash kit Helper class
25 class Helper
3-
4- #This method is called to make a hash allow indifferent access (it will accept both strings & symbols for a valid key).
6+ # This method is called to make a hash allow indifferent access (it will
7+ # accept both strings & symbols for a valid key).
58 def indifferent! ( hash )
6- unless hash . is_a? ( Hash )
7- return
8- end
9+ return unless hash . is_a? ( Hash )
910
10- #set the default proc to allow the key to be either string or symbol if a matching key is found.
11+ # Set the default proc to allow the key to be either string or symbol if
12+ # a matching key is found.
1113 hash . default_proc = proc do |h , k |
1214 if h . key? ( k . to_s )
1315 h [ k . to_s ]
@@ -18,9 +20,9 @@ def indifferent!(hash)
1820 end
1921 end
2022
21- #recursively process any child hashes
23+ # Recursively process any child hashes
2224 hash . each do |key , value |
23- if hash [ key ] != nil
25+ unless hash [ key ] . nil?
2426 if hash [ key ] . is_a? ( Hash )
2527 indifferent! ( hash [ key ] )
2628 elsif hash [ key ] . is_a? ( Array )
@@ -33,9 +35,7 @@ def indifferent!(hash)
3335 end
3436
3537 def indifferent_array! ( array )
36- unless array . is_a? ( Array )
37- return
38- end
38+ return unless array . is_a? ( Array )
3939
4040 array . each do |i |
4141 if i . is_a? ( Hash )
@@ -46,14 +46,16 @@ def indifferent_array!(array)
4646 end
4747 end
4848
49- #This method is called to convert all the keys of a hash into symbols to allow consistent usage of hashes within your Ruby application.
49+ # This method is called to convert all the keys of a hash into symbols to
50+ # allow consistent usage of hashes within your Ruby application.
5051 def symbolize ( hash )
5152 { } . tap do |h |
5253 hash . each { |key , value | h [ key . to_sym ] = map_value_symbol ( value ) }
5354 end
5455 end
5556
56- #This method is called to convert all the keys of a hash into strings to allow consistent usage of hashes within your Ruby application.
57+ # This method is called to convert all the keys of a hash into strings to
58+ # allow consistent usage of hashes within your Ruby application.
5759 def stringify ( hash )
5860 { } . tap do |h |
5961 hash . each { |key , value | h [ key . to_s ] = map_value_string ( value ) }
@@ -73,18 +75,22 @@ def to_hash(obj)
7375 hash
7476 end
7577
76- def from_hash ( hash , klass , transforms = [ ] )
78+ # Return an object of type klass from the values in the given hash
79+ #
80+ # @param [Hash] hash
81+ # @param [Class] klass
82+ # @param [Array] transforms
83+ # @return [Object]
84+ def from_hash ( hash , klass , transforms = [ ] )
7785 obj = klass . new
78- if hash ==nil || hash == { }
79- return obj
80- end
86+ return obj if hash . nil? || hash . empty?
87+ raise ArgumentError , "#{ hash . inspect } is not a hash" unless hash . is_a? ( Hash )
88+
89+ hash . each do |k , v |
90+ next unless obj . respond_to? ( k )
8191
82- hash . each do |k , v |
83- if !obj . respond_to? ( k )
84- next
85- end
8692 transform = transforms . detect { |t | t . key . to_sym == k . to_sym }
87- if transform != nil
93+ if ! transform . nil?
8894 if v . is_a? ( Hash )
8995 child = from_hash ( v , transform . klass , transforms )
9096 obj . instance_variable_set ( "@#{ k } " , child )
@@ -98,7 +104,8 @@ def from_hash(hash, klass, transforms = [])
98104 obj . instance_variable_set ( "@#{ k } " , v )
99105 end
100106 end
101- return obj
107+
108+ obj
102109 end
103110
104111 private
0 commit comments