Class: Hammock::RouteNode

Inherits:
ActionController::Resources::Resource
  • Object
show all
Defined in:
lib/hammock/route_node.rb

Constant Summary collapse

DefaultRecordVerbs =
{
  :show => :get,
  :edit => :get,
  :update => :put,
  :destroy => :delete
}.freeze
DefaultResourceVerbs =
{
  :index => :get
}.freeze
DefaultBuildVerbs =
{
  :new => :get,
  :create => :post
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity = nil, options = {}) ⇒ RouteNode

Returns a new instance of RouteNode.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hammock/route_node.rb', line 20

def initialize entity = nil, options = {}
  @parent = options[:parent]
  @children = {}
  @options = options[:name_prefix]
  @singleton = options[:singleton]
  unless root?
    @mdl = entity if entity.is_a?(Symbol)
    @routing_parent = determine_routing_parent
    define_routes options
  end
end

Instance Attribute Details

#all_routesObject (readonly)

Returns the value of attribute all_routes.



18
19
20
# File 'lib/hammock/route_node.rb', line 18

def all_routes
  @all_routes
end

#build_routesObject (readonly)

Returns the value of attribute build_routes.



18
19
20
# File 'lib/hammock/route_node.rb', line 18

def build_routes
  @build_routes
end

#childrenObject (readonly)

Returns the value of attribute children.



18
19
20
# File 'lib/hammock/route_node.rb', line 18

def children
  @children
end

#mdlObject (readonly)

Returns the value of attribute mdl.



18
19
20
# File 'lib/hammock/route_node.rb', line 18

def mdl
  @mdl
end

#parentObject (readonly)

Returns the value of attribute parent.



18
19
20
# File 'lib/hammock/route_node.rb', line 18

def parent
  @parent
end

#prefixObject (readonly)

Returns the value of attribute prefix.



18
19
20
# File 'lib/hammock/route_node.rb', line 18

def prefix
  @prefix
end

#record_routesObject (readonly)

Returns the value of attribute record_routes.



18
19
20
# File 'lib/hammock/route_node.rb', line 18

def record_routes
  @record_routes
end

#resourceObject (readonly)

Returns the value of attribute resource.



18
19
20
# File 'lib/hammock/route_node.rb', line 18

def resource
  @resource
end

#resource_routesObject (readonly)

Returns the value of attribute resource_routes.



18
19
20
# File 'lib/hammock/route_node.rb', line 18

def resource_routes
  @resource_routes
end

#routing_parentObject (readonly)

Returns the value of attribute routing_parent.



18
19
20
# File 'lib/hammock/route_node.rb', line 18

def routing_parent
  @routing_parent
end

Instance Method Details

#add(entity, options, path_steps = nil) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/hammock/route_node.rb', line 103

def add entity, options, path_steps = nil
  if path_steps.nil?
    path_steps = (options[:path_prefix] || '').split('/').squash.discard {|i| i.starts_with? ':' }.map(&:to_sym)
    add entity, options, path_steps
  elsif path_steps.empty?
    add_child entity, options
  else
    children[path_steps.shift].add entity, options, path_steps
  end
end

#add_singleton(entity, options, path_steps = nil) ⇒ Object



99
100
101
# File 'lib/hammock/route_node.rb', line 99

def add_singleton entity, options, path_steps = nil
  add entity, options.merge(:singleton => true), path_steps
end

#ancestryObject



54
55
56
# File 'lib/hammock/route_node.rb', line 54

def ancestry
  root? ? [] : parent.ancestry.push(self)
end

#base_for(resources) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/hammock/route_node.rb', line 71

def base_for resources
  if resources.empty?
    if root?
      raise "You have to supply at least one valid resource to find its routing base."
    else
      self
    end
  else
    children.values.pick {|child|
      child.base_for resources.discard(child.mdl) if resources.include?(child.mdl)
    }
  end
end

#default_verb_for(verb) ⇒ Object



37
38
39
# File 'lib/hammock/route_node.rb', line 37

def default_verb_for verb
  safe_for?(verb.to_sym) ? :read : :write
end

#for(verb, entities, options) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hammock/route_node.rb', line 58

def for verb, entities, options
  raise "Hammock::RouteNode#for requires a verb (as a symbol) as its first argument." unless verb.is_a?(Symbol)
  raise "Hammock::RouteNode#for requires an Array of at least one record or resource." if entities.empty? || !entities.is_a?(Array)

  entity = entities.shift

  if entities.empty?
    steps_for verb, entity
  else
    children[entity.resource_sym].for(verb, entities, options).within steps_for(nil, entity)
  end
end

#nesting_scope_list_for(params) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/hammock/route_node.rb', line 85

def nesting_scope_list_for params
  if root?
    [ ]
  else
    parent.nesting_scope_list_for(params).push nesting_scope_segment_for(params)
  end
end

#nesting_scope_segment_for(params) ⇒ Object



93
94
95
96
97
# File 'lib/hammock/route_node.rb', line 93

def nesting_scope_segment_for params
  raise "The root of the route map isn't associated with a resource." if root?
  value = params.delete resource.param_key
  eval "resource.select {|r| r.#{resource.routing_attribute} == value }"
end

#root?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/hammock/route_node.rb', line 41

def root?
  parent.nil?
end

#routeable_as(verb, entity) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/hammock/route_node.rb', line 114

def routeable_as verb, entity
  if entity.record? && record_routes[verb || :show]
    :record
  elsif entity.resource? && resource_routes[verb || :index]
    :resource
  elsif !verb.nil? && build_routes[verb]
    :build
  end
end

#safe_for?(verb) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/hammock/route_node.rb', line 45

def safe_for? verb
  verb = verb.to_sym
  (:get == all_routes[verb]) && !verb.in?(Hammock::Constants::ImpliedUnsafeActions)
end

#singleton?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/hammock/route_node.rb', line 50

def singleton?
  @singleton
end