Class: MyData::XmlGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/my_data/xml_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, parent_namespace: nil) ⇒ XmlGenerator

Returns a new instance of XmlGenerator.



7
8
9
10
11
12
13
# File 'lib/my_data/xml_generator.rb', line 7

def initialize(resource, parent_namespace: nil)
  @resource = resource
  @mappings = resource.class.mappings
  @class_name = resource.class.name
  @container = resource.class.container
  @parent_namespace = parent_namespace
end

Instance Attribute Details

#class_nameObject (readonly)

Returns the value of attribute class_name.



5
6
7
# File 'lib/my_data/xml_generator.rb', line 5

def class_name
  @class_name
end

#containerObject (readonly)

Returns the value of attribute container.



5
6
7
# File 'lib/my_data/xml_generator.rb', line 5

def container
  @container
end

#mappingsObject (readonly)

Returns the value of attribute mappings.



5
6
7
# File 'lib/my_data/xml_generator.rb', line 5

def mappings
  @mappings
end

#parent_namespaceObject (readonly)

Returns the value of attribute parent_namespace.



5
6
7
# File 'lib/my_data/xml_generator.rb', line 5

def parent_namespace
  @parent_namespace
end

#resourceObject (readonly)

Returns the value of attribute resource.



5
6
7
# File 'lib/my_data/xml_generator.rb', line 5

def resource
  @resource
end

Instance Method Details

#namespaceObject



15
16
17
# File 'lib/my_data/xml_generator.rb', line 15

def namespace
  @namespace ||= resource.class.module_parent_name.split("::").last.downcase
end

#to_xmlObject



19
20
21
# File 'lib/my_data/xml_generator.rb', line 19

def to_xml
  to_doc.parent.to_xml
end

#to_xml_structureObject



23
24
25
26
27
28
29
30
31
# File 'lib/my_data/xml_generator.rb', line 23

def to_xml_structure
  resource.attributes.each_with_object([]) do |(key, value), structure|
    next if value.nil? || (value.respond_to?(:empty?) && value.empty?)

    value = value_to_xml_structure(key, value)

    structure.push(*extract_attributes(key, value))
  end
end