class Tea::Model

The Model class

Public Class Methods

new(hash = nil) click to toggle source

new Model from hash

# 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

Public Instance Methods

to_hash() click to toggle source
# 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