Class: MethodPattern::PatternMatchedFunction

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

Defined Under Namespace

Classes: Pattern

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ PatternMatchedFunction



16
17
18
19
20
21
22
23
# File 'lib/method_pattern.rb', line 16

def initialize name
  @name = name
  @patterns = []
  @default = proc do |*args|
    raise ArgumentError,
      "method #{self.class.inspect}##{name} does not know how to handle arguments: #{args.map(&:inspect).join(', ')}"
  end
end

Instance Method Details

#match(args) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/method_pattern.rb', line 29

def match args
  @patterns.each do |pattern|
    if pattern.match? args
      return pattern.block
    end
  end

  @default
end

#with(*patterns, &block) ⇒ Object



25
26
27
# File 'lib/method_pattern.rb', line 25

def with *patterns, &block
  @patterns << Pattern.new(patterns, block)
end