Sha256: 1581dec89c3c97add28e9ace740eb5dd2386dad0eb9ba059feb918f3a52c8431

Contents?: true

Size: 754 Bytes

Versions: 1

Compression:

Stored size: 754 Bytes

Contents

class Musterb::Evaluator
  include ExtractValues

  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

1 entries across 1 versions & 1 rubygems

Version Path
musterb-0.0.5 lib/musterb/evaluator.rb