Sha256: 5561050b23c1b049f3261b97147d519011ae313a83c7393ce7f0957a300a50b6

Contents?: true

Size: 765 Bytes

Versions: 4

Compression:

Stored size: 765 Bytes

Contents

module Erlectricity
class Matcher
  attr_accessor :condition, :block
  attr_accessor :receiver
  
  def initialize(parent, condition, block)
    self.receiver = parent
    @block = block
    @condition = condition
  end
  
  def run(arg)
    args = get_bound arg
    block.call *args
  end
  
  def matches?(arg)
    if @condition.is_a?(Array)
      return false unless arg.is_a?(Array)
      return false if @condition.length != arg.length
      @condition.zip(arg).all?{|l,r| l.satisfies?(r)}
    else
      @condition.satisfies?(arg)
    end
  end
  

  private
  
  def get_bound(arg)
    if @condition.is_a?(Array) && arg.is_a?(Array)
      @condition.zip(arg).map{|l,r| l.binding_for r}.compact
    else
      @condition.binding_for(arg)
    end
  end
end
end

Version data entries

4 entries across 4 versions & 3 rubygems

Version Path
mojombo-erlectricity-0.2.1 lib/erlectricity/matcher.rb
tmm1-erlectricity-0.2.1 lib/erlectricity/matcher.rb
erlectricity-0.2.0 lib/erlectricity/matcher.rb
erlectricity-0.2.1 lib/erlectricity/matcher.rb