Class: Clearsale::Object

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/clearsale/object.rb

Instance Method Summary collapse

Instance Method Details

#new_ostruct_member(name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/clearsale/object.rb', line 5

def new_ostruct_member(name)
  name = name.to_sym
  unless self.respond_to?(name)
    class << self; self; end.class_eval do
      define_method(name) {
        v = @table[name]
        case v
          when Hash
            Object.new(v)
          when Array
            v.each_with_index { |item, index| v[index] = Object.new(item) }
          else
            v
          end
      }
      define_method("#{name}=") { |x| modifiable[name] = x }
      define_method("#{name}_as_a_hash") { @table[name] }
    end
  end
  name
end