Recursive/Deep open struct for hashes in ruby

So, I had to convert a hash into an open struct to make accessing things easy. Here is the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

require 'ostruct'

module DeepStruct

def to_ostruct

case self
when Hash
root = OpenStruct.new(self)
self.each_with_object(root) do |(k,v), o|
o.send("#{k}=", v.to_ostruct)
end
root
when Array
self.map do |v|
v.to_ostruct
end
else
self
end

end

end

Object.send(:include, DeepStruct)

I am currently working on LiveForm which makes setting up contact forms on your website a breeze.