Class: Mailman::Router
- Inherits:
-
Object
- Object
- Mailman::Router
- Defined in:
- lib/mailman/router.rb
Overview
The router. Stores routes and uses them to process messages.
Instance Attribute Summary collapse
-
#bounce_block ⇒ Proc
The block to run if a message has bounced.
-
#default_block ⇒ Proc
The block to run if no routes match.
-
#message ⇒ Mail::Message
readonly
The most recently processed message.
-
#params ⇒ Hash
readonly
The params of the most recently processed message.
-
#routes ⇒ Array
The list of routes.
Instance Method Summary collapse
-
#add_route(route) ⇒ Mailman::Route
Adds a route to the router.
-
#initialize ⇒ Router
constructor
A new instance of Router.
-
#route(message) ⇒ Object
Route a message.
Constructor Details
#initialize ⇒ Router
Returns a new instance of Router.
21 22 23 24 |
# File 'lib/mailman/router.rb', line 21 def initialize @routes = [] @params = HashWithIndifferentAccess.new end |
Instance Attribute Details
#bounce_block ⇒ Proc
Returns the block to run if a message has bounced.
9 10 11 |
# File 'lib/mailman/router.rb', line 9 def bounce_block @bounce_block end |
#default_block ⇒ Proc
Returns the block to run if no routes match.
12 13 14 |
# File 'lib/mailman/router.rb', line 12 def default_block @default_block end |
#message ⇒ Mail::Message (readonly)
Returns the most recently processed message.
19 20 21 |
# File 'lib/mailman/router.rb', line 19 def @message end |
#params ⇒ Hash (readonly)
Returns the params of the most recently processed message. Used by route blocks.
16 17 18 |
# File 'lib/mailman/router.rb', line 16 def params @params end |
#routes ⇒ Array
Returns the list of routes.
6 7 8 |
# File 'lib/mailman/router.rb', line 6 def routes @routes end |
Instance Method Details
#add_route(route) ⇒ Mailman::Route
Adds a route to the router.
30 31 32 |
# File 'lib/mailman/router.rb', line 30 def add_route(route) @routes.push(route)[-1] end |
#route(message) ⇒ Object
Route a message. If the route block accepts arguments, it passes any captured params. Named params are available from the params
helper. The message is available from the message
helper.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/mailman/router.rb', line 38 def route() @params.clear @message = result = nil if @bounce_block and .respond_to?(:bounced?) and .bounced? return instance_exec(&@bounce_block) end routes.each do |route| break if result = route.match!() end if result @params.merge!(result[:params]) if !result[:klass].nil? if result[:klass].is_a?(Class) # no instance method specified result[:klass].new.send(:receive, @message, @params) elsif result[:klass].kind_of?(String) # instance method specified klass, method = result[:klass].split('#') klass.camelize.constantize.new.send(method.to_sym, @message, @params) end elsif result[:block].arity > 0 instance_exec(*result[:args], &result[:block]) else instance_exec(&result[:block]) end elsif @default_block instance_exec(&@default_block) end end |