Class: Hollerith

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

Defined Under Namespace

Classes: ArgumentDecoder, FunctionRunner, SystemFunctions, ValueGetter

Constant Summary collapse

HOOKS =
%w(
  on_place_order
  on_update_order
)

Instance Method Summary collapse

Constructor Details

#initialize(configuration, main_context = {}) ⇒ Hollerith

Returns a new instance of Hollerith.



8
9
10
11
12
13
# File 'lib/hollerith.rb', line 8

def initialize configuration, main_context = {}
  @configuration = configuration 
  @main_context = main_context
  @user_context = @configuration['configuration'] || {}
  @user_defined_functions = @configuration['custom_functions'] 
end

Instance Method Details

#evaluate_conditional(conditional_statement) ⇒ Object



99
100
101
102
103
# File 'lib/hollerith.rb', line 99

def evaluate_conditional conditional_statement
  return false unless conditional_statement

  evaluate_function(conditional_statement)
end

#evaluate_function(function_definition) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/hollerith.rb', line 105

def evaluate_function function_definition
  return unless function_definition

  runner = Hollerith::FunctionRunner.new(
    function_definition,
    @main_context,
    @user_context,
    @user_defined_functions
  )

  results = runner.evaluate

  if runner.user_context_change
    @user_context.deep_merge!(runner.user_context_change)
  end
  
  return results
end

#evaluate_functions(functions) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/hollerith.rb', line 91

def evaluate_functions functions
  if functions.compact.any?
    functions.compact.each do |function|
      evaluate_function(function)
    end
  end
end

#execute_hook_implementation(definition) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hollerith.rb', line 33

def execute_hook_implementation definition
  if definition['before'] && definition['before']['functions']
    evaluate_functions(definition['before']['functions'])
  end

  case definition['method']
  when 'none'
    evaluate_functions(definition['functions'])
  when 'conditional'
    # TODO
    raise "Not implemented!" 
  when 'for_each'
    before_iteration_functions = definition['for_each']['before_iteration']
    each_iteration_rules = definition['for_each']['each_iteration']
    break_when = definition['for_each']['break_iteration_if']
    next_when = definition['for_each']['next_if']

    evaluate_functions(before_iteration_functions)

    get_iterator(definition['loop']) do
      next if evaluate_conditional(next_when)

      evaluate_functions(each_iteration_rules)        
      break if evaluate_conditional(break_when)
    end
  end

  if definition['finally'] && definition['finally']['functions']
    evaluate_functions(definition['finally']['functions'])
  end
end

#get_iterator(iterator_definition) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/hollerith.rb', line 65

def get_iterator iterator_definition
  # `$$_schools as each_school`
  collection_variable, each_value_variable = iterator_definition.split(' as ')

  get_variable_from_context(collection_variable).each do |each_iteration|
    @user_context[each_value_variable] = each_iteration
    yield
  end
end

#get_variable_from_context(variable) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/hollerith.rb', line 75

def get_variable_from_context variable
  if variable.start_with?('$$_')
    value_to_return = @main_context[variable[3..-1].strip]   

    if value_to_return
      return value_to_return
    else
      raise ArgumentError.new(
        "Variable not found #{value_to_return} in this context"
      )
    end
  else
    raise ArgumentError.new("Invalid variable definition #{variable}")
  end
end

#hook_should_be_run(hook_implementation) ⇒ Object



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

def hook_should_be_run(hook_implementation)
  if hook_implementation.has_key?('run_if')
    evaluate_conditional(hook_implementation['run_if'])      
  elsif hook_implementation.has_key?('run_unless')
    !evaluate_conditional(hook_implementation['run_unless'])      
  else
    true
  end
end

#trigger_hook(hook) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/hollerith.rb', line 15

def trigger_hook hook
  @configuration[hook].each do |each_hook_implementation|
    if hook_should_be_run(each_hook_implementation)
      execute_hook_implementation(each_hook_implementation)
    end
  end
end