Class: ActiveGit::ModelParser

Inherits:
Object
  • Object
show all
Defined in:
lib/active_git/model_parser.rb

Class Method Summary collapse

Class Method Details

.from_json(model, json) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/active_git/model_parser.rb', line 20

def self.from_json(model, json)
  record = model.new
  attributes = json.is_a?(Hash) ? json : JSON.parse(json)
  attributes.each do |attr, value|
    if model.column_names.include?(attr.to_s)
      if model.columns_hash[attr.to_s].type == :datetime && value.is_a?(String)
        record.send("#{attr}=", Time.parse(value).utc)
      else
        record.send("#{attr}=", value)
      end
    end
  end
  record
end

.instances(model, json) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/active_git/model_parser.rb', line 4

def self.instances(model, json)
  list = [from_json(model, json)]
  attributes = json.is_a?(Hash) ? json : JSON.parse(json)
  attributes.each do |attr, value|
    if model.reflections.has_key?(attr.to_sym)
      attr_model = attr.to_s.classify.constantize
      if value.is_a? Array
        value.each {|json| list = list + instances(attr_model, json)}
      else
        list = list + instances(attr_model, value)
      end
    end
  end
  list
end