Module: HappyMapper

Defined in:
lib/happymapper.rb,
lib/happymapper/item.rb,
lib/happymapper/element.rb,
lib/happymapper/version.rb,
lib/happymapper/attribute.rb

Defined Under Namespace

Modules: ClassMethods Classes: Attribute, Element, Item

Constant Summary collapse

DEFAULT_NS =
"happymapper"
Version =
'0.3.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



15
16
17
18
19
# File 'lib/happymapper.rb', line 15

def self.included(base)
  base.instance_variable_set("@attributes", {})
  base.instance_variable_set("@elements", {})
  base.extend ClassMethods
end

Instance Method Details

#to_xmlObject



132
133
134
135
136
# File 'lib/happymapper.rb', line 132

def to_xml
  document = XML::Document.new
  document.root = to_xml_node
  document.to_s
end

#to_xml_nodeObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/happymapper.rb', line 116

def to_xml_node
  node = XML::Node.new(self.class.tag_name)

  self.class.attributes.each do |attribute|
    value = self.send(attribute.method_name)
    node.attributes[attribute.name] = value.to_s unless value.nil?
  end

  self.class.elements.each do |element|
    value = self.send(element.method_name)
    has_one_and_has_many_to_xml(node, element, value) unless value.nil?
  end

  node
end