Module: Wukong::HashLike
- Defined in:
- lib/wukong/extensions/hash_like.rb
Overview
A hashlike has to
*
-
The arguments to your initializer should be the same as the keys, in order If not, you must override #from_hash
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #coerce_attr(attr, coerce_blank_to_nil = false, &block) ⇒ Object
- #coerce_to_date!(attr, *args) ⇒ Object
- #coerce_to_int!(attr, *args) ⇒ Object
-
#compact_blank ⇒ Object
remove all key-value pairs where the value is blank.
-
#deep_merge(hsh2) ⇒ Object
Merge hashes recursively.
-
#each_pair(*args, &block) ⇒ Object
Analagous to Hash#each_pair.
-
#keys ⇒ Object
List of possible keys – delegates to the class.
-
#merge(*args) ⇒ Object
Analagous to Hash#merge.
- #merge!(hsh, &block) ⇒ Object (also: #update)
-
#pairs ⇒ Object
Analagous to Hash#each_pair.
-
#slice(*keys) ⇒ Object
Return a Hash containing only values for the given keys.
-
#to_hash ⇒ Object
Convert to a hash.
-
#values_of(*keys) ⇒ Object
values_at like a hash.
Class Method Details
.included(base) ⇒ Object
126 127 128 129 130 |
# File 'lib/wukong/extensions/hash_like.rb', line 126 def self.included base base.class_eval do extend ClassMethods end end |
Instance Method Details
#coerce_attr(attr, coerce_blank_to_nil = false, &block) ⇒ Object
132 133 134 135 136 |
# File 'lib/wukong/extensions/hash_like.rb', line 132 def coerce_attr attr, coerce_blank_to_nil=false, &block orig_val = self.send(attr) new_val = (coerce_blank_to_nil && orig_val.blank?) ? nil : block.call(orig_val) self.send("#{attr}=", new_val) end |
#coerce_to_date!(attr, *args) ⇒ Object
144 145 146 |
# File 'lib/wukong/extensions/hash_like.rb', line 144 def coerce_to_date! attr, *args coerce_attr(attr, *args){|val| val.is_a?(DateTime) ? val : DateTime.parse(val) rescue nil } end |
#coerce_to_int!(attr, *args) ⇒ Object
138 139 140 141 142 |
# File 'lib/wukong/extensions/hash_like.rb', line 138 def coerce_to_int! attr, *args coerce_attr(attr, *args) do |val| val.to_i end end |
#compact_blank ⇒ Object
remove all key-value pairs where the value is blank
94 95 96 |
# File 'lib/wukong/extensions/hash_like.rb', line 94 def compact_blank to_hash.compact_blank! end |
#deep_merge(hsh2) ⇒ Object
Merge hashes recursively. Nothing special happens to array values
x = { :subhash => { 1 => :val_from_x, 222 => :only_in_x, 333 => :only_in_x }, :scalar => :scalar_from_x}
y = { :subhash => { 1 => :val_from_y, 999 => :only_in_y }, :scalar => :scalar_from_y }
x.deep_merge y
=> {:subhash=>{1=>:val_from_y, 222=>:only_in_x, 333=>:only_in_x, 999=>:only_in_y}, :scalar=>:scalar_from_y}
y.deep_merge x
=> {:subhash=>{1=>:val_from_x, 222=>:only_in_x, 333=>:only_in_x, 999=>:only_in_y}, :scalar=>:scalar_from_x}
87 88 89 |
# File 'lib/wukong/extensions/hash_like.rb', line 87 def deep_merge hsh2 merge hsh2, &Hash::DEEP_MERGER end |
#each_pair(*args, &block) ⇒ Object
Analagous to Hash#each_pair
59 60 61 |
# File 'lib/wukong/extensions/hash_like.rb', line 59 def each_pair *args, &block pairs.each(*args, &block) end |
#keys ⇒ Object
List of possible keys – delegates to the class
14 15 16 |
# File 'lib/wukong/extensions/hash_like.rb', line 14 def keys self.class.keys end |
#merge(*args) ⇒ Object
Analagous to Hash#merge
66 67 68 |
# File 'lib/wukong/extensions/hash_like.rb', line 66 def merge *args self.dup.merge!(*args) end |
#merge!(hsh, &block) ⇒ Object Also known as: update
69 70 71 72 73 |
# File 'lib/wukong/extensions/hash_like.rb', line 69 def merge! hsh, &block raise "can't handle block arg yet" if block hsh.each_pair{|key, val| self.send("#{key}=", val) if self.respond_to?("#{key}=") } self end |
#pairs ⇒ Object
Analagous to Hash#each_pair
52 53 54 |
# File 'lib/wukong/extensions/hash_like.rb', line 52 def pairs self.class.members.map{|attr| [attr, self[attr]] } end |
#slice(*keys) ⇒ Object
Return a Hash containing only values for the given keys.
Since this is intended to mirror Hash#slice it will harmlessly ignore keys not present in the struct. They will be unset (hsh.include? is not true) as opposed to nil.
25 26 27 28 29 30 |
# File 'lib/wukong/extensions/hash_like.rb', line 25 def slice *keys keys.inject({}) do |hsh, key| hsh[key] = send(key) if respond_to?(key) hsh end end |
#to_hash ⇒ Object
Convert to a hash
45 46 47 |
# File 'lib/wukong/extensions/hash_like.rb', line 45 def to_hash slice(*self.class.members) end |
#values_of(*keys) ⇒ Object
values_at like a hash
Since this is intended to mirror Hash#values_at it will harmlessly ignore keys not present in the struct
38 39 40 |
# File 'lib/wukong/extensions/hash_like.rb', line 38 def values_of *keys keys.map{|key| self.send(key) if respond_to?(key) } end |