Class: Hammock::RouteStep
- Inherits:
-
Object
- Object
- Hammock::RouteStep
- Defined in:
- lib/hammock/route_step.rb
Instance Attribute Summary collapse
-
#entity ⇒ Object
readonly
Returns the value of attribute entity.
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#routeable_as ⇒ Object
readonly
Returns the value of attribute routeable_as.
-
#verb ⇒ Object
readonly
Returns the value of attribute verb.
Instance Method Summary collapse
- #default_verb ⇒ Object
- #delete? ⇒ Boolean
- #fake_http_method ⇒ Object
- #for(verb, entity) ⇒ Object
- #get? ⇒ Boolean
- #http_method ⇒ Object
-
#initialize(node) ⇒ RouteStep
constructor
A new instance of RouteStep.
- #matches?(request) ⇒ Boolean
- #path(params = nil) ⇒ Object
- #post? ⇒ Boolean
- #put? ⇒ Boolean
- #safe? ⇒ Boolean
- #setup? ⇒ Boolean
- #within(parent) ⇒ Object
Constructor Details
#initialize(node) ⇒ RouteStep
Returns a new instance of RouteStep.
5 6 7 |
# File 'lib/hammock/route_step.rb', line 5 def initialize node @node = node end |
Instance Attribute Details
#entity ⇒ Object (readonly)
Returns the value of attribute entity.
3 4 5 |
# File 'lib/hammock/route_step.rb', line 3 def entity @entity end |
#node ⇒ Object (readonly)
Returns the value of attribute node.
3 4 5 |
# File 'lib/hammock/route_step.rb', line 3 def node @node end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
3 4 5 |
# File 'lib/hammock/route_step.rb', line 3 def parent @parent end |
#routeable_as ⇒ Object (readonly)
Returns the value of attribute routeable_as.
3 4 5 |
# File 'lib/hammock/route_step.rb', line 3 def routeable_as @routeable_as end |
#verb ⇒ Object (readonly)
Returns the value of attribute verb.
3 4 5 |
# File 'lib/hammock/route_step.rb', line 3 def verb @verb end |
Instance Method Details
#default_verb ⇒ Object
78 79 80 |
# File 'lib/hammock/route_step.rb', line 78 def default_verb safe? ? :read : :write end |
#delete? ⇒ Boolean
72 |
# File 'lib/hammock/route_step.rb', line 72 def delete?; :delete == http_method end |
#fake_http_method ⇒ Object
65 66 67 |
# File 'lib/hammock/route_step.rb', line 65 def fake_http_method http_method.in?(:get, :post) ? http_method : :post end |
#for(verb, entity) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/hammock/route_step.rb', line 9 def for verb, entity routeable_as = node.routeable_as(verb, entity) if !routeable_as raise "The verb '#{verb}' can't be applied to " + (entity.record? ? "#{entity.resource} records" : "the #{entity.resource} resource") + "." elsif (:record == routeable_as) && entity.new_record? raise "The verb '#{verb}' requires a #{entity.resource} with an ID (i.e. not a new record)." elsif (:build == routeable_as) && entity.record? && !entity.new_record? raise "The verb '#{verb}' requires either the #{entity.resource} resource, or a #{entity.resource} without an ID (i.e. a new record)." else @verb, @entity, @routeable_as = verb, entity, routeable_as end self end |
#get? ⇒ Boolean
69 |
# File 'lib/hammock/route_step.rb', line 69 def get?; :get == http_method end |
#http_method ⇒ Object
60 61 62 63 |
# File 'lib/hammock/route_step.rb', line 60 def http_method 'extract the HTTP method' node.send("#{routeable_as}_routes")[verb] end |
#matches?(request) ⇒ Boolean
34 35 36 37 |
# File 'lib/hammock/route_step.rb', line 34 def matches? request (path == request.path) && (http_method == request.method) end |
#path(params = nil) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/hammock/route_step.rb', line 39 def path params = nil 'render a path' buf = '/' buf << node.prefix unless node.prefix.nil? if node.singleton? buf << entity.resource_name.singularize else buf << entity.resource_name buf << '/' + entity.to_param if entity.record? && !entity.new_record? end buf << '/' + verb.to_s unless verb.nil? or implied_verb?(verb) buf = parent.path + buf unless parent.nil? buf << param_str(params) buf end |
#post? ⇒ Boolean
70 |
# File 'lib/hammock/route_step.rb', line 70 def post?; :post == http_method end |
#put? ⇒ Boolean
71 |
# File 'lib/hammock/route_step.rb', line 71 def put?; :put == http_method end |
#safe? ⇒ Boolean
74 75 76 |
# File 'lib/hammock/route_step.rb', line 74 def safe? get? && !verb.in?(Hammock::Constants::ImpliedUnsafeActions) end |
#setup? ⇒ Boolean
30 31 32 |
# File 'lib/hammock/route_step.rb', line 30 def setup? !@entity.nil? end |
#within(parent) ⇒ Object
25 26 27 28 |
# File 'lib/hammock/route_step.rb', line 25 def within parent @parent = parent self end |