Module: Agris::XmlModel::InstanceMethods

Defined in:
lib/agris/xml_model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



10
11
12
# File 'lib/agris/xml_model.rb', line 10

def hash
  @hash
end

Instance Method Details

#attributesObject



12
13
14
15
16
17
# File 'lib/agris/xml_model.rb', line 12

def attributes
  (instance_variables - [:@hash]).each_with_object({}) do |ivar, new_hash|
    new_hash[ivar.to_s.delete('@')] = instance_variable_get(ivar)
    new_hash
  end
end

#initialize(hash = {}) ⇒ Object



19
20
21
22
23
24
# File 'lib/agris/xml_model.rb', line 19

def initialize(hash = {})
  @hash = hash
  @hash.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end

#to_xml_hashObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/agris/xml_model.rb', line 26

def to_xml_hash
  ignore_attributes = (xml_ignore_attributes + [:hash]).map(&:to_s)
  match_attributes = attributes.keys - ignore_attributes
  attributes
    .select { |key, _value| match_attributes.include?(key) }
    .each_with_object({}) do |(name, value), new_hash|
    new_hash["@#{name.delete('_')}".to_sym] = value
    new_hash
  end
end

#xml_ignore_attributesObject



37
38
39
# File 'lib/agris/xml_model.rb', line 37

def xml_ignore_attributes
  []
end