Class: MyData::XmlGenerator
- Inherits:
-
Object
- Object
- MyData::XmlGenerator
- Defined in:
- lib/my_data/xml_generator.rb
Instance Attribute Summary collapse
-
#class_name ⇒ Object
readonly
Returns the value of attribute class_name.
-
#container ⇒ Object
readonly
Returns the value of attribute container.
-
#mappings ⇒ Object
readonly
Returns the value of attribute mappings.
-
#parent_namespace ⇒ Object
readonly
Returns the value of attribute parent_namespace.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Instance Method Summary collapse
-
#initialize(resource, parent_namespace: nil) ⇒ XmlGenerator
constructor
A new instance of XmlGenerator.
- #namespace ⇒ Object
- #to_xml ⇒ Object
- #to_xml_structure ⇒ Object
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_name ⇒ Object (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 |
#container ⇒ Object (readonly)
Returns the value of attribute container.
5 6 7 |
# File 'lib/my_data/xml_generator.rb', line 5 def container @container end |
#mappings ⇒ Object (readonly)
Returns the value of attribute mappings.
5 6 7 |
# File 'lib/my_data/xml_generator.rb', line 5 def mappings @mappings end |
#parent_namespace ⇒ Object (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 |
#resource ⇒ Object (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
#namespace ⇒ Object
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_xml ⇒ Object
19 20 21 |
# File 'lib/my_data/xml_generator.rb', line 19 def to_xml to_doc.parent.to_xml end |
#to_xml_structure ⇒ Object
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 |