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.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#routeable_as ⇒ Object
readonly
Returns the value of attribute routeable_as.
-
#verb ⇒ Object
readonly
Returns the value of attribute verb.
Instance Method Summary collapse
- #delete? ⇒ Boolean
- #fake_http_method ⇒ Object
- #for(verb, entity) ⇒ Object
- #get? ⇒ Boolean
- #http_method ⇒ Object
-
#initialize(resource) ⇒ RouteStep
constructor
A new instance of RouteStep.
- #path(params = nil) ⇒ Object
- #post? ⇒ Boolean
- #put? ⇒ Boolean
- #safe? ⇒ Boolean
- #setup? ⇒ Boolean
- #within(parent) ⇒ Object
Constructor Details
#initialize(resource) ⇒ RouteStep
Returns a new instance of RouteStep.
5 6 7 |
# File 'lib/hammock/route_step.rb', line 5 def initialize resource @resource = resource 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 |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
3 4 5 |
# File 'lib/hammock/route_step.rb', line 3 def parent @parent end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
3 4 5 |
# File 'lib/hammock/route_step.rb', line 3 def resource @resource 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
#delete? ⇒ Boolean
60 |
# File 'lib/hammock/route_step.rb', line 60 def delete?; :delete == http_method end |
#fake_http_method ⇒ Object
53 54 55 |
# File 'lib/hammock/route_step.rb', line 53 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 = resource.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
57 |
# File 'lib/hammock/route_step.rb', line 57 def get?; :get == http_method end |
#http_method ⇒ Object
48 49 50 51 |
# File 'lib/hammock/route_step.rb', line 48 def http_method 'extract the HTTP method' resource.send("#{routeable_as}_routes")[verb] end |
#path(params = nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/hammock/route_step.rb', line 34 def path params = nil 'render a path' buf = '/' buf << entity.resource_name buf << '/' + entity.to_param if entity.record? && !entity.new_record? 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
58 |
# File 'lib/hammock/route_step.rb', line 58 def post?; :post == http_method end |
#put? ⇒ Boolean
59 |
# File 'lib/hammock/route_step.rb', line 59 def put?; :put == http_method end |
#safe? ⇒ Boolean
62 63 64 |
# File 'lib/hammock/route_step.rb', line 62 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 |