Sha256: 6ce72f8836718132ea71fd9fd6e6bf874c1b30f994217a2f51aa7e81dcabba3d

Contents?: true

Size: 520 Bytes

Versions: 5

Compression:

Stored size: 520 Bytes

Contents

# turn hashes into openstruct-like objects with dotted access
class Hash
  def method_missing(meth, *args, &block)
    if args.size == 0
      self[meth.to_s] || self[meth.to_sym]
    end
  end
  
  # in: "foo.bar.baz"
  # out: self.foo.bar.baz == ['foo']['bar']['baz']
  def value_of_dotted_property(property)
    methods = property.split('.')
    # call each method in the dotted chain until we get to the result
    hash = self
    methods.each do |method|
      hash = hash.send(method)
    end
    hash
  end
end


Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
esod-client-0.3.0 lib/hash.rb
esod-client-0.2.1 lib/hash.rb
esod-client-0.2.0 lib/hash.rb
esod-client-0.1.1 lib/hash.rb
esod-client-0.1.0 lib/hash.rb