Sha256: e08d1f4e615f9f8e7cd6f24e1f1367c5c554a2f6ccc6f0774ff0fc6688cfb756

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

class Musterb::Evaluator
  def initialize(context)
    @context = context
  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]
    return if is_falsy? value

    case value
    when Hash
      switch_context(value) { |v| yield v }
    when Enumerable
      value.each { |e| switch_context(e) { |v| yield v } }
    else
      switch_context(value) { |v| yield v }
    end
  end

  def block_unless(symbol)
    yield if is_falsy? self[symbol]
  end

  private

  def is_falsy?(value)
    case value
    when Hash
      false
    when Enumerable
      value.empty?
    else
      !value
    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
musterb-0.0.3 lib/musterb/evaluator.rb
musterb-0.0.2 lib/musterb/evaluator.rb