Ruby's OpenStruct offers the same functionality.
Provides a subclass of Hash and a wrapper around Rails' serialize attribute for object-like access to hashes without validating existence of nested hashes
Arand new Metadata objects act just like hashes:
h = Metadata.new
h
=> {}
Accessing nested data requires no wrapping conditions checking for existence of the rquested data:
h.outer.inner
=> {}
Possible real-word example:
if (min_numbers = h.password_rules.formats.numbers.minimum).present?
# some code using min_numbers
else
# data doesn't exist in h
h.password_rules.formats.numbers.minimum = 1
# h
# => { password_rules: { formats: { numbers: { minimum: 1 } } } }
end
Convert an existing hash to metadata
{a: 1}.to_metadata.a
# => 1
h = Metadata.new( { outer: { inner: { hash_key: "value" } } } )
h.outer.inner.hash_key
=> "value"
h = Metadata.new
h
=> {}
h.outer.inner = 2
h
=> { outer: { inner: 2 } }
gem "metahash-rb", require: "metahash"
has_metadata
or
has_metadata :field_not_called_metadata
This gem has been tested with Ruby 2.0, and rails 3.2, 4.1
- Fork the project
- Create a new, descriptively named branch
- Add Test(s)!
- Commit your proposed changes
- Submit a pull request