Class: Rollerskates::Routing::Router
- Defined in:
- lib/rollerskates/routing/router.rb
Instance Method Summary collapse
- #actions(options = {}) ⇒ Object
- #default_actions ⇒ Object
- #draw(&block) ⇒ Object
- #endpoints ⇒ Object
- #method_path_to(route, controller_name) ⇒ Object
- #resources(controller_name, options = {}) ⇒ Object
- #resources_except(*options) ⇒ Object
- #resources_only(*options) ⇒ Object
- #root(location) ⇒ Object
Instance Method Details
#actions(options = {}) ⇒ Object
33 34 35 36 37 |
# File 'lib/rollerskates/routing/router.rb', line 33 def actions( = {}) return resources_only([:only]) if [:only] return resources_except([:except]) if [:except] default_actions end |
#default_actions ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rollerskates/routing/router.rb', line 20 def default_actions [ { action: :index, method: :get }, { action: :create, method: :post }, { action: :new, method: :get, suffix: :new }, { action: :show, method: :get, placeholder: :id }, { action: :edit, method: :get, placeholder: :id, suffix: :edit }, { action: :destroy, method: :delete, placeholder: :id }, { action: :update, method: :put, placeholder: :id }, { action: :update, method: :patch, placeholder: :id } ] end |
#draw(&block) ⇒ Object
4 5 6 |
# File 'lib/rollerskates/routing/router.rb', line 4 def draw(&block) instance_eval(&block) end |
#endpoints ⇒ Object
72 73 74 |
# File 'lib/rollerskates/routing/router.rb', line 72 def endpoints @endpoints ||= Hash.new { |hash, key| hash[key] = [] } end |
#method_path_to(route, controller_name) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/rollerskates/routing/router.rb', line 51 def method_path_to(route, controller_name) suffix = route[:suffix] ? "/#{route[:suffix]}" : "" placeholder = route[:placeholder] ? "/:#{route[:placeholder]}" : "" [ route[:method], "/#{controller_name}#{placeholder}#{suffix}", "#{controller_name}##{route[:action]}" ] end |
#resources(controller_name, options = {}) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/rollerskates/routing/router.rb', line 61 def resources(controller_name, = {}) actions().each do |route| method, path, action = method_path_to(route, controller_name) send(method, path, to: action) end end |
#resources_except(*options) ⇒ Object
45 46 47 48 49 |
# File 'lib/rollerskates/routing/router.rb', line 45 def resources_except(*) default_actions.reject do |action| .include? action[:action] end end |
#resources_only(*options) ⇒ Object
39 40 41 42 43 |
# File 'lib/rollerskates/routing/router.rb', line 39 def resources_only(*) default_actions.select do |action| .include? action[:action] end end |