Module: Agris::XmlModel::ClassMethods

Defined in:
lib/agris/xml_model.rb

Instance Method Summary collapse

Instance Method Details

#from_json_hash(hash) ⇒ Object



43
44
45
# File 'lib/agris/xml_model.rb', line 43

def from_json_hash(hash)
  Object.const_get(name).new(hash)
end

#from_xml_hash(hash) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/agris/xml_model.rb', line 47

def from_xml_hash(hash)
  klass = Object.const_get(name)
  attribute_map =
    klass::ATTRIBUTE_NAMES
    .each_with_object({}) do |name, new_hash|
      new_hash[name.delete('_').to_s] = name
    end

  translated_hash = hash.each_with_object({}) do |(key, value), new_hash|
    attribute_name = attribute_map[key]

    new_hash[attribute_name] = value if attribute_name
    new_hash
  end

  klass.new(translated_hash)
end

#pluralized_nameObject



65
66
67
# File 'lib/agris/xml_model.rb', line 65

def pluralized_name
  "#{name.split('::').last.downcase}s"
end