Class: Aggeratio::Base

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

Direct Known Subclasses

DocumentFragment, XML

Constant Summary collapse

LEVELS =
[:api, :human].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aggregator) ⇒ Base


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

def initialize(aggregator)
  @minimum_level = LEVELS.last
  @aggregator = aggregator
  @name = @aggregator.attr('name')
  @parameters = @aggregator.children[0].children.collect do |element|
    Parameter.import(element)
  end
  @root = @aggregator.children[1]
end

Instance Attribute Details

#aggregatorObject (readonly)

Returns the value of attribute aggregator


5
6
7
# File 'lib/aggeratio/base.rb', line 5

def aggregator
  @aggregator
end

#nameObject (readonly)

Returns the value of attribute name


5
6
7
# File 'lib/aggeratio/base.rb', line 5

def name
  @name
end

#parametersObject (readonly)

Returns the value of attribute parameters


5
6
7
# File 'lib/aggeratio/base.rb', line 5

def parameters
  @parameters
end

#rootObject (readonly)

Returns the value of attribute root


5
6
7
# File 'lib/aggeratio/base.rb', line 5

def root
  @root
end

Instance Method Details

#buildObject

Raises:

  • (NotImplementedError)

21
22
23
# File 'lib/aggeratio/base.rb', line 21

def build
  raise NotImplementedError
end

#build_variable(element) ⇒ Object


25
26
27
# File 'lib/aggeratio/base.rb', line 25

def build_variable(element)
  "#{element.attr('name')} = #{value_of(element)}\n"
end

#class_nameObject


17
18
19
# File 'lib/aggeratio/base.rb', line 17

def class_name
  name.camelcase
end

#conditionate(code, element, minimum_level = nil) ⇒ Object


50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/aggeratio/base.rb', line 50

def conditionate(code, element, minimum_level = nil)
  minimum_level ||= @minimum_level || :api
  level = (element.has_attribute?('level') ? element.attr('level').strip.to_sym : @minimum_level)
  return '' if LEVELS.index(minimum_level) > LEVELS.index(level)
  if element.has_attribute?('if')
    test = element.attr('if').strip.gsub(/[\r\n\t]+/, ' ')
    code = "if (#{test})\n" + code.dig + "end\n"
  elsif element.has_attribute?('unless')
    test = element.attr('unless').strip.gsub(/[\r\n\t]+/, ' ')
    code = "unless (#{test})\n" + code.dig + "end\n"
  end
  code
end

#human_name_of(element) ⇒ Object


106
107
108
109
110
111
# File 'lib/aggeratio/base.rb', line 106

def human_name_of(element)
  name = element.attr('value').to_s.downcase # unless name.is_a?(String)
  name = element.attr('name') unless name =~ /^\w+$/
  name = name.to_s.strip.tr('-', '_')
  "'aggregator_properties.#{name}'.t(default: [:'attributes.#{name}', :'labels.#{name}', :'activerecord.models.#{name}', #{name.to_s.humanize.inspect}])"
end

#human_value_of(*args) ⇒ Object


75
76
77
78
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
# File 'lib/aggeratio/base.rb', line 75

def human_value_of(*args)
  options = args.extract_options!
  element = args.shift
  tag = 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
           "xml.text(#{value}.l) unless #{value}.nil?"
         elsif type == :measure
           "xml.text(#{value}.l) unless #{value}.nil?"
         elsif type == :url
           "xml.a(#{value}, href: #{value}) unless #{value}.blank?"
         else
           "xml.text(#{value})"
         end
  if type != :url && element.has_attribute?('of-type')
    of = element.attr('of').to_s
    of_type = element.attr('of-type').to_sym
    if of_type == :record && of.present?
      code = "if #{of}.class < Ekylibre::Record::Base\n" \
             "  xml.a(:href => \"/backend/\#{#{of}.class.table_name}/\#{#{of}.to_param}\") do\n" + code.dig(2) + "  end\n" \
                                                                                                                 "else\n" + code.dig + "end\n"
    end
  end
  unless tag.nil?
    code = "xml.#{tag}(#{options.inspect}) do\n" + code.dig + "end\n"
  end
  code
end

#normalize_name(name) ⇒ Object


64
65
66
67
# File 'lib/aggeratio/base.rb', line 64

def normalize_name(name)
  name = name.attr('name') unless name.is_a?(String)
  name.to_s.strip.tr('_', '-')
end

#parameter_initializationObject


42
43
44
45
46
47
48
# File 'lib/aggeratio/base.rb', line 42

def parameter_initialization
  code = ''
  for parameter in parameters
    code << "#{parameter.name} = @#{parameter.name}\n"
  end
  code
end

#properties(element = nil) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/aggeratio/base.rb', line 29

def properties(element = nil)
  element ||= @root
  array = []
  array << element if %w(section cell property title).include?(element.name)
  for child in element.children
    if child.has_attribute?('name') && !(child.attr('name') =~ /^\w+(\_\w+)*$/)
      raise InvalidDocument, "#{child.name} element has invalid name attribute: #{child.attr('name')}"
    end
    array += properties(child)
  end
  array
end

#queries(options = {}) ⇒ Object

Returns all default XPATH queries


114
115
116
117
118
119
# File 'lib/aggeratio/base.rb', line 114

def queries(options = {})
  @root.xpath('//node()[not(node())]').collect do |leaf|
    next if options[:strict].is_a?(FalseClass) && leaf.has_attribute?('if')
    xpath(leaf)
  end.compact
end

#value_of(element) ⇒ Object


69
70
71
72
73
# File 'lib/aggeratio/base.rb', line 69

def value_of(element)
  value = (element.has_attribute?('value') ? element.attr('value') : element.has_attribute?('name') ? element.attr('name') : 'inspect')
  value = element.attr('of') + '.' + value if element.has_attribute?('of')
  value
end