Class: ForestLiana::Utils::ContextVariablesInjector

Inherits:
Object
  • Object
show all
Defined in:
app/services/forest_liana/utils/context_variables_injector.rb

Class Method Summary collapse

Class Method Details

.inject_context_in_filter(filter, context_variables) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/services/forest_liana/utils/context_variables_injector.rb', line 34

def self.inject_context_in_filter(filter, context_variables)
  return nil unless filter

  if filter.key? 'aggregator'
    return {
      'aggregator' => filter['aggregator'],
      'conditions' => filter['conditions'].map { |condition| inject_context_in_filter(condition, context_variables) }
    }
  end

  {
    'field' => filter['field'],
    'operator' => filter['operator'],
    'value' => inject_context_in_value(filter['value'], context_variables)
  }

end

.inject_context_in_value(value, context_variables) ⇒ Object



5
6
7
8
9
# File 'app/services/forest_liana/utils/context_variables_injector.rb', line 5

def self.inject_context_in_value(value, context_variables)
  inject_context_in_value_custom(value) do |context_variable_key|
    context_variables.get_value(context_variable_key).to_s
  end
end

.inject_context_in_value_custom(value) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/services/forest_liana/utils/context_variables_injector.rb', line 11

def self.inject_context_in_value_custom(value)
  return value unless value.is_a?(String)

  value_with_context_variables_injected = value
  regex = /{{([^}]+)}}/
  encountered_variables = []

  while (match = regex.match(value_with_context_variables_injected))
    context_variable_key = match[1]

    unless encountered_variables.include?(context_variable_key)
      value_with_context_variables_injected.gsub!(
        /{{#{context_variable_key}}}/,
        yield(context_variable_key)
      )
    end

    encountered_variables.push(context_variable_key)
  end

  value_with_context_variables_injected
end