Class: Aggeratio::XML

Inherits:
Base
  • Object
show all
Defined in:
lib/aggeratio/xml.rb

Constant Summary

Constants inherited from Base

Base::LEVELS

Instance Attribute Summary

Attributes inherited from Base

#aggregator, #name, #parameters, #root

Instance Method Summary collapse

Methods inherited from Base

#build_variable, #class_name, #conditionate, #human_name_of, #human_value_of, #normalize_name, #parameter_initialization, #properties, #queries, #value_of

Constructor Details

#initialize(aggregator) ⇒ XML

Returns a new instance of XML.


3
4
5
6
# File 'lib/aggeratio/xml.rb', line 3

def initialize(aggregator)
  super(aggregator)
  @minimum_level = :api
end

Instance Method Details

#buildObject


8
9
10
11
12
13
14
15
16
# File 'lib/aggeratio/xml.rb', line 8

def build
  # Build code
  code = parameter_initialization
  code << "builder = Nokogiri::XML::Builder.new do |xml|\n"
  code << build_element(@root).dig
  code << "end\n"
  code << "return builder.to_xml\n"
  code
end

#build_cell(element) ⇒ Object


87
88
89
# File 'lib/aggeratio/xml.rb', line 87

def build_cell(element)
  "xml.send('#{normalize_name(element)}', #{xml_value_of(element)})\n"
end

#build_children_of(element) ⇒ Object


40
41
42
# File 'lib/aggeratio/xml.rb', line 40

def build_children_of(element)
  build_elements(element.children)
end

#build_element(element) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/aggeratio/xml.rb', line 18

def build_element(element)
  method_name = "build_#{element.name}".to_sym
  code = ''
  if respond_to?(method_name)
    code << conditionate(send(method_name, element), element)
  elsif element.name.to_s != 'comment'
    Rails.logger.warn("Markup <#{element.name}> is unknown or not implemented")
    code << "# #{element.name}: not implemented\n"
  end
  code
end

#build_elements(elements) ⇒ Object


30
31
32
33
34
35
36
37
38
# File 'lib/aggeratio/xml.rb', line 30

def build_elements(elements)
  code = ''
  for element in elements
    next if %w[property title].include?(element.name.to_s)
    code << build_element(element)
  end
  code << "# No elements\n" if code.blank?
  code
end

#build_matrix(element) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/aggeratio/xml.rb', line 70

def build_matrix(element)
  item = element.attr('for')
  code = build_children_of(element)
  if element.has_attribute?('in')
    code = "for #{item} in #{element.attr('in')}\n" +
           build_elements(element.xpath('xmlns:variable')).dig +
           "  xml.send('#{normalize_name(item)}') do\n" +
           code.dig(2) +
           "  end\n" \
           "end\n"
  end
  if element.has_attribute?('name')
    code = "xml.send('#{normalize_name(element)}') do\n" + code.dig + "end\n"
  end
  code
end

#build_properties_hash(items, options = {}) ⇒ Object


91
92
93
94
95
96
97
98
99
# File 'lib/aggeratio/xml.rb', line 91

def build_properties_hash(items, options = {})
  var = options[:var] || 'properties'
  code = "#{var} = {}\n"
  max = items.collect { |i| i.attr('name').size }.max
  items.collect do |item|
    code << conditionate(("#{var}['" + normalize_name(item) + "'] = ").ljust(max + 7 + var.size) + xml_value_of(item) + "\n", item)
  end
  code
end

#build_section(element) ⇒ Object


59
60
61
62
63
64
65
66
67
68
# File 'lib/aggeratio/xml.rb', line 59

def build_section(element)
  name = element.attr('name').to_s
  code = ''
  code << build_elements(element.xpath('xmlns:variable'))
  code << build_properties_hash(element.xpath('*[self::xmlns:property or self::xmlns:title]'), var: 'attrs')
  code << "xml.send('#{normalize_name(name)}', attrs) do\n"
  code << build_elements(element.xpath('*[not(self::xmlns:variable)]')).dig
  code << "end\n"
  code
end

#build_sections(element) ⇒ Object


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

def build_sections(element)
  item  = element.attr('for')
  code  = "for #{item} in #{element.attr('in')}\n"
  code << build_elements(element.xpath('xmlns:variable')).dig
  code << build_properties_hash(element.xpath('*[self::xmlns:property or self::xmlns:title]'), var: 'attrs').dig
  code << "  xml.send('#{normalize_name(item)}', attrs) do\n"
  code << build_children_of(element).dig(2)
  code << "  end\n"
  code << "end\n"
  if element.has_attribute?('name')
    code = "xml.send('#{normalize_name(element)}') do\n" + code.dig + "end\n"
  end
  code
end

#xml_value_of(*args) ⇒ Object


101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/aggeratio/xml.rb', line 101

def xml_value_of(*args)
  options = args.extract_options!
  element = args.shift
  value = value_of(element)
  type = (element.has_attribute?('type') ? element.attr('type').to_s : :string).to_s.tr('-', '_').to_sym
  code = if type == :date || type == :datetime
           "(#{value} ? #{value}.xmlschema : #{value})"
         else
           value
         end
  code
end