PropertyString
Use Java-style property notation to execute method call chains on an object.
Installation
Install the gem and add to the application's Gemfile by executing:
bundle add property_string
If bundler is not being used to manage dependencies, install the gem by executing:
gem install property_string
Usage
product.company.name
ps = PropertyString.new(product)
ps["company.name"]
user.posts[0].replied_to.name
ps = PropertyString.new(user)
ps["posts.0.replied_to.name"]
ps = PropertyString.new(product)
ps["company.does_not_exist!"]
h.dig(:some, "nested", :hash)
ps = PropertyString(h)
ps["some.nested.hash"]
a[0][0][0]
ps = PropertyString(a)
ps["0.0.0"]
ps.fetch("posts.9999", "your default")
ps.fetch("posts.9999") { |key| "some_default_for_#{key}" }
Ignore NoMethodError
for Unknown Properties
ps = PropertyString.new(product, :raise_if_method_missing => false)
ps["company.does_not_exist!"]
Restrict Methods That Can Be Called
ps = PropertyString.new(product, :whitelist => { Product => %w[company], Company => %w[name] })
ps["id"]
ps["company.id"]
Currently does not work when a superclass is whitelisted but trivial to add.
See Also
- PropertyHash - Access a nested Ruby Hash using Java-style properties as keys.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/sshaw/property_string.
License
The gem is available as open source under the terms of the MIT License.