Class: MountainBerryFields::Evaluator
- Inherits:
-
Object
- Object
- MountainBerryFields::Evaluator
- Defined in:
- lib/mountain_berry_fields/evaluator.rb
Overview
This class evaluates a block of code that was emitted from the parser. It has a document that the evalutable code can append to. It implements the visible and invisible methods (see the parser for more on these) that MountainBerryFields supports.
It also evaluates tests, and tracks whether they pass or fail (currently only tracking the last failure)
Instance Attribute Summary collapse
-
#to_evaluate ⇒ Object
readonly
Returns the value of attribute to_evaluate.
Class Method Summary collapse
Instance Method Summary collapse
- #context(context_name, options = {}, &block) ⇒ Object
- #contexts ⇒ Object
- #document ⇒ Object
- #evaluate ⇒ Object
- #failure_message ⇒ Object
- #failure_name ⇒ Object
- #in_context(context_name, code) ⇒ Object
-
#initialize(to_evaluate) ⇒ Evaluator
constructor
A new instance of Evaluator.
- #setup ⇒ Object
- #setup_code ⇒ Object
- #test(name, options = {}, &block) ⇒ Object
- #tests ⇒ Object
- #tests_pass? ⇒ Boolean
Constructor Details
#initialize(to_evaluate) ⇒ Evaluator
Returns a new instance of Evaluator.
14 15 16 |
# File 'lib/mountain_berry_fields/evaluator.rb', line 14 def initialize(to_evaluate) @to_evaluate = to_evaluate end |
Instance Attribute Details
#to_evaluate ⇒ Object (readonly)
Returns the value of attribute to_evaluate.
12 13 14 |
# File 'lib/mountain_berry_fields/evaluator.rb', line 12 def to_evaluate @to_evaluate end |
Class Method Details
.invisible_commands ⇒ Object
22 23 24 |
# File 'lib/mountain_berry_fields/evaluator.rb', line 22 def self.invisible_commands [:setup, :context] end |
.visible_commands ⇒ Object
18 19 20 |
# File 'lib/mountain_berry_fields/evaluator.rb', line 18 def self.visible_commands [:test] end |
Instance Method Details
#context(context_name, options = {}, &block) ⇒ Object
30 31 32 33 34 |
# File 'lib/mountain_berry_fields/evaluator.rb', line 30 def context(context_name, ={}, &block) contexts[context_name] = in_context [:context], block.call return if contexts[context_name]['__CODE__'] raise ArgumentError, "Context #{context_name.inspect} does not have a __CODE__ block" end |
#contexts ⇒ Object
36 37 38 |
# File 'lib/mountain_berry_fields/evaluator.rb', line 36 def contexts @contexts ||= Hash.new end |
#document ⇒ Object
79 80 81 82 |
# File 'lib/mountain_berry_fields/evaluator.rb', line 79 def document evaluate @document ||= '' end |
#evaluate ⇒ Object
84 85 86 87 88 |
# File 'lib/mountain_berry_fields/evaluator.rb', line 84 def evaluate return if @evaluated @evaluated = true instance_eval to_evaluate end |
#failure_message ⇒ Object
71 72 73 |
# File 'lib/mountain_berry_fields/evaluator.rb', line 71 def @failing_strategy. end |
#failure_name ⇒ Object
67 68 69 |
# File 'lib/mountain_berry_fields/evaluator.rb', line 67 def failure_name @failing_test.name end |
#in_context(context_name, code) ⇒ Object
56 57 58 59 60 |
# File 'lib/mountain_berry_fields/evaluator.rb', line 56 def in_context(context_name, code) return code unless context_name return contexts[context_name].gsub '__CODE__', code if contexts[context_name] raise NameError, "There is no context #{context_name.inspect}, only #{contexts.keys.inspect}" end |
#setup ⇒ Object
26 27 28 |
# File 'lib/mountain_berry_fields/evaluator.rb', line 26 def setup setup_code << yield end |
#setup_code ⇒ Object
40 41 42 |
# File 'lib/mountain_berry_fields/evaluator.rb', line 40 def setup_code @setup_code ||= '' end |
#test(name, options = {}, &block) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/mountain_berry_fields/evaluator.rb', line 44 def test(name, ={}, &block) code = setup_code + in_context([:context], block.call.to_s) test = Test.new(name, .merge(code: code)) strategy = Test::Strategy.for(test.strategy).new(test.code) unless strategy.pass? @failing_strategy = strategy @failing_test = test end tests << test end |
#tests ⇒ Object
75 76 77 |
# File 'lib/mountain_berry_fields/evaluator.rb', line 75 def tests @tests ||= [] end |
#tests_pass? ⇒ Boolean
62 63 64 65 |
# File 'lib/mountain_berry_fields/evaluator.rb', line 62 def tests_pass? evaluate !@failing_test end |