Module: OpenStructFactory

Defined in:
lib/open_struct_factory.rb,
lib/open_struct_factory/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.create(hash, &block) ⇒ Object

Creates an OpenStruct from the given hash

  • It recursively transform nested hashes into OpenStruct

  • It recursively goes through arrays, transforming hashes into OpenStruct

  • An optional block can be passed to process the hash keys



9
10
11
12
13
14
15
16
# File 'lib/open_struct_factory.rb', line 9

def self.create(hash, &block) # :yields: key
  properties = {}
  hash.each do |key, value|
    property_name = block_given? ? (yield key) : key
    properties[property_name] = process_value(value, &block)
  end
  OpenStruct.new(properties)
end