Sha256: f4e7c684e9a2d6aa8085c2eba5d12d058b7fff6221475bd896ecff10468912ac

Contents?: true

Size: 787 Bytes

Versions: 6

Compression:

Stored size: 787 Bytes

Contents

class Musterb::Evaluator
  include Musterb::ExtractValues

  attr_reader :context

  def initialize(context)
    @context = context
  end  

  def current
    @context.value
  end

  def block_if(value)
    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(value)
    yield if is_falsy? value
  end

  private

  def is_falsy?(value)
    case value
    when Hash
      false
    when Enumerable
      value.empty?
    else
      !value
    end
  end  

  def switch_context(value)
    @context = new_context(value)
    yield value
    @context = @context.parent
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
musterb-1.0.1 lib/musterb/evaluator.rb
musterb-1.0.0 lib/musterb/evaluator.rb
musterb-0.2.0 lib/musterb/evaluator.rb
musterb-0.1.2 lib/musterb/evaluator.rb
musterb-0.1.1 lib/musterb/evaluator.rb
musterb-0.1.0 lib/musterb/evaluator.rb