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.



298
299
300
301
302
303
304
# File 'lib/patm.rb', line 298

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

Instance Method Details

#apply(obj, _self = nil) ⇒ Object



318
319
320
321
322
323
324
325
326
# File 'lib/patm.rb', line 318

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



314
315
316
# File 'lib/patm.rb', line 314

def else(&block)
  @else = block
end

#on(pat, &block) ⇒ Object



306
307
308
309
310
311
312
# File 'lib/patm.rb', line 306

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