Module: Xtify::Model

Extended by:
ActiveSupport::Concern
Included in:
Action, Device, Message
Defined in:
lib/xtify/model.rb

Instance Method Summary collapse

Instance Method Details

#as_json(opts = {}) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/xtify/model.rb', line 50

def as_json(opts={})
  json = {}
  (fields + one_associations.keys).each do |field|
    value = send(field)
    json[field] = value unless value.blank?
  end
  json
end

#fieldsObject



26
27
28
# File 'lib/xtify/model.rb', line 26

def fields
  self.class.fields
end

#initialize(opts = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/xtify/model.rb', line 34

def initialize(opts={})
  opts = opts.symbolize_keys
  fields.each do |field|
    self.send("#{field}=", opts[field])
  end

  one_associations.each do |assoc, assoc_opts|
    value = opts[assoc]
    if value.is_a?(Hash)
      klass = assoc_opts[:type] || File.join("xtify", assoc.to_s).classify.constantize
      value = klass.new(value)
    end
    self.send("#{assoc}=", value)
  end
end

#one_associationsObject



30
31
32
# File 'lib/xtify/model.rb', line 30

def one_associations
  self.class.one_associations
end