Class: Gxapi::Ostruct

Inherits:
Object
  • Object
show all
Defined in:
lib/gxapi/ostruct.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Ostruct

recursive open struct



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/gxapi/ostruct.rb', line 4

def initialize(hash)

  @hash = {}

  self.send(:extend, self.generated_methods)
  hash.each_pair do |key, val|
    # set the key to an underscored verison of itself
    key = self.underscore(key)
    self.define_accessors(key)
    self.send("#{key}=", self.convert_value_to_ostruct(val))
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (protected)

dynamically define getter and setter when an unknown setter is called



69
70
71
72
73
74
75
# File 'lib/gxapi/ostruct.rb', line 69

def method_missing(meth, *args, &block)
  if meth.to_s =~ /=$/
    self.define_accessors(meth.to_s.gsub(/=$/,''))
    return self.send(meth, *args, &block)
  end
  super
end

Instance Method Details

#to_hashObject



17
18
19
20
21
22
23
# File 'lib/gxapi/ostruct.rb', line 17

def to_hash
  ret = {}
  @hash.dup.each_pair do |key, val|
    ret[key] = self.convert_value_from_ostruct(val)
  end
  ret
end