Module: ParserCore::BaseBuilder

Includes:
Mega
Defined in:
lib/parser_core/base_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mega

#mega

Instance Attribute Details

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/parser_core/base_builder.rb', line 5

def data
  @data
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/parser_core/base_builder.rb', line 5

def name
  @name
end

#namespacesObject

Returns the value of attribute namespaces.



5
6
7
# File 'lib/parser_core/base_builder.rb', line 5

def namespaces
  @namespaces
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/parser_core/base_builder.rb', line 5

def options
  @options
end

Instance Method Details

#add_attributes_and_namespaces(node) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/parser_core/base_builder.rb', line 45

def add_attributes_and_namespaces(node)
  if data.key? :attributes
    data[:attributes].each { |k, v| node[k] = v }
  end

  if namespaces.any?
    namespaces.each do |prefix, uri|
      node["xmlns:#{prefix}"] = uri
    end
  end

  node
end

#build_element(name, content, attributes = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/parser_core/base_builder.rb', line 27

def build_element(name, content, attributes = {})
  attributes ||= attributes || {}

  element = Ox::Element.new(name)

  attributes.each { |k, v| element[k] = v }

  element << content if content

  element
end

#builderObject



39
40
41
42
43
# File 'lib/parser_core/base_builder.rb', line 39

def builder
  root = Ox::Element.new(name)

  root
end

#initialize(name, data = {}, options = {}) ⇒ Object



7
8
9
10
11
12
# File 'lib/parser_core/base_builder.rb', line 7

def initialize(name, data = {}, options = {})
  @name = name
  @data = data || {}
  @options = options || {}
  @namespaces = options[:namespaces] || {}
end

#to_xmlObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/parser_core/base_builder.rb', line 14

def to_xml
  encoding = options[:encoding]

  doc_options = { version: '1.0' }
  doc_options[:encoding] = encoding if encoding
  doc = Ox::Document.new(doc_options)
  doc << builder

  dump_options = { with_xml: true }
  dump_options[:encoding] = encoding if encoding
  Ox.dump(doc, dump_options)
end