Class: Hammock::RouteStep

Inherits:
Object
  • Object
show all
Defined in:
lib/hammock/route_step.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#entityObject (readonly)

Returns the value of attribute entity.



3
4
5
# File 'lib/hammock/route_step.rb', line 3

def entity
  @entity
end

#nodeObject (readonly)

Returns the value of attribute node.



3
4
5
# File 'lib/hammock/route_step.rb', line 3

def node
  @node
end

#parentObject (readonly)

Returns the value of attribute parent.



3
4
5
# File 'lib/hammock/route_step.rb', line 3

def parent
  @parent
end

#routeable_asObject (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

#verbObject (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_verbObject



78
79
80
# File 'lib/hammock/route_step.rb', line 78

def default_verb
  safe? ? :read : :write
end

#delete?Boolean

Returns:

  • (Boolean)


72
# File 'lib/hammock/route_step.rb', line 72

def delete?; :delete == http_method end

#fake_http_methodObject



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

Returns:

  • (Boolean)


69
# File 'lib/hammock/route_step.rb', line 69

def get?;       :get == http_method end

#http_methodObject



60
61
62
63
# File 'lib/hammock/route_step.rb', line 60

def http_method
  raise_unless_setup_while_trying_to 'extract the HTTP method'
  node.send("#{routeable_as}_routes")[verb]
end

#matches?(request) ⇒ Boolean

Returns:

  • (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
  raise_unless_setup_while_trying_to '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

Returns:

  • (Boolean)


70
# File 'lib/hammock/route_step.rb', line 70

def post?;     :post == http_method end

#put?Boolean

Returns:

  • (Boolean)


71
# File 'lib/hammock/route_step.rb', line 71

def put?;       :put == http_method end

#safe?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/hammock/route_step.rb', line 74

def safe?
  get? && !verb.in?(Hammock::Constants::ImpliedUnsafeActions)
end

#setup?Boolean

Returns:

  • (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