Method: Hash#to_ostruct_recurse

Defined in:
lib/standard/facets/ostruct/to_ostruct.rb

#to_ostruct_recurse(exclude = {}) ⇒ Object

Like #to_ostruct but recusively objectifies all hash elements as well.

o = {'a' => { 'b' => 1 }}.to_ostruct_recurse
o.a.b  #=> 1

The exclude parameter is used internally to prevent infinite recursion and is not intended to be utilized by the end-user. But for more advance use, if there is a particular subhash you would like to prevent from being converted to an OpoenStruct then include it in the exclude hash referencing itself. e.g.

h = { 'a' => { 'b' => 1 } }
o = h.to_ostruct_recurse( { h['a'] => h['a'] } )
o.a['b']  #=> 1

CREDIT: Alison Rowland, Jamie Macey, Mat Schaffer



48
49
50
51
52
53
54
55
56
# File 'lib/standard/facets/ostruct/to_ostruct.rb', line 48

def to_ostruct_recurse(exclude={})
  return exclude[self] if exclude.key?( self )
  o = exclude[self] = OpenStruct.new
  h = self.dup
  each_pair do |k,v|
    h[k] = v.to_ostruct_recurse( exclude ) if v.respond_to?(:to_ostruct_recurse)
  end
  o.merge!(h)
end