Module: Objectize
- Defined in:
- lib/objectize.rb,
lib/objectize/version.rb
Constant Summary collapse
- VERSION =
'0.1.1'
Class Method Summary collapse
-
.to_basic_type(element) ⇒ Object
Converts an objectized element back to a basic type (i.e. a hash or an array of hashes).
-
.to_object(element) ⇒ Object
Converts a hash or array of hashes into an object.
Class Method Details
.to_basic_type(element) ⇒ Object
Converts an objectized element back to a basic type (i.e. a hash or an array of hashes)
my_hash = Objectize.to_basic_type(my_object)
my_hash['a']['b']['c'] #=> 'foo'
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/objectize.rb', line 27 def to_basic_type(element) case element when Hashie::Mash element.to_hash when Array element.map { |sub_element| to_basic_type(sub_element) } else element end end |
.to_object(element) ⇒ Object
Converts a hash or array of hashes into an object.
my_object = Objectize.to_object(a: { b: { c: 'foo' } })
my_object.a.b.c #=> 'foo'
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/objectize.rb', line 11 def to_object(element) case element when Hash Hashie::Mash.new(element) when Array element.map { |sub_element| to_object(sub_element) } else element end end |