Class: FatTable::Evaluator
- Inherits:
-
Object
- Object
- FatTable::Evaluator
- Defined in:
- lib/fat_table/evaluator.rb
Overview
The Evaluator class provides a class for evaluating Ruby expressions based on variable settings provided at runtime. If the same Evaluator object is used for several successive calls, it can maintain state between calls with instance variables. The call to Evaluator.new can be given a hash of instance variable names and values that will be maintained across all calls to the #evaluate method. In addition, on each evaluate call, a set of /local/ variables can be supplied to provide variables that exist only for the duration of that evaluate call. An optional before and after string can be given to the constructor that will evaluate the given expression before and, respectively, after each call to #evaluate. This provides a way to update values of instance variables for use in subsequent calls to
evaluate.
Instance Method Summary collapse
-
#eval_after_hook(locals: {}) ⇒ Object
Run any after hook in the context of the given local variables.
-
#eval_before_hook(locals: {}) ⇒ Object
Run any before hook in the context of the given local variables.
-
#evaluate(expr = '', locals: {}) ⇒ Object
Return the result of evaluating +expr+ as a Ruby expression in which the instance variables set in Evaluator.new and any local variables set in the Hash parameter +locals+ are available to the expression.
-
#initialize(ivars: {}, before: nil, after: nil) ⇒ Evaluator
constructor
Return a new Evaluator object in which the Hash +ivars+ defines the bindings for instance variables to be available and maintained across all subsequent calls to Evaluator.evaluate.
-
#update_ivars(ivars) ⇒ Object
Set the @group instance variable to the given value.
Constructor Details
#initialize(ivars: {}, before: nil, after: nil) ⇒ Evaluator
Return a new Evaluator object in which the Hash +ivars+ defines the bindings for instance variables to be available and maintained across all subsequent calls to Evaluator.evaluate. The strings +before+ and +after+ are string Ruby expressions that will be evaluated before and after each subsequent call to Evaluator.evaluate.
22 23 24 25 26 |
# File 'lib/fat_table/evaluator.rb', line 22 def initialize(ivars: {}, before: nil, after: nil) @before = before @after = after instance_vars(ivars) end |
Instance Method Details
#eval_after_hook(locals: {}) ⇒ Object
Run any after hook in the context of the given local variables.
41 42 43 44 45 |
# File 'lib/fat_table/evaluator.rb', line 41 def eval_after_hook(locals: {}) return if @after.blank? evaluate(@after, locals: locals) end |
#eval_before_hook(locals: {}) ⇒ Object
Run any before hook in the context of the given local variables.
34 35 36 37 38 |
# File 'lib/fat_table/evaluator.rb', line 34 def eval_before_hook(locals: {}) return if @before.blank? evaluate(@before, locals: locals) end |
#evaluate(expr = '', locals: {}) ⇒ Object
Return the result of evaluating +expr+ as a Ruby expression in which the instance variables set in Evaluator.new and any local variables set in the Hash parameter +locals+ are available to the expression. Certain errors simply return nil as the result. This can happen, for example, when a string gets into an otherwise numeric column because the column is set to tolerant.
53 54 55 56 57 |
# File 'lib/fat_table/evaluator.rb', line 53 def evaluate(expr = '', locals: {}) eval(expr, local_vars(binding, locals)) rescue NoMethodError, TypeError => ex nil end |
#update_ivars(ivars) ⇒ Object
Set the @group instance variable to the given value.
29 30 31 |
# File 'lib/fat_table/evaluator.rb', line 29 def update_ivars(ivars) instance_vars(ivars) end |