Class: RubyRoutes::Router
- Inherits:
-
Object
- Object
- RubyRoutes::Router
- Defined in:
- lib/ruby_routes/router.rb
Instance Attribute Summary collapse
-
#route_set ⇒ Object
readonly
Returns the value of attribute route_set.
Instance Method Summary collapse
- #concern(name, &block) ⇒ Object
-
#concerns(*names, &block) ⇒ Object
Concerns (reusable route groups).
-
#constraints(constraints = {}, &block) ⇒ Object
Route constraints.
-
#defaults(defaults = {}, &block) ⇒ Object
Defaults.
- #delete(path, options = {}) ⇒ Object
-
#get(path, options = {}) ⇒ Object
Basic route definition.
-
#initialize(&block) ⇒ Router
constructor
A new instance of Router.
- #match(path, options = {}) ⇒ Object
-
#mount(app, at: nil) ⇒ Object
Mount other applications.
-
#namespace(name, options = {}, &block) ⇒ Object
Namespace support.
- #patch(path, options = {}) ⇒ Object
- #post(path, options = {}) ⇒ Object
- #put(path, options = {}) ⇒ Object
- #resource(name, options = {}) ⇒ Object
-
#resources(name, options = {}, &block) ⇒ Object
Resources routing (Rails-like).
-
#root(options = {}) ⇒ Object
Root route.
-
#scope(options = {}, &block) ⇒ Object
Scope support.
Constructor Details
Instance Attribute Details
#route_set ⇒ Object (readonly)
Returns the value of attribute route_set.
3 4 5 |
# File 'lib/ruby_routes/router.rb', line 3 def route_set @route_set end |
Instance Method Details
#concern(name, &block) ⇒ Object
134 135 136 |
# File 'lib/ruby_routes/router.rb', line 134 def concern(name, &block) @concerns[name] = block end |
#concerns(*names, &block) ⇒ Object
Concerns (reusable route groups)
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/ruby_routes/router.rb', line 121 def concerns(*names, &block) names.each do |name| concern = @concerns[name] raise "Concern '#{name}' not found" unless concern instance_eval(&concern) end if block_given? instance_eval(&block) end end |
#constraints(constraints = {}, &block) ⇒ Object
Route constraints
139 140 141 142 143 144 145 146 147 |
# File 'lib/ruby_routes/router.rb', line 139 def constraints(constraints = {}, &block) @scope_stack.push({ constraints: constraints }) if block_given? instance_eval(&block) end @scope_stack.pop end |
#defaults(defaults = {}, &block) ⇒ Object
Defaults
150 151 152 153 154 155 156 157 158 |
# File 'lib/ruby_routes/router.rb', line 150 def defaults(defaults = {}, &block) @scope_stack.push({ defaults: defaults }) if block_given? instance_eval(&block) end @scope_stack.pop end |
#delete(path, options = {}) ⇒ Object
29 30 31 |
# File 'lib/ruby_routes/router.rb', line 29 def delete(path, = {}) add_route(path, .merge(via: :delete)) end |
#get(path, options = {}) ⇒ Object
Basic route definition
13 14 15 |
# File 'lib/ruby_routes/router.rb', line 13 def get(path, = {}) add_route(path, .merge(via: :get)) end |
#match(path, options = {}) ⇒ Object
33 34 35 |
# File 'lib/ruby_routes/router.rb', line 33 def match(path, = {}) add_route(path, ) end |
#mount(app, at: nil) ⇒ Object
Mount other applications
161 162 163 164 |
# File 'lib/ruby_routes/router.rb', line 161 def mount(app, at: nil) path = at || "/#{app}" add_route("#{path}/*path", to: app, via: :all) end |
#namespace(name, options = {}, &block) ⇒ Object
Namespace support
94 95 96 97 98 99 100 101 102 |
# File 'lib/ruby_routes/router.rb', line 94 def namespace(name, = {}, &block) @scope_stack.push({ path: "/#{name}", module: name }) if block_given? instance_eval(&block) end @scope_stack.pop end |
#patch(path, options = {}) ⇒ Object
25 26 27 |
# File 'lib/ruby_routes/router.rb', line 25 def patch(path, = {}) add_route(path, .merge(via: :patch)) end |
#post(path, options = {}) ⇒ Object
17 18 19 |
# File 'lib/ruby_routes/router.rb', line 17 def post(path, = {}) add_route(path, .merge(via: :post)) end |
#put(path, options = {}) ⇒ Object
21 22 23 |
# File 'lib/ruby_routes/router.rb', line 21 def put(path, = {}) add_route(path, .merge(via: :put)) end |
#resource(name, options = {}) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ruby_routes/router.rb', line 81 def resource(name, = {}) singular = name.to_s.singularize get "/#{singular}", .merge(to: "#{singular}#show") get "/#{singular}/new", .merge(to: "#{singular}#new") post "/#{singular}", .merge(to: "#{singular}#create") get "/#{singular}/edit", .merge(to: "#{singular}#edit") put "/#{singular}", .merge(to: "#{singular}#update") patch "/#{singular}", .merge(to: "#{singular}#update") delete "/#{singular}", .merge(to: "#{singular}#destroy") end |
#resources(name, options = {}, &block) ⇒ Object
Resources routing (Rails-like)
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 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/ruby_routes/router.rb', line 38 def resources(name, = {}, &block) singular = name.to_s.singularize plural = ([:path] || name.to_s.pluralize) controller = [:controller] || plural # Collection routes get "/#{plural}", .merge(to: "#{controller}#index") get "/#{plural}/new", .merge(to: "#{controller}#new") post "/#{plural}", .merge(to: "#{controller}#create") # Member routes get "/#{plural}/:id", .merge(to: "#{controller}#show") get "/#{plural}/:id/edit", .merge(to: "#{controller}#edit") put "/#{plural}/:id", .merge(to: "#{controller}#update") patch "/#{plural}/:id", .merge(to: "#{controller}#update") delete "/#{plural}/:id", .merge(to: "#{controller}#destroy") # Nested resources if specified if [:nested] nested_name = [:nested] nested_singular = nested_name.to_s.singularize nested_plural = nested_name.to_s.pluralize get "/#{plural}/:id/#{nested_plural}", .merge(to: "#{nested_plural}#index") get "/#{plural}/:id/#{nested_plural}/new", .merge(to: "#{nested_plural}#new") post "/#{plural}/:id/#{nested_plural}", .merge(to: "#{nested_plural}#create") get "/#{plural}/:id/#{nested_plural}/:nested_id", .merge(to: "#{nested_plural}#show") get "/#{plural}/:id/#{nested_plural}/:nested_id/edit", .merge(to: "#{nested_plural}#edit") put "/#{plural}/:id/#{nested_plural}/:nested_id", .merge(to: "#{nested_plural}#update") patch "/#{plural}/:id/#{nested_plural}/:nested_id", .merge(to: "#{nested_plural}#update") delete "/#{plural}/:id/#{nested_plural}/:nested_id", .merge(to: "#{nested_plural}#destroy") end # Handle concerns if block is given if block_given? # Push a scope for nested resources @scope_stack.push({ path: "/#{plural}/:id" }) # Execute the block in the context of this router instance instance_eval(&block) @scope_stack.pop end end |
#root(options = {}) ⇒ Object
Root route
116 117 118 |
# File 'lib/ruby_routes/router.rb', line 116 def root( = {}) add_route("/", .merge(via: :get)) end |
#scope(options = {}, &block) ⇒ Object
Scope support
105 106 107 108 109 110 111 112 113 |
# File 'lib/ruby_routes/router.rb', line 105 def scope( = {}, &block) @scope_stack.push() if block_given? instance_eval(&block) end @scope_stack.pop end |