Module: Iora::Associations::HasOne

Includes:
Helpers
Included in:
Models::Element
Defined in:
lib/iora/associations/has_one.rb

Overview

The association HasOne defines a macro method for mapping an XML element to a Ruby method.

The macro is available to Element subclasses. For example:

class Population < Iora::Element; end

class Country < Iora::Element
  has_one :president, String
  has_one :population, Population
end

country = Country.new(<<XML)
  <country>
    <president>Jacob Zuma</president>
    <population census="2010">52.98 million</population>
  </country>
XML
country.president # => 'Jacob Zuma'
country.population # => #<Population @element=...>

Associations are inspired by Fullscreen/yt.

Instance Method Summary collapse

Methods included from Helpers

#parse_element, #tag_for

Instance Method Details

#has_one(name, klass, tag: tag_for(name)) ⇒ Object

Define a method to access an XML element.


36
37
38
39
40
# File 'lib/iora/associations/has_one.rb', line 36

def has_one(name, klass, tag: tag_for(name))
  define_method name do
    self.class.parse_element(element.at_css(tag), klass)
  end
end