Module: Aggeratio

Defined in:
lib/aggeratio.rb,
lib/aggeratio/xml.rb,
lib/aggeratio/base.rb,
lib/aggeratio/parameter.rb,
lib/aggeratio/aggregator.rb,
lib/aggeratio/document_fragment.rb

Defined Under Namespace

Classes: Aggregator, Base, DocumentFragment, InvalidDocument, Parameter, XML

Constant Summary collapse

XMLNS =

autoload :JSON, 'aggeratio/json' autoload :CSV, 'aggeratio/csv' autoload :XSD, 'aggeratio/xsd'

'http://www.ekylibre.org/XML/2013/aggregators'.freeze
NS_SEPARATOR =
'-'.freeze
@@load_path =
[]
@@list =
HashWithIndifferentAccess.new
@@categories =
HashWithIndifferentAccess.new

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Object

Give access to named aggregators


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

def [](name)
  @@list[name]
end

.build(element) ⇒ Object


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/aggeratio.rb', line 130

def build(element)
  agg = Base.new(element)
  name = agg.name

  code = "class #{agg.class_name} < Aggregator\n"

  parameters = agg.parameters
  root = agg.root

  code << "  class << self\n"

  code << "    def parameters\n"
  code << '      [' + agg.parameters.collect do |parameter|
    "Parameter.new(#{parameter.name.inspect}, :#{parameter.type}, #{parameter.options.inspect}).freeze"
  end.join(', ') + "].freeze\n"
  code << "    end\n"

  code << "    def aggregator_name\n"
  code << "      '#{name}'\n"
  code << "    end\n"
  code << "    alias :id :aggregator_name\n"

  code << "    def category\n"
  code << "      '#{element.attr('category')}'\n"
  code << "    end\n"

  code << "  end\n"

  params = 'options'
  code << "  def initialize(#{params} = nil)\n"
  code << "    #{params} ||= {}\n"
  for p in parameters
    if p.record_list?
      # campaigns
      code << "    if #{params}['#{p.name}'].is_a?(String)\n"
      code << "      @#{p.name} = #{p.foreign_class.name}.where(:id => #{params}['#{p.name}'].to_s.split(/[\\,\\s]+/))\n"
      code << "    elsif #{params}['#{p.name}'].is_a?(Hash)\n"
      code << "      @#{p.name} = #{p.foreign_class.name}.where(:id => #{params}['#{p.name}'].select{|k,v| !v.to_i.zero?}.map(&:first))\n"
      code << "    elsif #{params}['#{p.name}'].is_a?(Array)\n"
      code << "      @#{p.name} = #{p.foreign_class.name}.where(:id => #{params}['#{p.name}'])\n"
      # # campaign_ids
      # name = p.name.singularize + "_ids"
      # code << "    elsif #{params}['#{name}']\n"
      # code << "      @#{p.name} = #{p.foreign_class.name}.where(:id => #{params}['#{name}'].to_s.split(/[\\,\\s]+/))\n"
      code << "    else\n"
      code << "      @#{p.name} = #{p.foreign_class.name}.#{p.default}\n"
      code << "    end\n"
    elsif p.record?
      # campaign
      code << "    if #{params}['#{p.name}']\n"
      code << "      @#{p.name} = #{p.foreign_class.name}.find(#{params}['#{p.name}'].to_i)\n"
      # # campaign_id
      # name = p.name + "_id"
      # code << "    elsif #{params}['#{name}']\n"
      # code << "      @#{p.name} = #{p.foreign_class.name}.find(#{params}['#{name}'].to_i)\n"
      code << "    else\n"
      code << "      @#{p.name} = #{p.foreign_class.name}.#{p.default}\n"
      code << "    end\n"
    elsif p.decimal?
      code << "    @#{p.name} = (#{params}['#{p.name}'] ? #{params}['#{name}'].to_f : #{p.default.to_f.inspect})\n"
    elsif p.integer?
      code << "    @#{p.name} = (#{params}['#{p.name}'] ? #{params}['#{name}'].to_i : #{p.default.to_i.inspect})\n"
    elsif p.date?
      code << "    @#{p.name} = (#{params}['#{p.name}'] || #{p.default.to_s.inspect}).to_date\n"
    else
      code << "    @#{p.name} = (#{params}['#{p.name}'] ? #{params}['#{name}'].to_s : #{p.default.inspect})\n"
    end
  end
  code << "  end\n"

  # code << "  def to_json\n"
  # code << JSON.new(element).build.gsub(/^/, '    ')
  # code << "  end\n"

  code << "  def to_xml(options = {})\n"
  code << XML.new(element).build.gsub(/^/, '    ')
  code << "  end\n"

  code << "  def to_html_fragment\n"
  code << DocumentFragment.new(element).build.gsub(/^/, '    ')
  code << "  end\n"

  code << "end\n"

  if true # Rails.env.development?
    f = Rails.root.join('tmp', 'code', 'aggregators', "#{agg.name}.rb")
    FileUtils.mkdir_p(f.dirname)
    File.write(f, code)
  end
  # code.split(/\n/).each_with_index{|l,i| puts (i+1).to_s.rjust(4)+": "+l}

  class_eval(code)

  "Aggeratio::#{agg.class_name}".constantize
end

.clean(element) ⇒ Object


79
80
81
82
83
84
85
86
87
88
89
90
91
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
# File 'lib/aggeratio.rb', line 79

def clean(element)
  # Merge <within>s
  for within in element.xpath('//xmlns:within')
    name = within.attr('name')
    of = within.attr('of')
    of_type = within.attr('of-type')
    of ||= name
    for child in within.children
      unless child.has_attribute?('value')
        child['value'] = child.attr('name').to_s
      end
      unless of.blank?
        child['of'] = if child.has_attribute?('of')
                        of + '.' + child.attr('of').to_s
                      else
                        of
                      end
      end
      unless child.has_attribute?('of-type') || of_type.blank?
        child['of-type'] = of_type
      end
      if name
        child['name'] = (child.has_attribute?('name') ? name + '-' + child.attr('name').to_s : name)
      end
      within.add_previous_sibling(child)
    end
    within.remove
  end

  # Flatten <section> and <sections>
  for section in element.xpath('//*[self::xmlns:section or self::xmlns:sections]')
    of = section.attr('of')
    of_type = section.attr('of-type')
    section['if'] = of if section.name == 'section' && !of.blank?
    for child in section.children
      unless of.blank?
        child['of'] = if child.has_attribute?('of')
                        of + '.' + child.attr('of').to_s
                      else
                        of
                      end
      end
      unless child.has_attribute?('of-type') || of_type.blank?
        child['of-type'] = of_type
      end
    end
  end
  # element.to_xml.split(/\n/).each_with_index{|l,i| puts (i+1).to_s.rjust(4)+": "+l}
  element
end

.each(&block) ⇒ Object


25
26
27
28
29
30
31
# File 'lib/aggeratio.rb', line 25

def each(&block)
  if block.arity == 2
    @@list.each(&block)
  else
    @@list.values.each(&block)
  end
end

.each_xml_aggregator(&_block) ⇒ Object

Browse all aggregator elements in XML files


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/aggeratio.rb', line 57

def each_xml_aggregator(&_block)
  @@load_path.sort.each do |file_path|
    f = File.open(file_path, 'rb')
    document = Nokogiri::XML(f) do |config|
      config.strict.nonet.noblanks.noent
    end
    f.close
    # TODO: Add a better syntax check
    if document.root.namespace.href.to_s == XMLNS
      document.root.xpath('xmlns:aggregator').each do |element|
        yield clean(element)
      end
    else
      Rails.logger.info("File #{file_path} is not a aggregator as defined by #{XMLNS}")
    end
  end
end

.loadObject

Load all aggregator and build Aggregator children


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

def load
  each_xml_aggregator do |element|
    aggregator = build(element)
    @@list[aggregator.aggregator_name] = aggregator
    cat = aggregator.category
    @@categories[cat] ||= []
    @@categories[cat] << aggregator.id unless @@categories[cat].include?(aggregator.id)
  end
  Rails.logger.info 'Loaded aggregators: ' + Aggeratio.names.to_sentence
  true
end

.namesObject

Returns the names of the aggregators


34
35
36
# File 'lib/aggeratio.rb', line 34

def names
  @@list.keys
end

.of_category(cat) ⇒ Object


75
76
77
# File 'lib/aggeratio.rb', line 75

def of_category(cat)
  (@@categories[cat] || []).collect { |a| Aggeratio[a] }
end