Class: Teer::Engine
- Inherits:
-
Object
- Object
- Teer::Engine
- Defined in:
- lib/teer/engine.rb
Instance Method Summary collapse
- #add_to_store(template) ⇒ Object
- #catch_eval(k) ⇒ Object
- #data ⇒ Object
- #finding ⇒ Object
- #findings ⇒ Object
-
#initialize(data, names, template, parser, locale, kwargs) ⇒ Engine
constructor
A new instance of Engine.
- #interpolate ⇒ Object
- #parse_template(template) ⇒ Object
- #pre_parsed_finding ⇒ Object
- #setup_store(data, names) ⇒ Object
- #update_preparsed_finding(text) ⇒ Object
- #value_for_store_key(key) ⇒ Object
Constructor Details
#initialize(data, names, template, parser, 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, parser, locale, kwargs) @template = template @parser = parser @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
71 72 73 74 75 76 77 78 79 |
# File 'lib/teer/engine.rb', line 71 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 |
#catch_eval(k) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/teer/engine.rb', line 107 def catch_eval(k) begin eval(k, @store.instance_eval { binding }) rescue raise ArgumentError, "Could not parse variables in condition: '#{k}'" end end |
#data ⇒ Object
58 59 60 |
# File 'lib/teer/engine.rb', line 58 def data @data ||= @data_store end |
#finding ⇒ Object
44 45 46 |
# File 'lib/teer/engine.rb', line 44 def finding @finding ||= parse_template(@template) end |
#findings ⇒ Object
48 49 50 51 |
# File 'lib/teer/engine.rb', line 48 def findings @finding ||= parse_template(@template) @findings end |
#interpolate ⇒ Object
67 68 69 |
# File 'lib/teer/engine.rb', line 67 def interpolate @interpolate ||= proc { |string| @parser.render(string, @store.to_h) } end |
#parse_template(template) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/teer/engine.rb', line 81 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) && catch_eval(k) new_text = parse_template(v) end text += new_text + ' ' if new_text pre_parsed_text += unparsed_new_text + ' ' if unparsed_new_text end update_preparsed_finding(pre_parsed_text) CGI.unescapeHTML(text[0...-1]) if !text.nil? && !text.empty? end |
#pre_parsed_finding ⇒ Object
53 54 55 56 |
# File 'lib/teer/engine.rb', line 53 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 42 |
# 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 |
#update_preparsed_finding(text) ⇒ Object
101 102 103 104 105 |
# File 'lib/teer/engine.rb', line 101 def update_preparsed_finding(text) if !text.nil? && !text.empty? @pre_parsed_finding = CGI.unescapeHTML(text[0...-1]) end end |
#value_for_store_key(key) ⇒ Object
62 63 64 65 |
# File 'lib/teer/engine.rb', line 62 def value_for_store_key(key) finding if @finding.nil? @store[key] end |