Class: Pendragon::Route
- Inherits:
-
Object
- Object
- Pendragon::Route
- Defined in:
- lib/pendragon/route.rb
Overview
A class for defining the route
Direct Known Subclasses
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
The verb should be read from Pendragon::Router.
-
#capture ⇒ Object
The accessors are useful to access from Pendragon::Router.
-
#index ⇒ Object
For compile option.
-
#name ⇒ Object
The accessors are useful to access from Pendragon::Router.
-
#options ⇒ Object
The accessors are useful to access from Pendragon::Router.
-
#order ⇒ Object
The accessors are useful to access from Pendragon::Router.
-
#router ⇒ Object
writeonly
The router will be treated in this class.
-
#verb ⇒ Object
readonly
The verb should be read from Pendragon::Router.
Instance Method Summary collapse
-
#arity ⇒ Fixnum
Returns arity of route block.
-
#call(*args) ⇒ Object
Calls the route block with arguments.
-
#initialize(path, verb, options = {}, &block) ⇒ Route
constructor
Constructs a new instance of Pendragon::Route.
-
#match(pattern) ⇒ MatchData, Nil
Matches a pattern with the route matcher.
-
#matcher ⇒ Pendragon::Matcher
Returns an instance of Pendragon::Matcherthat is associated with the route.
-
#params(pattern, parameters = {}) ⇒ Hash
Matches a pattern with the route matcher, and then returns the route params.
-
#path(*args) ⇒ String
Expands a path using parameters.
-
#to { ... } ⇒ Object
Associates the block with the route, and increments current order of the router.
Constructor Details
#initialize(path, verb, options = {}, &block) ⇒ Route
Constructs a new instance of Pendragon::Route
26 27 28 29 30 31 32 |
# File 'lib/pendragon/route.rb', line 26 def initialize(path, verb, = {}, &block) @block = block if block_given? @path, @verb = path, verb.to_s.upcase @capture = {} @order = 0 () end |
Instance Attribute Details
#block ⇒ Object (readonly)
The verb should be read from Pendragon::Router
17 18 19 |
# File 'lib/pendragon/route.rb', line 17 def block @block end |
#capture ⇒ Object
The accessors are useful to access from Pendragon::Router
11 12 13 |
# File 'lib/pendragon/route.rb', line 11 def capture @capture end |
#index ⇒ Object
For compile option
14 15 16 |
# File 'lib/pendragon/route.rb', line 14 def index @index end |
#name ⇒ Object
The accessors are useful to access from Pendragon::Router
11 12 13 |
# File 'lib/pendragon/route.rb', line 11 def name @name end |
#options ⇒ Object
The accessors are useful to access from Pendragon::Router
11 12 13 |
# File 'lib/pendragon/route.rb', line 11 def @options end |
#order ⇒ Object
The accessors are useful to access from Pendragon::Router
11 12 13 |
# File 'lib/pendragon/route.rb', line 11 def order @order end |
#router=(value) ⇒ Object (writeonly)
The router will be treated in this class.
20 21 22 |
# File 'lib/pendragon/route.rb', line 20 def router=(value) @router = value end |
#verb ⇒ Object (readonly)
The verb should be read from Pendragon::Router
17 18 19 |
# File 'lib/pendragon/route.rb', line 17 def verb @verb end |
Instance Method Details
#arity ⇒ Fixnum
Returns arity of route block
43 44 45 |
# File 'lib/pendragon/route.rb', line 43 def arity @block.arity end |
#call(*args) ⇒ Object
Calls the route block with arguments
49 50 51 |
# File 'lib/pendragon/route.rb', line 49 def call(*args) @block.call(*args) end |
#match(pattern) ⇒ MatchData, Nil
Matches a pattern with the route matcher
56 57 58 |
# File 'lib/pendragon/route.rb', line 56 def match(pattern) matcher.match(pattern) end |
#matcher ⇒ Pendragon::Matcher
Returns an instance of Pendragon::Matcherthat is associated with the route
36 37 38 39 |
# File 'lib/pendragon/route.rb', line 36 def matcher @matcher ||= Matcher.new(@path, :capture => @capture, :default_values => [:default_values]) end |
#params(pattern, parameters = {}) ⇒ Hash
Matches a pattern with the route matcher, and then returns the route params
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/pendragon/route.rb', line 92 def params(pattern, parameters = {}) match_data, params = match(pattern), indifferent_hash if match_data.names.empty? params.merge!(:captures => match_data.captures) unless match_data.captures.empty? params else params_from_matcher = matcher.handler.params(pattern, :captures => match_data) params.merge!(params_from_matcher) if params_from_matcher params.merge(parameters){|key, old, new| old || new } end end |
#path(*args) ⇒ String
Expands a path using parameters
76 77 78 79 80 81 |
# File 'lib/pendragon/route.rb', line 76 def path(*args) return @path if args.empty? params = args[0] params.delete(:captures) matcher.(params) if matcher.mustermann? end |
#to { ... } ⇒ Object
Associates the block with the route, and increments current order of the router
62 63 64 65 66 |
# File 'lib/pendragon/route.rb', line 62 def to(&block) @block = block if block_given? @order = @router.current @router.increment_order! end |