lib/musterb/evaluator.rb in musterb-0.0.3 vs lib/musterb/evaluator.rb in musterb-0.0.4
- old
+ new
@@ -1,20 +1,13 @@
class Musterb::Evaluator
+ include ExtractValues
+
def initialize(context)
@context = context
- end
+ end
- def [](symbol)
- return @context.value if symbol == "."
- final_context = symbol.split(".").inject(@context) do |con, symbol|
- new_context con[symbol], con
- end
- final_context.value
- end
-
- def block(symbol)
- value = self[symbol]
+ def block_if(value)
return if is_falsy? value
case value
when Hash
switch_context(value) { |v| yield v }
@@ -23,12 +16,12 @@
else
switch_context(value) { |v| yield v }
end
end
- def block_unless(symbol)
- yield if is_falsy? self[symbol]
+ def block_unless(value)
+ yield if is_falsy? value
end
private
def is_falsy?(value)
@@ -38,28 +31,13 @@
when Enumerable
value.empty?
else
!value
end
- end
+ end
- def new_context(value, old_context = @context)
- case value
- when Hash
- Musterb::HashExtractor.new(value, old_context)
- when nil
- Musterb::NullExtractor.new(old_context)
- else
- Musterb::ObjectExtractor.new(value, old_context)
- end
- end
-
- def old_context
- @context.parent
- end
-
def switch_context(value)
@context = new_context(value)
yield value
- @context = old_context
+ @context = @context.parent
end
end
\ No newline at end of file