Sha256: 9abe703fc9a088be3e1a1650837e198831cbfe350c6fe3af4c6b72d254f249c3

Contents?: true

Size: 1 KB

Versions: 3

Compression:

Stored size: 1 KB

Contents

module ActiveGit
  class ModelParser

    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

    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

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
active_git-0.0.10 lib/active_git/model_parser.rb
active_git-0.0.9 lib/active_git/model_parser.rb
active_git-0.0.8 lib/active_git/model_parser.rb