Class: Aggeratio::DocumentFragment

Inherits:
Base
  • Object
show all
Defined in:
lib/aggeratio/document_fragment.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) ⇒ DocumentFragment

Returns a new instance of DocumentFragment.


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

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

Instance Method Details

#buildObject


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

def build
  # Build code
  document_variable = '__doc__'
  code = parameter_initialization
  code << "#{document_variable} = Nokogiri::HTML::DocumentFragment.parse('')\n"
  code << "builder = Nokogiri::HTML::Builder.with(#{document_variable}) do |xml|\n"
  code << build_element(@root).dig
  code << "end\n"
  code << "return #{document_variable}.to_html.html_safe\n"
  code
end

#build_children_of(element) ⇒ Object


42
43
44
# File 'lib/aggeratio/document_fragment.rb', line 42

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

#build_element(element) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/aggeratio/document_fragment.rb', line 20

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


32
33
34
35
36
37
38
39
40
# File 'lib/aggeratio/document_fragment.rb', line 32

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

#build_matrix(element, mode = :table, vertical = false) ⇒ Object


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/aggeratio/document_fragment.rb', line 92

def build_matrix(element, mode = :table, vertical = false)
  item = element.attr('for')
  columns = element.xpath("*[(self::xmlns:cell or self::xmlns:matrix) and (not(@level) or @level != 'api')]")
  code = "xml.table(:class => 'matrix') do\n"

  if mode == :table || mode == :head
    code << "  xml.thead do\n"
    code << build_table_serie(columns, vertical) do |col, cell_code|
      if col.name == 'cell'
        cell_code << "xml.th(#{human_name_of(col)}, :class => '#{normalize_name(col)}')\n"
      elsif col.name == 'matrix'
        cell_code << "xml.th(:class => 'matrix #{normalize_name(col)}') do\n"
        # cell_code << "  xml.em(#{human_name_of(col)})\n"
        cell_code << build_matrix(col, :head, !vertical).dig
        cell_code << "end\n"
      end
    end.dig(2)
    code << "  end\n"
  end

  if mode == :table || mode == :body
    code << "  xml.tbody do\n"

    body_code = build_elements(element.xpath('xmlns:variable'))
    body_code << build_table_serie(columns, vertical) do |col, cell_code|
      if col.name == 'cell'
        if col.has_attribute?('if')
          cell_code << "xml.td(:class => '#{normalize_name(col)}') do\n"
          cell_code << conditionate(human_value_of(col), col).dig
          cell_code << "end\n"
        else
          # cell_code << "xml.td(#{human_value_of(col)}, :class => '#{normalize_name(col)}')\n"
          cell_code << human_value_of(col, :td, class: normalize_name(col))
        end
      elsif col.name == 'matrix'
        cell_code << "xml.td(:class => 'matrix #{normalize_name(col)}') do\n"
        cell_code << conditionate(build_matrix(col, :body, !vertical), col).dig
        cell_code << "end\n"
      end
    end

    if element.has_attribute?('in')
      code << "    for #{item} in #{element.attr('in')}\n"
      code << body_code.dig(3)
      code << "    end\n"
    else
      code << body_code.dig(2)
    end
    code << "  end\n"
  end

  code << "end\n"
  code
end

#build_property(element) ⇒ Object


82
83
84
85
86
87
88
89
90
# File 'lib/aggeratio/document_fragment.rb', line 82

def build_property(element)
  # return "" if element.attr("level") == "api"
  code = "xml.dl do\n"
  code << "  xml.dd(#{human_name_of(element)})\n"
  # code << "  xml.dt(#{human_value_of(element)})\n"
  code << human_value_of(element, :dt).dig
  code << "end\n"
  code
end

#build_section(element) ⇒ Object


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/aggeratio/document_fragment.rb', line 57

def build_section(element)
  code = ''
  code << "xml.section do\n"
  if title = element.xpath('xmlns:title').first
    type = (element.has_attribute?('name') ? element.attr('name') : element.has_attribute?('for') ? element.attr('for') : nil)
    if type.blank?
      # code << "  xml.h1(#{human_value_of(title)})\n"
      code << human_value_of(title, :h1).dig
    else
      code << "  xml.h1 do\n"
      code << "    xml.span('aggregator_properties.#{type}'.t(default: [:'attributes.#{type}', :'labels.#{type}', :'activerecord.models.#{type}', '#{type.humanize}']))\n"
      code << "    xml.text(' ')\n"
      # code << "    xml.em(#{human_value_of(title)})\n"
      code << human_value_of(title, :em).dig(2)
      code << "  end\n"
      # code << "  xml.h1(:section_x.tl(:section => :#{type}.tl(:default => [:'activerecord.models.#{type}', '#{type.humanize}']), :x => '<em>' + CGI::escapeHTML(#{human_value_of(title)}) + '</em>'))\n" #
    end
  end
  code << "  xml.div(:class => 'section-content') do\n"
  code << build_children_of(element).dig(2)
  code << "  end\n"
  code << "end\n"
  code
end

#build_sections(element) ⇒ Object


46
47
48
49
50
51
52
53
54
55
# File 'lib/aggeratio/document_fragment.rb', line 46

def build_sections(element)
  item  = element.attr('for')
  code  = "for #{item} in #{element.attr('in')}\n"
  code << conditionate(build_section(element), element).dig
  code << "end\n"
  if element.has_attribute?('name')
    code = "xml.section(:class => 'sections') do\n" + code.dig + "end\n"
  end
  code
end

#build_table_serie(elements, vertical = false, &_block) ⇒ Object


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/aggeratio/document_fragment.rb', line 147

def build_table_serie(elements, vertical = false, &_block)
  code = ''
  if vertical
    for element in elements
      code << "xml.tr do\n"
      element_code = ''
      yield(element, element_code)
      code << element_code.dig
      code << "end\n"
    end
  else
    code << "xml.tr do\n"
    for element in elements
      element_code = ''
      yield(element, element_code)
      code << element_code.dig
    end
    code << "end\n"
  end
  code
end