Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Commit

Permalink
Add match_data, along with a delegated :[] method, to Lita::Response …
Browse files Browse the repository at this point in the history
…which is especially useful for named capture groups from a route
  • Loading branch information
charleseff committed Aug 28, 2013
1 parent 5bf8812 commit d4859e4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/lita/handler.rb
Expand Up @@ -97,7 +97,11 @@ def namespace

# Builds a response object for an incoming message.
def build_response(message, route)
Response.new(message, matches: message.match(route.pattern))
# TODO: this is highly inefficient running two regexp's (3 total if you count the one that matched the route in the first place)
# make faster!
match_data = message.body.match(route.pattern)
matches = message.match(route.pattern)
Response.new(message, matches: matches, match_data: match_data)
end

# Determines whether or not an incoming messages should trigger a route.
Expand Down
8 changes: 7 additions & 1 deletion lib/lita/response.rb
Expand Up @@ -12,6 +12,9 @@ class Response
# @return [Array<String>, Array<Array<String>>] The array of matches.
attr_accessor :matches

# A [MatchData] object from running the message against the route pattern.
attr_accessor :match_data

# @!method args
# @see Lita::Message#args
# @!method reply
Expand All @@ -20,11 +23,14 @@ class Response
# @see Lita::Message#user
def_delegators :message, :args, :reply, :user, :command?

def_delegators :match_data, :[]

# @param message [Lita::Message] The incoming message.
# @param matches [Array<String>, Array<Array<String>>] The Regexp matches.
def initialize(message, matches: nil)
def initialize(message, matches: nil, match_data: nil)
self.message = message
self.matches = matches
self.match_data = match_data
end
end
end

0 comments on commit d4859e4

Please sign in to comment.