class ResponderBot::MatcherList

Matcher List

Contains the list of available named matchers for a given handler as well as the map from matchers to actions.

Note: Not all matchers need a name. match, is, exactly, and any all create anonymous matchers.

Attributes

actions[RW]
matchers[RW]

Public Class Methods

new(use_defaults = true) click to toggle source
# File lib/responder_bot/matcher_list.rb, line 14
def initialize(use_defaults = true)
  self.actions = {}
  self.matchers = if use_defaults
                    ResponderBot.default_matchers
                  else
                    false
                  end
end

Public Instance Methods

any(&action) click to toggle source

Always matches

# File lib/responder_bot/matcher_list.rb, line 54
def any(&action)
  append_action(true, action)
end
any_of(*matcher_names, &action) click to toggle source

Use any of the named matchers to call this action

# File lib/responder_bot/matcher_list.rb, line 28
def any_of(*matcher_names, &action)
  matcher_names.each do |name|
    append_action(matchers[name], action)
  end
  self
end
define_matcher(matcher_name, matcher) click to toggle source
# File lib/responder_bot/matcher_list.rb, line 58
def define_matcher(matcher_name, matcher)
  matchers[matcher_name] = matcher
end
exactly(string, &action) click to toggle source

Case-sensitive comparison

# File lib/responder_bot/matcher_list.rb, line 48
def exactly(string, &action)
  append_action(string, action)
  self
end
is(string, &action) click to toggle source

Case-insensitive comparison

# File lib/responder_bot/matcher_list.rb, line 42
def is(string, &action)
  append_action(Regexp.new(string, 'i'), action)
  self
end
match(regex, &action) click to toggle source

Provide a regex to use one time

# File lib/responder_bot/matcher_list.rb, line 36
def match(regex, &action)
  append_action(regex, action)
  self
end
matches?(text) click to toggle source
# File lib/responder_bot/matcher_list.rb, line 23
def matches?(text)
  !action_for(text).nil?
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/responder_bot/matcher_list.rb, line 62
def method_missing(method_name, *args, &block)
  if matchers.key?(method_name)
    append_action(matchers[method_name], block)
  else
    super
  end
end
respond_to_missing?(method_name, *) click to toggle source
# File lib/responder_bot/matcher_list.rb, line 70
def respond_to_missing?(method_name, *)
  matchers.key?(method_name)
end

Private Instance Methods

action_for(text) click to toggle source
# File lib/responder_bot/matcher_list.rb, line 80
def action_for(text)
  actions.each_pair do |matcher, action|
    return action if matcher_matches?(matcher, text)
  end
  nil
end
append_action(matcher, action) click to toggle source
# File lib/responder_bot/matcher_list.rb, line 76
def append_action(matcher, action)
  actions[matcher] = action
end
matcher_matches?(matcher, text) click to toggle source
# File lib/responder_bot/matcher_list.rb, line 87
def matcher_matches?(matcher, text)
  case matcher
  when TrueClass then true
  when Regexp then matcher.match?(text)
  when Proc
    matcher.arity.zero? ? matcher.call : matcher.call(text)
  when String then matcher == text
  end
end