Sha256: 1674e0ad62cf969dd7ef9f4c15804b6689290c4a929f8a4ad0f6251dc3ab1484

Contents?: true

Size: 778 Bytes

Versions: 2

Compression:

Stored size: 778 Bytes

Contents

class Musterb::Evaluator
  include 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

2 entries across 2 versions & 1 rubygems

Version Path
musterb-0.0.7 lib/musterb/evaluator.rb
musterb-0.0.6 lib/musterb/evaluator.rb