Class: Hammock::RouteNode
- Inherits:
-
ActionController::Resources::Resource
- Object
- ActionController::Resources::Resource
- Hammock::RouteNode
- 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
-
#all_routes ⇒ Object
readonly
Returns the value of attribute all_routes.
-
#build_routes ⇒ Object
readonly
Returns the value of attribute build_routes.
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#mdl ⇒ Object
readonly
Returns the value of attribute mdl.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#record_routes ⇒ Object
readonly
Returns the value of attribute record_routes.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#resource_routes ⇒ Object
readonly
Returns the value of attribute resource_routes.
-
#routing_parent ⇒ Object
readonly
Returns the value of attribute routing_parent.
Instance Method Summary collapse
- #add(entity, options, path_steps = nil) ⇒ Object
- #add_singleton(entity, options, path_steps = nil) ⇒ Object
- #ancestry ⇒ Object
- #base_for(resources) ⇒ Object
- #default_verb_for(verb) ⇒ Object
- #for(verb, entities, options) ⇒ Object
-
#initialize(entity = nil, options = {}) ⇒ RouteNode
constructor
A new instance of RouteNode.
- #nesting_scope_list_for(params) ⇒ Object
- #nesting_scope_segment_for(params) ⇒ Object
- #root? ⇒ Boolean
- #routeable_as(verb, entity) ⇒ Object
- #safe_for?(verb) ⇒ Boolean
- #singleton? ⇒ Boolean
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, = {} @parent = [:parent] @children = {} @options = [:name_prefix] @singleton = [:singleton] unless root? @mdl = entity if entity.is_a?(Symbol) @routing_parent = determine_routing_parent define_routes end end |
Instance Attribute Details
#all_routes ⇒ Object (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_routes ⇒ Object (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 |
#children ⇒ Object (readonly)
Returns the value of attribute children.
18 19 20 |
# File 'lib/hammock/route_node.rb', line 18 def children @children end |
#mdl ⇒ Object (readonly)
Returns the value of attribute mdl.
18 19 20 |
# File 'lib/hammock/route_node.rb', line 18 def mdl @mdl end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
18 19 20 |
# File 'lib/hammock/route_node.rb', line 18 def parent @parent end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
18 19 20 |
# File 'lib/hammock/route_node.rb', line 18 def prefix @prefix end |
#record_routes ⇒ Object (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 |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
18 19 20 |
# File 'lib/hammock/route_node.rb', line 18 def resource @resource end |
#resource_routes ⇒ Object (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_parent ⇒ Object (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, , path_steps = nil if path_steps.nil? path_steps = ([:path_prefix] || '').split('/').squash.discard {|i| i.starts_with? ':' }.map(&:to_sym) add entity, , path_steps elsif path_steps.empty? add_child entity, else children[path_steps.shift].add entity, , 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, , path_steps = nil add entity, .merge(:singleton => true), path_steps end |
#ancestry ⇒ Object
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, 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, ).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
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
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
50 51 52 |
# File 'lib/hammock/route_node.rb', line 50 def singleton? @singleton end |