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
# File 'lib/hammock/route_node.rb', line 20

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

Instance Attribute Details

#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

#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, steps = nil) ⇒ Object



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

def add entity, options, steps = nil
  if steps.nil?
    add entity, options, (options[:name_prefix] || '').chomp('_').split('_').map {|i| i.pluralize.underscore.to_sym }
  elsif steps.empty?
    add_child entity, options
  else
    children[steps.shift].add entity, options, steps
  end
end

#ancestryObject



39
40
41
# File 'lib/hammock/route_node.rb', line 39

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

#base_for(resources) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hammock/route_node.rb', line 56

def base_for resources
  # puts "base_for<#{mdl}>: resources=#{resources.inspect}."
  if resources.empty?
    self
  else
    match = nil
    children.values.detect {|child|
      # puts "  Trying #{child.ancestry.map(&:mdl).inspect} for #{resources.inspect}"
      if !resources.include?(child.mdl)
        # puts "  Can't match #{resources.inspect} against #{child.mdl}."
        nil
      else
        # puts "  Matched #{child.mdl} from #{resources.inspect}."
        match = child.base_for resources.discard(child.mdl)
      end
    } #|| raise("There is no routing path for #{resources.map(&:inspect).inspect}.")
    match
  end
end

#for(verb, entities, options) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hammock/route_node.rb', line 43

def for verb, entities, options
  raise "Hammock::RouteNode#for requires an explicitly specified verb 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



76
77
78
79
80
81
82
# File 'lib/hammock/route_node.rb', line 76

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



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

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

#root?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/hammock/route_node.rb', line 35

def root?
  parent.nil?
end

#routeable_as(verb, entity) ⇒ Object



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

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