class LazyXmlModel::ObjectProxy

Attributes

association_name[R]
options[R]
xml_document[R]
xml_parent_element[R]

Public Class Methods

new(association_name, xml_document, xml_parent_element, options = {}) click to toggle source
# File lib/lazy_xml_model/object_proxy.rb, line 5
def initialize(association_name, xml_document, xml_parent_element, options = {})
  @association_name = association_name
  @xml_document = xml_document
  @xml_parent_element = xml_parent_element
  @options = options
end

Public Instance Methods

attributes=(attributes) click to toggle source
# File lib/lazy_xml_model/object_proxy.rb, line 50
def attributes=(attributes)
  if object.present?
    object.assign_attributes(attributes) # Update the object
  else
    build_object(attributes) # Build the object
  end
end
build_object(params = {}) click to toggle source

Builder Method

# File lib/lazy_xml_model/object_proxy.rb, line 38
def build_object(params = {})
  new_object =
    begin
      klass.new(params)
    # Incase the class does not include ActiveModel::AttributeAssignment
    rescue ArgumentError
      klass.new
    end

  self.object = new_object
end
object() click to toggle source

Getter Method

# File lib/lazy_xml_model/object_proxy.rb, line 13
def object
  element = xml_parent_element.elements.find { |element| element.name == association_name }
  return unless element.present?

  @object ||= begin
    new_object = klass.new
    new_object.xml_parent_element = xml_parent_element
    new_object.xml_element = element
    new_object
  end
end
object=(new_object) click to toggle source

Setter Method

# File lib/lazy_xml_model/object_proxy.rb, line 26
def object=(new_object)
  @object.delete if @object.present?

  @object = new_object

  xml_parent_element.add_child(
    new_object.xml_element
  )
  new_object.xml_parent_element = xml_parent_element
end

Private Instance Methods

klass() click to toggle source
# File lib/lazy_xml_model/object_proxy.rb, line 60
def klass
  options[:class_name].constantize
end