Class: XmlSchemaMapper::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/xml_schema_mapper/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, parent, namespace_resolver) ⇒ Builder

Returns a new instance of Builder.



9
10
11
12
13
14
15
16
# File 'lib/xml_schema_mapper/builder.rb', line 9

def initialize(source, parent, namespace_resolver)
  @parent             = parent.is_a?(Nokogiri::XML::Document) ? parent.root : parent
  @document           = parent.document
  @source             = source
  @klass              = source.class
  @namespace_resolver = namespace_resolver
  @schema             = @klass._schema
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



7
8
9
# File 'lib/xml_schema_mapper/builder.rb', line 7

def document
  @document
end

#namespace_resolverObject (readonly)

Returns the value of attribute namespace_resolver.



7
8
9
# File 'lib/xml_schema_mapper/builder.rb', line 7

def namespace_resolver
  @namespace_resolver
end

#parentObject (readonly)

Returns the value of attribute parent.



7
8
9
# File 'lib/xml_schema_mapper/builder.rb', line 7

def parent
  @parent
end

Instance Method Details

#buildObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/xml_schema_mapper/builder.rb', line 22

def build
  elements.each do |element|
    add_element_namespace_to_root!(element)
    node = create_node(element)

    parent << node if can_add?(element, node)

    if node.is_a?(Nokogiri::XML::NodeSet)
      node.each { |n| n.namespace = nil if element.namespace.nil? }
    else
      node.namespace = nil if element.namespace.nil?
    end
  end
  self
end

#can_add?(element, node) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/xml_schema_mapper/builder.rb', line 38

def can_add?(element, node)
  node.is_a?(Nokogiri::XML::NodeSet) || node.content.present?
end

#elementsObject



18
19
20
# File 'lib/xml_schema_mapper/builder.rb', line 18

def elements
  @klass.type.elements.values.map { |e| XmlSchemaMapper::Element.new e }
end