Module: Agris::XmlModel::InstanceMethods
- Defined in:
- lib/agris/xml_model.rb
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
Instance Method Summary collapse
- #attributes ⇒ Object
- #initialize(hash = {}) ⇒ Object
- #to_xml_hash ⇒ Object
- #xml_ignore_attributes ⇒ Object
Instance Attribute Details
#hash ⇒ Object (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
#attributes ⇒ Object
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_hash ⇒ Object
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_attributes ⇒ Object
37 38 39 |
# File 'lib/agris/xml_model.rb', line 37 def xml_ignore_attributes [] end |