Class: Patm::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/patm.rb

Instance Method Summary collapse

Constructor Details

#initialize(compile = true, &block) ⇒ Rule

Returns a new instance of Rule.



433
434
435
436
437
438
439
# File 'lib/patm.rb', line 433

def initialize(compile = true, &block)
  @compile = compile
  # { Pattern => Proc }
  @rules = []
  @else = ->(obj){nil}
  block[self]
end

Instance Method Details

#apply(obj, _self = nil) ⇒ Object



453
454
455
456
457
458
459
460
461
# File 'lib/patm.rb', line 453

def apply(obj, _self = nil)
  match = Match.new
  @rules.each do|(pat, block)|
    if pat.execute(match, obj)
      return block.call(match, _self)
    end
  end
  @else[obj, _self]
end

#else(&block) ⇒ Object



449
450
451
# File 'lib/patm.rb', line 449

def else(&block)
  @else = block
end

#on(pat, &block) ⇒ Object



441
442
443
444
445
446
447
# File 'lib/patm.rb', line 441

def on(pat, &block)
  if @compile
    @rules << [Pattern.build_from(pat).compile, block]
  else
    @rules << [Pattern.build_from(pat), block]
  end
end