Class: Teer::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/teer/engine.rb

Instance Method Summary collapse

Constructor Details

#initialize(data, names, template, handlebars, locale, kwargs) ⇒ Engine

Returns a new instance of Engine.



10
11
12
13
14
15
16
17
18
19
# File 'lib/teer/engine.rb', line 10

def initialize(data, names, template, handlebars, locale, kwargs)
  @template = template
  @handlebars = handlebars
  @locale = locale
  @findings = []
  setup_store(data, names)
  @data_store = @store.clone
  kwargs.each { |k, v| @store[k] = v }
  @store.interpolate = interpolate
end

Instance Method Details

#add_to_store(template) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/teer/engine.rb', line 70

def add_to_store(template)
  template.each do |k, v|
    if v.is_a?(String) && k != 'text'
      @store[k] = eval(v, @store.instance_eval { binding })
    elsif !!v == v
      @store[k] = v
    end
  end
end

#dataObject



57
58
59
# File 'lib/teer/engine.rb', line 57

def data
  @data ||= @data_store
end

#findingObject



43
44
45
# File 'lib/teer/engine.rb', line 43

def finding
  @finding ||= parse_template(@template)
end

#findingsObject



47
48
49
50
# File 'lib/teer/engine.rb', line 47

def findings
  @finding ||= parse_template(@template)
  @findings
end

#interpolateObject



66
67
68
# File 'lib/teer/engine.rb', line 66

def interpolate
  @interpolate ||= proc { |string| @handlebars.compile(string).call(@store.to_h) }
end

#parse_template(template) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/teer/engine.rb', line 80

def parse_template(template)
  text = ''
  pre_parsed_text = ''
  add_to_store(template)
  template.each do |k, v|
    new_text = nil
    unparsed_new_text = nil
    if k == 'text' && (!v.nil? && !v.empty?)
      @findings << (new_text = interpolate.call(v[@locale.to_s]))
      unparsed_new_text = v[@locale.to_s]
    elsif v.is_a?(Hash) && eval(k, @store.instance_eval { binding })
      new_text = parse_template(v)
    end
    text += new_text + ' ' if new_text
    pre_parsed_text += unparsed_new_text + ' ' if unparsed_new_text
  end
  @pre_parsed_finding = CGI.unescapeHTML(pre_parsed_text[0...-1]) if !pre_parsed_text.nil? && !pre_parsed_text.empty?
  CGI.unescapeHTML(text[0...-1]) if !text.nil? && !text.empty?
end

#pre_parsed_findingObject



52
53
54
55
# File 'lib/teer/engine.rb', line 52

def pre_parsed_finding
  @finding ||= parse_template(@template)
  @pre_parsed_finding
end

#setup_store(data, names) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/teer/engine.rb', line 21

def setup_store(data, names)
  n_arr = Array(names)
  if n_arr.count == 1
    @store = OpenStruct.new((data[0].keys - n_arr).map do |idx|
      [idx.pluralize, DataStore.new(data.map { |r| [r[idx], r[n_arr[0]]] }, @locale)]
    end.to_h)
  else
    @store = OpenStruct.new.tap do |struct|
      idxs = (data[0].keys - n_arr)
      names.each do |name|
        raise ArgumentError, "column name cannot be plural: #{name}" if name == name.pluralize
        struct[name] = OpenStruct.new(idxs.map do |idx|
          [idx.pluralize, DataStore.new(data.map { |r| [r[idx], r[name]] }, @locale)]
        end.to_h)
      end
    end
  end
  n_arr.each do |name|
    @store[name.pluralize] = VectorStore.new(data.map { |r| r[name] }, @locale)
  end
end

#value_for_store_key(key) ⇒ Object



61
62
63
64
# File 'lib/teer/engine.rb', line 61

def value_for_store_key(key)
  finding if @finding.nil?
  @store[key]
end