class OpenHash

OpenHash lets Hash called and assigned by the key in chainable way. Example:

person = OpenHash.new(name: "John", hometown: { city: "London" }, pets: [{ name: "Mia", animal: "Cat" }])
person.name #=> "John"
person.hometown.city #=> "London"
person.pets[0].name #=> "Mia"

person = OpenHash.new
person.name = "Lion"
person.hometown.city = "Paris"
person.parents.father.name = "Heron"
person #=> { name: "Lion", hometown: { city: "Paris" }, parents: { father: { name: "Heron" } } }

Public Class Methods

new(hash = {}) click to toggle source
Calls superclass method
# File lib/open_hash.rb, line 20
def initialize(hash = {})
  hash = hash.each_with_object({}) do |(k, v), r|
    r[k] = case v
    when Hash, OpenStruct
      OpenHash.new(v)
    when Array
      v.map { |x| OpenHash.new(x) }
    else
      v
    end
  end

  super
end