Class: DslBuilder
- Inherits:
-
Object
- Object
- DslBuilder
- Defined in:
- lib/roundtrip_xml/dsl_builder.rb
Instance Method Summary collapse
-
#initialize(xmlStr, runtime, root_class) ⇒ DslBuilder
constructor
A new instance of DslBuilder.
- #to_dsl ⇒ Object
- #write_attrs(clazz, xml, inset = '') ⇒ Object
Constructor Details
#initialize(xmlStr, runtime, root_class) ⇒ DslBuilder
Returns a new instance of DslBuilder.
4 5 6 7 8 |
# File 'lib/roundtrip_xml/dsl_builder.rb', line 4 def initialize(xmlStr, runtime, root_class) @doc = Nokogiri::XML(xmlStr) @runtime = runtime @root_class = root_class end |
Instance Method Details
#to_dsl ⇒ Object
10 11 12 |
# File 'lib/roundtrip_xml/dsl_builder.rb', line 10 def to_dsl write_attrs @runtime.fetch(@root_class), @doc.root end |
#write_attrs(clazz, xml, inset = '') ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/roundtrip_xml/dsl_builder.rb', line 14 def write_attrs(clazz, xml, inset='') clazz.roxml_attrs.inject('') do |out, attr| # if no xml was found for the given selector, the attribute was optional return '' unless xml accessor = attr.accessor selector = attr.name if attr.sought_type == :text text_element = xml.children.find {|c| c.name == selector} out += inset + "#{accessor} '#{text_element.content}'\n" if text_element elsif attr.sought_type == :attr child_attribute = xml.attributes[selector] out += inset + "#{accessor} '#{child_attribute.content}'\n" if child_attribute elsif !attr.array? # the element that matches `selector` may not be in this node child_element = xml.children.find {|c| c.name == selector} if child_element out += inset + "#{accessor} do\n#{write_attrs attr.sought_type, child_element, inset + ' '}#{inset}end\n" end else xml.xpath(selector).each do |node| out += inset + "#{accessor} do\n#{write_attrs attr.sought_type, node, inset + ' '}#{inset}end\n" if inset == '' out += "\n" end end end out end end |