Sha256: 5bde3ee989ab371eb664019f1e11b717b403f9d664361ede9b56d077af29a027

Contents?: true

Size: 714 Bytes

Versions: 1

Compression:

Stored size: 714 Bytes

Contents

class Musterb::Evaluator
  include ExtractValues

  def initialize(context)
    @context = context
  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.4 lib/musterb/evaluator.rb