class Newgistics::XmlMarshaller

Public Instance Methods

assign_attributes(object, root) click to toggle source
# File lib/newgistics/xml_marshaller.rb, line 3
def assign_attributes(object, root)
  marshall_attributes(object, root)
  marshall_elements(object, root)
end

Private Instance Methods

assign_attribute(object, attribute, value) click to toggle source
# File lib/newgistics/xml_marshaller.rb, line 56
def assign_attribute(object, attribute, value)
  setter = "#{attribute}="
  if object.is_a?(Hash) || object.respond_to?(setter)
    object[attribute.to_sym] = value unless value.empty?
  end
end
assign_nested_attribute(object, element) click to toggle source
# File lib/newgistics/xml_marshaller.rb, line 27
def assign_nested_attribute(object, element)
  attribute = attribute_name(element)
  attribute_set = object.class.attribute_set[attribute]
  return if attribute_set.nil?

  if attribute_set.type.primitive == Array
    object[attribute] = build_list(attribute_set.member_type.primitive, element)
  else
    object[attribute] = build_object(attribute_set.type.primitive, element)
  end
end
assign_simple_attribute(object, element) click to toggle source
# File lib/newgistics/xml_marshaller.rb, line 51
def assign_simple_attribute(object, element)
  attribute = attribute_name(element)
  assign_attribute(object, attribute, element.text)
end
attribute_name(element) click to toggle source
# File lib/newgistics/xml_marshaller.rb, line 63
def attribute_name(element)
  StringHelper.underscore(element.name)
end
build_list(item_class, element) click to toggle source
# File lib/newgistics/xml_marshaller.rb, line 39
def build_list(item_class, element)
  element.css(item_class.element_selector).map do |child|
    build_object(item_class, child)
  end
end
build_object(klass_name, element) click to toggle source
# File lib/newgistics/xml_marshaller.rb, line 45
def build_object(klass_name, element)
  klass_name.new.tap do |new_object|
    assign_attributes(new_object, element)
  end
end
marshall_attributes(object, root) click to toggle source
# File lib/newgistics/xml_marshaller.rb, line 10
def marshall_attributes(object, root)
  root.attributes.values.each do |attribute|
    transformed_name = attribute_name(attribute)
    assign_attribute(object, transformed_name, attribute.value)
  end
end
marshall_elements(object, root) click to toggle source
# File lib/newgistics/xml_marshaller.rb, line 17
def marshall_elements(object, root)
  root.elements.each do |element|
    if element.elements.any?
      assign_nested_attribute(object, element)
    else
      assign_simple_attribute(object, element)
    end
  end
end