Class: Jets::Router::Scope

Inherits:
Object
  • Object
show all
Includes:
Macros, Util
Defined in:
lib/jets/router/scope.rb

Defined Under Namespace

Modules: Macros

Constant Summary

Constants included from Macros

Macros::OPTION_ACCESSORS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Macros

#[], #[]=

Methods included from Util

#underscore

Constructor Details

#initialize(options = {}, parent = nil, level = 1) ⇒ Scope

Returns a new instance of Scope.



55
56
57
58
59
60
61
62
63
# File 'lib/jets/router/scope.rb', line 55

def initialize(options = {}, parent = nil, level = 1)
  @options = options
  @parent = parent
  @level = level
  @children = []
  if parent
    parent.children << self
  end
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



51
52
53
# File 'lib/jets/router/scope.rb', line 51

def children
  @children
end

#levelObject (readonly)

Returns the value of attribute level.



51
52
53
# File 'lib/jets/router/scope.rb', line 51

def level
  @level
end

#nextObject

Returns the value of attribute next.



52
53
54
# File 'lib/jets/router/scope.rb', line 52

def next
  @next
end

#optionsObject (readonly)

Returns the value of attribute options.



51
52
53
# File 'lib/jets/router/scope.rb', line 51

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



51
52
53
# File 'lib/jets/router/scope.rb', line 51

def parent
  @parent
end

Instance Method Details

#any_parent_shallow?Boolean

Returns:

  • (Boolean)


205
206
207
208
209
# File 'lib/jets/router/scope.rb', line 205

def any_parent_shallow?
  from_bottom.any? do |scope|
    scope.shallow? || scope.from == :shallow
  end
end

#colliding_resource_sibling?Boolean

Returns:

  • (Boolean)


154
155
156
157
158
159
160
# File 'lib/jets/router/scope.rb', line 154

def colliding_resource_sibling?
  return false
  # Don't think need to check for path because it doesn't really make sense
  # to point 2 different resources to the same path.
  resource_names = resource_siblings.map(&:resource_name)
  resource_names.uniq.size != resource_names.size
end

#from_bottomObject



191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/jets/router/scope.rb', line 191

def from_bottom
  current_scope = self
  scopes = []
  previous_scope = nil # child
  # do not include the root scope
  while current_scope.parent
    current_scope.next = previous_scope if previous_scope
    scopes << current_scope
    previous_scope = current_scope
    current_scope = current_scope.parent
  end
  scopes
end

#from_topObject Also known as: all_scopes



186
187
188
# File 'lib/jets/router/scope.rb', line 186

def from_top
  from_bottom.reverse
end

#needs_controller_path?Boolean

Returns:

  • (Boolean)


100
101
102
103
104
# File 'lib/jets/router/scope.rb', line 100

def needs_controller_path?
  return false if resource_name  # no adjustments if within resource or resources scope

  from.nil?
end

#new(options = {}) ⇒ Object



65
66
67
# File 'lib/jets/router/scope.rb', line 65

def new(options={})
  self.class.new(options, self, level + 1)
end

#param_placeholderObject

At time of each_resource DSL evaluation, the routes file has not fully evaluated and the scope context is not fully available. IE: info on the children.

Placeholder param allows the values to lazily replaced later with full context. We only have to replace the last segment with the placeholder. This is because previous segments are already replaced with Route::Path#path_prefixes scopes_from_top logic.



177
178
179
180
181
182
183
184
# File 'lib/jets/router/scope.rb', line 177

def param_placeholder
  if resource_name
    "#{resource_name.upcase}_PARAM"
  else
    prefix = path.to_s.gsub('/','_').upcase
    "#{prefix}_PARAM"
  end
end

#parent_or_higher?(other_scope) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
124
125
126
127
128
129
130
# File 'lib/jets/router/scope.rb', line 121

def parent_or_higher?(other_scope)
  current_scope = self

  while current_scope.parent && !current_scope.parent.virtual?
    return true if current_scope.parent == other_scope
    current_scope = current_scope.parent
  end

  false
end

#real_parent?(scope) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
# File 'lib/jets/router/scope.rb', line 132

def real_parent?(scope)
  if scope.virtual?
    scope.parent == parent
  else
    parent == scope
  end
end

#resolved_constraintsObject



88
89
90
91
92
93
# File 'lib/jets/router/scope.rb', line 88

def resolved_constraints
  from_bottom.each do |scope|
    return scope.constraints if scope.constraints
  end
  nil
end

#resolved_defaultsObject



80
81
82
83
84
85
86
# File 'lib/jets/router/scope.rb', line 80

def resolved_defaults
  result = {}
  from_top.each do |scope|
    result.merge!(scope.defaults) if scope.defaults
  end
  result
end

#resolved_moduleObject



95
96
97
98
# File 'lib/jets/router/scope.rb', line 95

def resolved_module
  items = from_top.map(&:module).compact
  items.join('/') unless items.empty?
end

#resolved_pathObject



110
111
112
113
114
115
116
117
118
119
# File 'lib/jets/router/scope.rb', line 110

def resolved_path
  case from
  when :resource, :resources
    path || resource_name
  when :namespace, :path, nil
    path
  when :member, :collection
    parent.resolved_path
  end
end

#resource_descendent?(scope = self) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
143
144
145
146
147
# File 'lib/jets/router/scope.rb', line 140

def resource_descendent?(scope=self)
  # return false
  scope.children.each do |child|
    return true if child.from == :resource || child.from == :resources
    return true if resource_descendent?(child)
  end
  false
end

#resource_sibling?Boolean

Returns:

  • (Boolean)


149
150
151
152
# File 'lib/jets/router/scope.rb', line 149

def resource_sibling?
  return false
  !resource_siblings.empty?
end

#resource_siblingsObject



162
163
164
165
166
167
168
# File 'lib/jets/router/scope.rb', line 162

def resource_siblings
  return [] unless parent
  parent.children.select do |c|
    c != self &&
    (c.from == :resource || c.from == :resources)
  end
end

#root?Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/jets/router/scope.rb', line 222

def root?
  @parent.nil?
end

#singularize_leading(items) ⇒ Object

singularize all except last item



212
213
214
215
216
217
218
219
220
# File 'lib/jets/router/scope.rb', line 212

def singularize_leading(items)
  result = []
  items.each_with_index do |item, index|
    item = item.to_s
    r = index == items.size - 1 ? item : item.singularize
    result << r
  end
  result
end

#to_sObject



226
227
228
# File 'lib/jets/router/scope.rb', line 226

def to_s
  "<Scope:#{object_id} @level=#{@level} @options=#{@options}>"
end

#virtual?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/jets/router/scope.rb', line 106

def virtual?
  from == :member || from == :collection
end

#virtual_controllerObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/jets/router/scope.rb', line 69

def virtual_controller
  case from
  when :member, :collection
    parent.virtual_controller
  when :resource
    controller || resource_name.to_s.pluralize
  else
    controller || resource_name
  end
end