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

Returns a new instance of PatternMatchedFunction.



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

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



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

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

  @default
end

#with(*patterns, &block) ⇒ Object



23
24
25
# File 'lib/method_pattern.rb', line 23

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