Class: Tea::Model
- Inherits:
-
Object
- Object
- Tea::Model
- Defined in:
- lib/tea_core.rb
Overview
The Model class
Instance Method Summary collapse
-
#initialize(hash = nil) ⇒ Model
constructor
new Model from hash.
- #to_hash ⇒ Object
Constructor Details
#initialize(hash = nil) ⇒ Model
new Model from hash
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/tea_core.rb', line 30 def initialize(hash = nil) return if hash.nil? names = self.class.name_mapping types = self.class.type_mapping names.each do |key, value| type = types[key] if type.instance_of? Hash # array if type['type'].instance_of? String if type['type'] == 'array' instance_variable_set('@' + key, hash[value].map do |item| type['itemType'].new(item) end) end else instance_variable_set('@' + key, type.new(hash[value])) end elsif type.instance_of? Class instance_variable_set('@' + key, type.new(hash[value])) else instance_variable_set('@' + key, hash[value]) end end end |
Instance Method Details
#to_hash ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/tea_core.rb', line 20 def to_hash hash = {} names = self.class.name_mapping names.each do |key, value| hash[value] = instance_variable_get('@' + key) end hash end |