Module: Strelka::App::Routing::ClassMethods
- Defined in:
- lib/strelka/app/routing.rb
Overview
Class methods to add to classes with routing.
Instance Attribute Summary collapse
-
#routerclass ⇒ Object
The name of the routing strategy class to use.
-
#routes ⇒ Object
readonly
The list of routes to pass to the Router when the application is created.
Instance Method Summary collapse
-
#add_route(verb, pattern, options = {}, &block) ⇒ Object
Define a route method for the specified
verbandpatternwith the specifiedoptions, and theblockas its body. -
#connect(options = {}, &block) ⇒ Object
Define a route for the CONNECT verb.
-
#delete(pattern = '', options = {}, &block) ⇒ Object
Define a route for the DELETE verb and the given
pattern. -
#get(pattern = '', options = {}, &block) ⇒ Object
Define a route for the GET verb and the given
pattern. -
#has_route?(http_verb, path) ⇒ Boolean
Returns
trueif the app has a route for the specifiedverbandpath. -
#inherited(subclass) ⇒ Object
Inheritance hook – inheriting classes inherit their parents’ routes table.
-
#make_route_name(pattern) ⇒ Object
Generate a name based on the parts of the given
pattern. -
#options(pattern = '', options = {}, &block) ⇒ Object
Define a route for the OPTIONS verb and the given
pattern. -
#post(pattern = '', options = {}, &block) ⇒ Object
Define a route for the POST verb and the given
pattern. -
#put(pattern = '', options = {}, &block) ⇒ Object
Define a route for the PUT verb and the given
pattern. -
#route_methods ⇒ Object
Return a Hash of the methods defined by routes.
-
#router(newclass = nil) ⇒ Object
Get/set the router class to use for mapping requests to handlers to +newclass.
-
#split_route_pattern(pattern) ⇒ Object
Split the given
patterninto its path components and. -
#trace(pattern = '', options = {}, &block) ⇒ Object
Define a route for the TRACE verb and the given
pattern.
Instance Attribute Details
#routerclass ⇒ Object
The name of the routing strategy class to use
120 121 122 |
# File 'lib/strelka/app/routing.rb', line 120 def routerclass @routerclass end |
#routes ⇒ Object (readonly)
The list of routes to pass to the Router when the application is created
116 117 118 |
# File 'lib/strelka/app/routing.rb', line 116 def routes @routes end |
Instance Method Details
#add_route(verb, pattern, options = {}, &block) ⇒ Object
Define a route method for the specified verb and pattern with the specified options, and the block as its body.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/strelka/app/routing.rb', line 194 def add_route( verb, pattern, ={}, &block ) # Start the name of the route method with the HTTP verb, then split the # route pattern into its components methodparts = [ verb.upcase ] patternparts = self.split_route_pattern( pattern ) self.log.debug "Split pattern %p into parts: %p" % [ pattern, patternparts ] # Make a method name from the directories and the named captures of the patterns # in the route patternparts.each do |part| if part.respond_to?( :names ) methodparts << make_route_name( part ) else methodparts << part end end self.log.debug " route methodname parts are: %p" % [ methodparts ] methodname = methodparts.join( '_' ) # Define the method using the block from the route as its body self.log.debug " adding route method %p for %p route: %p" % [ methodname, verb, block ] define_method( methodname, &block ) # Remove any existing route for the same verb, patternparts, and options # (support for overriding inherited routes) self.routes.delete_if do |r| r[0] == verb && r[1] == patternparts && r[2][:options] == end # Now add all the parts to the routes array for the router created by # instances self.routes << [ verb, patternparts, {:action => self.instance_method(methodname), :options => } ] end |
#connect(options = {}, &block) ⇒ Object
Define a route for the CONNECT verb.
176 177 178 |
# File 'lib/strelka/app/routing.rb', line 176 def connect( ={}, &block ) self.add_route( :CONNECT, '', , &block ) end |
#delete(pattern = '', options = {}, &block) ⇒ Object
Define a route for the DELETE verb and the given pattern.
164 165 166 |
# File 'lib/strelka/app/routing.rb', line 164 def delete( pattern='', ={}, &block ) self.add_route( :DELETE, pattern, , &block ) end |
#get(pattern = '', options = {}, &block) ⇒ Object
Define a route for the GET verb and the given pattern.
146 147 148 |
# File 'lib/strelka/app/routing.rb', line 146 def get( pattern='', ={}, &block ) self.add_route( :GET, pattern, , &block ) end |
#has_route?(http_verb, path) ⇒ Boolean
Returns true if the app has a route for the specified verb and path.
131 132 133 134 |
# File 'lib/strelka/app/routing.rb', line 131 def has_route?( http_verb, path ) path_pattern = self.split_route_pattern( path ) self.routes.find {|tuple| tuple[0] == http_verb && tuple[1] == path_pattern } end |
#inherited(subclass) ⇒ Object
Inheritance hook – inheriting classes inherit their parents’ routes table.
269 270 271 272 273 |
# File 'lib/strelka/app/routing.rb', line 269 def inherited( subclass ) super subclass.instance_variable_set( :@routerclass, self.routerclass ) subclass.instance_variable_set( :@routes, self.routes.dup ) end |
#make_route_name(pattern) ⇒ Object
Generate a name based on the parts of the given pattern.
255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/strelka/app/routing.rb', line 255 def make_route_name( pattern ) name = '_re_' if pattern.names.empty? name += ("%s%#x" % [ pattern.class.name, pattern.object_id * 2 ]) else name += pattern.names.join( '_' ) end return name end |
#options(pattern = '', options = {}, &block) ⇒ Object
Define a route for the OPTIONS verb and the given pattern.
140 141 142 |
# File 'lib/strelka/app/routing.rb', line 140 def ( pattern='', ={}, &block ) self.add_route( :OPTIONS, pattern, , &block ) end |
#post(pattern = '', options = {}, &block) ⇒ Object
Define a route for the POST verb and the given pattern.
152 153 154 |
# File 'lib/strelka/app/routing.rb', line 152 def post( pattern='', ={}, &block ) self.add_route( :POST, pattern, , &block ) end |
#put(pattern = '', options = {}, &block) ⇒ Object
Define a route for the PUT verb and the given pattern.
158 159 160 |
# File 'lib/strelka/app/routing.rb', line 158 def put( pattern='', ={}, &block ) self.add_route( :PUT, pattern, , &block ) end |
#route_methods ⇒ Object
Return a Hash of the methods defined by routes.
125 126 127 |
# File 'lib/strelka/app/routing.rb', line 125 def route_methods return self.instance_methods.grep( /^#{HTTP::RFC2616_VERB_REGEX}(_|$)/ ) end |
#router(newclass = nil) ⇒ Object
Get/set the router class to use for mapping requests to handlers to +newclass.
182 183 184 185 186 187 188 189 |
# File 'lib/strelka/app/routing.rb', line 182 def router( newclass=nil ) if newclass self.log.info "%p router class set to: %p" % [ self, newclass ] self.routerclass = newclass end return self.routerclass end |
#split_route_pattern(pattern) ⇒ Object
Split the given pattern into its path components and
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/strelka/app/routing.rb', line 235 def split_route_pattern( pattern ) return [pattern] if pattern.is_a?( Regexp ) return pattern.split( '/' ).reject( &:empty? ).collect do |component| if component.start_with?( ':' ) self.log.debug "translating parameter component %p to a regexp" % [component] raise ScriptError, "parameter-based routing not supported without a 'parameters' plugin" unless self.respond_to?( :paramvalidator ) component = component.slice( 1..-1 ) self.paramvalidator.constraint_regexp_for( component ) else component end end end |
#trace(pattern = '', options = {}, &block) ⇒ Object
Define a route for the TRACE verb and the given pattern.
170 171 172 |
# File 'lib/strelka/app/routing.rb', line 170 def trace( pattern='', ={}, &block ) self.add_route( :TRACE, pattern, , &block ) end |