Class: Usher::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/usher/node.rb

Defined Under Namespace

Classes: Response

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, value) ⇒ Node

Returns a new instance of Node.



24
25
26
27
28
29
30
31
# File 'lib/usher/node.rb', line 24

def initialize(parent, value)
  @parent = parent
  @value = value
  @request = nil
  @normal = nil
  @greedy = nil
  @request_method_type = nil
end

Instance Attribute Details

#greedyObject (readonly)

Returns the value of attribute greedy.



21
22
23
# File 'lib/usher/node.rb', line 21

def greedy
  @greedy
end

#normalObject (readonly)

Returns the value of attribute normal.



21
22
23
# File 'lib/usher/node.rb', line 21

def normal
  @normal
end

#parentObject

Returns the value of attribute parent.



22
23
24
# File 'lib/usher/node.rb', line 22

def parent
  @parent
end

#requestObject (readonly)

Returns the value of attribute request.



21
22
23
# File 'lib/usher/node.rb', line 21

def request
  @request
end

#request_method_typeObject

Returns the value of attribute request_method_type.



22
23
24
# File 'lib/usher/node.rb', line 22

def request_method_type
  @request_method_type
end

#request_methodsObject

Returns the value of attribute request_methods.



22
23
24
# File 'lib/usher/node.rb', line 22

def request_methods
  @request_methods
end

#terminatesObject

Returns the value of attribute terminates.



22
23
24
# File 'lib/usher/node.rb', line 22

def terminates
  @terminates
end

#valueObject

Returns the value of attribute value.



22
23
24
# File 'lib/usher/node.rb', line 22

def value
  @value
end

Class Method Details

.root(route_set, request_methods) ⇒ Object



65
66
67
68
69
# File 'lib/usher/node.rb', line 65

def self.root(route_set, request_methods)
  root = self.new(route_set, nil)
  root.request_methods = request_methods
  root
end

Instance Method Details

#activate_greedy!Object



37
38
39
# File 'lib/usher/node.rb', line 37

def activate_greedy!
  @greedy ||= Hash.new
end

#activate_normal!Object



33
34
35
# File 'lib/usher/node.rb', line 33

def activate_normal!
  @normal ||= Hash.new
end

#activate_request!Object



41
42
43
# File 'lib/usher/node.rb', line 41

def activate_request!
  @request ||= Hash.new
end

#add(route) ⇒ Object



95
96
97
98
99
# File 'lib/usher/node.rb', line 95

def add(route)
  route.paths.each do |path|
    set_path_with_destination(path)
  end
end

#delete(route) ⇒ Object



101
102
103
104
105
# File 'lib/usher/node.rb', line 101

def delete(route)
  route.paths.each do |path|
    set_path_with_destination(path, nil)
  end
end

#depthObject



57
58
59
# File 'lib/usher/node.rb', line 57

def depth
  @depth ||= @parent.is_a?(Node) ? @parent.depth + 1 : 0
end

#find(usher, request_object, original_path, path, params = [], position = 0) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/usher/node.rb', line 122

def find(usher, request_object, original_path, path, params = [], position = 0)
  if terminates? && (path.empty? || terminates.route.partial_match? || (usher.ignore_trailing_delimiters? && path.all?{|p| usher.delimiters.include?(p)}))
    terminates.route.partial_match? ?
      Response.new(terminates, params, original_path[position, original_path.size], original_path[0, position]) :
      Response.new(terminates, params, nil, original_path)
  elsif !path.empty? and greedy and match_with_result_output = greedy.match_with_result(whole_path = original_path[position, original_path.size])
next_path, matched_part = match_with_result_output
    position += matched_part.size
    whole_path.slice!(0, matched_part.size)
    params << [next_path.value.name, matched_part] if next_path.value.is_a?(Route::Variable)
    next_path.find(usher, request_object, original_path, whole_path.empty? ? whole_path : usher.splitter.url_split(whole_path), params, position)
  elsif !path.empty? and normal and next_part = normal[path.first] || normal[nil]
    part = path.shift
    position += part.size
    case next_part.value
    when String
      # do nothing
    when Route::Variable::Single
      # get the variable
      var = next_part.value
      # do a validity check
      var.valid!(part)
      # because its a variable, we need to add it to the params array
      params << [var.name, part]
      until path.empty? || (var.look_ahead === path.first)                # variables have a look ahead notion, 
        next_path_part = path.shift                                       # and until they are satified,
        position += next_path_part.size                                   # keep appending to the value in params
        params.last.last << next_path_part
      end if var.look_ahead && usher.delimiters.size > 1
    when Route::Variable::Glob
      params << [next_part.value.name, []]
      while true
        if (next_part.value.look_ahead === part || (!usher.delimiters.unescaped.include?(part) && next_part.value.regex_matcher && !next_part.value.regex_matcher.match(part)))
          path.unshift(part)
          position -= part.size
          if usher.delimiters.unescaped.include?(next_part.parent.value)
            path.unshift(next_part.parent.value)
            position -= next_part.parent.value.size
          end
          break
        elsif !usher.delimiters.unescaped.include?(part)
          next_part.value.valid!(part)
          params.last.last << part
        end
        if path.empty?
          break
        else
          part = path.shift
        end
      end
    end
    next_part.find(usher, request_object, original_path, path, params, position)
  elsif request_method_type
    return_value = if (specific_node = request[request_object.send(request_method_type)] and ret = specific_node.find(usher, request_object, original_path, path.dup, params.dup, position))
      usher.priority_lookups? ? [ret] : ret
    end
    
    if usher.priority_lookups? || return_value.nil? and general_node = request[nil] and ret = general_node.find(usher, request_object, original_path, path.dup, params.dup, position)
      return_value = usher.priority_lookups? && return_value ? [return_value, ret] : ret
    end
    
    unless usher.priority_lookups?
      return_value
    else
      return_value = Array(return_value).flatten.compact
      return_value.sort!{|r1, r2| r1.path.route.priority <=> r2.path.route.priority}
      return_value.last
    end
  else
    nil
  end
end

#greedy?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/usher/node.rb', line 61

def greedy?
  @greedy
end

#ppObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/usher/node.rb', line 75

def pp
  $stdout << " " * depth
  $stdout << "#{terminates? ? '* ' : ''}#{depth}: #{value.inspect}\n"
  normal.each do |k,v|
    $stdout << " " * (depth + 1)
    $stdout << ". #{k.inspect} ==> \n"
    v.pp
  end if normal
  greedy.each do |k,v|
    $stdout << " " * (depth + 1)
    $stdout << "g #{k.inspect} ==> \n"
    v.pp
  end if greedy
  request.each do |k,v|
    $stdout << " " * (depth + 1)
    $stdout << "r #{k.inspect} ==> \n"
    v.pp
  end if request
end

#terminates?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/usher/node.rb', line 71

def terminates?
  @terminates && @terminates.route.recognizable?
end

#unique_routes(node = self, routes = []) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/usher/node.rb', line 107

def unique_routes(node = self, routes = [])
  routes << node.terminates.route if node.terminates
  node.normal.values.each do |v|
    unique_routes(v, routes)
  end if node.normal
  node.greedy.values.each do |v|
    unique_routes(v, routes)
  end if node.greedy
  node.request.values.each do |v|
    unique_routes(v, routes)
  end if node.request
  routes.uniq!
  routes
end

#upgrade_greedy!Object



49
50
51
# File 'lib/usher/node.rb', line 49

def upgrade_greedy!
  @greedy = FuzzyHash.new(@greedy)
end

#upgrade_normal!Object



45
46
47
# File 'lib/usher/node.rb', line 45

def upgrade_normal!
  @normal = FuzzyHash.new(@normal)
end

#upgrade_request!Object



53
54
55
# File 'lib/usher/node.rb', line 53

def upgrade_request!
  @request = FuzzyHash.new(@request)
end