lib/scaffold_parser/scaffolders/xsd.rb in scaffold_parser-0.3.0 vs lib/scaffold_parser/scaffolders/xsd.rb in scaffold_parser-0.4.0

- old
+ new

@@ -58,70 +58,81 @@ def initialize(raw) @raw = raw end + def attributes + raw.attributes + end + private def at(locator) return nil if raw.nil? - raw[locator] + element = raw.locate(locator.to_s).first + + if element + text = WithAttributes.new(element.text) + text.attributes = element.attributes + text + end end + def has?(locator) + raw.locate(locator).any? + end + def submodel_at(klass, locator) - element_xml = at locator + element_xml = raw.locate(locator).first klass.new(element_xml) if element_xml end def array_of_at(klass, locator) return EMPTY_ARRAY if raw.nil? - elements = raw.dig(*locator) || EMPTY_ARRAY - if elements.is_a?(Hash) || elements.is_a?(String) - elements = [elements] - end + elements = raw.locate([*locator].join('/')) - elements.map do |raw| - klass.new(raw) + elements.map do |element| + klass.new(element) end end - - def all(locator) - return EMPTY_ARRAY if raw.nil? - - result = raw[locator] - - if result.is_a? Hash - [result] - elsif result.is_a? Array - result - else - EMPTY_ARRAY - end - end end end TEMPLATE end def base_builder_template <<~TEMPLATE module Builders module BaseBuilder - attr_accessor :attributes, :element_name + attr_accessor :name, :data - def initialize(attributes = {}, element_name) - @element_name = element_name - @attributes = attributes || {} + def initialize(name, data = {}) + @name = name + @data = data || {} end def to_xml doc = Ox::Document.new(version: '1.0') doc << builder Ox.dump(doc, with_xml: true) + end + + def build_element(name, content) + element = Ox::Element.new(name) + if content.respond_to? :attributes + content.attributes.each { |k, v| element[k] = v } + end + + if content.respond_to? :value + element << content.value if content.value + else + element << content if content + end + element end end end TEMPLATE end