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)
-
- (Proc) bounce_block
The block to run if a message has bounced.
-
- (Proc) default_block
The block to run if no routes match.
-
- (Mail::Message) message
readonly
The most recently processed message.
-
- (Hash) params
readonly
The params of the most recently processed message.
-
- (Array) routes
The list of routes.
Instance Method Summary (collapse)
-
- (Mailman::Route) add_route(route)
Adds a route to the router.
-
- (Router) initialize
constructor
A new instance of Router.
-
- (Object) route(message)
Route a message.
Constructor Details
- (Router) initialize
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
- (Proc) bounce_block
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 |
- (Proc) default_block
The block to run if no routes match
12 13 14 |
# File 'lib/mailman/router.rb', line 12 def default_block @default_block end |
- (Mail::Message) message (readonly)
The most recently processed message
19 20 21 |
# File 'lib/mailman/router.rb', line 19 def @message end |
- (Hash) params (readonly)
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 |
- (Array) routes
The list of routes
6 7 8 |
# File 'lib/mailman/router.rb', line 6 def routes @routes end |
Instance Method Details
- (Mailman::Route) add_route(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 |
- (Object) route(message)
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 |