Class: HumanRoutes::Context
- Inherits:
-
Object
- Object
- HumanRoutes::Context
- Defined in:
- lib/human_routes/context.rb
Instance Attribute Summary collapse
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#named_routes ⇒ Object
readonly
Returns the value of attribute named_routes.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#router ⇒ Object
readonly
Returns the value of attribute router.
Instance Method Summary collapse
- #all ⇒ Object
- #controller_name ⇒ Object
- #create(*args) ⇒ Object
- #get(action, *args) ⇒ Object
-
#initialize(router, controller, options = {}) ⇒ Context
constructor
A new instance of Context.
- #list(*args) ⇒ Object
- #path_name ⇒ Object
- #post(action, *args) ⇒ Object
- #remove(*args) ⇒ Object
- #resource? ⇒ Boolean
- #routes ⇒ Object
- #show(*args) ⇒ Object
- #singular_controller_name ⇒ Object
- #update(*args) ⇒ Object
Constructor Details
#initialize(router, controller, options = {}) ⇒ Context
Returns a new instance of Context.
7 8 9 10 11 12 |
# File 'lib/human_routes/context.rb', line 7 def initialize(router, controller, = {}) @router = router @controller = controller @options = @named_routes = {} end |
Instance Attribute Details
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
5 6 7 |
# File 'lib/human_routes/context.rb', line 5 def controller @controller end |
#named_routes ⇒ Object (readonly)
Returns the value of attribute named_routes.
5 6 7 |
# File 'lib/human_routes/context.rb', line 5 def named_routes @named_routes end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/human_routes/context.rb', line 5 def @options end |
#router ⇒ Object (readonly)
Returns the value of attribute router.
5 6 7 |
# File 'lib/human_routes/context.rb', line 5 def router @router end |
Instance Method Details
#all ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/human_routes/context.rb', line 135 def all create update remove show list unless resource? end |
#controller_name ⇒ Object
14 15 16 |
# File 'lib/human_routes/context.rb', line 14 def controller_name @controller_name ||= .delete(:name) { controller.to_s } end |
#create(*args) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/human_routes/context.rb', line 38 def create(*args) path, name, = extract_route_args( segment: :new, default_name: "new_#{singular_controller_name}", args: args ) match path, { via: :get, controller: controller, action: :new, as: name }.merge() match path, { via: :post, controller: controller, action: :create, as: "" }.merge() end |
#get(action, *args) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/human_routes/context.rb', line 143 def get(action, *args) path, name, = extract_route_args( segment: action, default_name: action.to_s, args: args ) match path, { via: :get, controller: controller, action: action, as: name }.merge() end |
#list(*args) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/human_routes/context.rb', line 104 def list(*args) path, name, = extract_route_args( segment: :list, default_name: controller_name, args: args ) match path, { via: :get, controller: controller, action: :index, as: name }.merge() end |
#path_name ⇒ Object
30 31 32 |
# File 'lib/human_routes/context.rb', line 30 def path_name [:path_name] || controller_name end |
#post(action, *args) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/human_routes/context.rb', line 158 def post(action, *args) path, name, = extract_route_args( segment: action, default_name: action.to_s, args: args ) match path, { via: :post, controller: controller, action: action, as: named_routes[path] == name ? "" : name }.merge() end |
#remove(*args) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/human_routes/context.rb', line 82 def remove(*args) path, name, = extract_route_args( segment: :remove, default_name: "remove_#{singular_controller_name}", args: args ) match path, { via: :get, controller: controller, action: :remove, as: name }.merge() match path, { via: :post, controller: controller, action: :destroy, as: "" }.merge() end |
#resource? ⇒ Boolean
26 27 28 |
# File 'lib/human_routes/context.rb', line 26 def resource? [:resource] || controller_name == singular_controller_name end |
#routes ⇒ Object
34 35 36 |
# File 'lib/human_routes/context.rb', line 34 def routes @routes ||= [] end |
#show(*args) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/human_routes/context.rb', line 119 def show(*args) path, name, = extract_route_args( segment: :show, default_name: singular_controller_name, args: args, bare: true ) match path, { via: :get, controller: controller, action: :show, as: name }.merge() end |
#singular_controller_name ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/human_routes/context.rb', line 18 def singular_controller_name @singular_controller_name ||= if [:resource] controller_name else controller_name.singularize end end |
#update(*args) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/human_routes/context.rb', line 60 def update(*args) path, name, = extract_route_args( segment: :edit, default_name: "edit_#{singular_controller_name}", args: args ) match path, { via: :get, controller: controller, action: :edit, as: name }.merge() match path, { via: :post, controller: controller, action: :update, as: "" }.merge() end |