Class: ZapiObject
- Inherits:
-
Object
- Object
- ZapiObject
- Defined in:
- lib/zapi_object.rb
Instance Method Summary collapse
- #_generate(hash:) ⇒ Object
-
#initialize(hash:) ⇒ ZapiObject
constructor
A new instance of ZapiObject.
- #to_h ⇒ Object
Constructor Details
#initialize(hash:) ⇒ ZapiObject
3 4 5 |
# File 'lib/zapi_object.rb', line 3 def initialize hash: _generate hash: hash end |
Instance Method Details
#_generate(hash:) ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/zapi_object.rb', line 7 def _generate hash: return nil unless hash.is_a? Hash hash.each do |k,v| self.instance_variable_set("@#{k}", v) ## create and initialize an instance variable for this key/value pair self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")}) ## create the getter that returns the instance variable self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)}) ## create the setter that sets the instance variable end return self end |
#to_h ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/zapi_object.rb', line 17 def to_h hash = {} self.instance_variables.each do |var| sym = var.to_s.delete("@").to_sym hash[sym] = self.instance_variable_get(var) end hash end |