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.



16
17
18
19
20
21
22
23
# File 'lib/usher/node.rb', line 16

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.



13
14
15
# File 'lib/usher/node.rb', line 13

def greedy
  @greedy
end

#normalObject (readonly)

Returns the value of attribute normal.



13
14
15
# File 'lib/usher/node.rb', line 13

def normal
  @normal
end

#parentObject

Returns the value of attribute parent.



14
15
16
# File 'lib/usher/node.rb', line 14

def parent
  @parent
end

#requestObject (readonly)

Returns the value of attribute request.



13
14
15
# File 'lib/usher/node.rb', line 13

def request
  @request
end

#request_method_typeObject

Returns the value of attribute request_method_type.



14
15
16
# File 'lib/usher/node.rb', line 14

def request_method_type
  @request_method_type
end

#request_methodsObject

Returns the value of attribute request_methods.



14
15
16
# File 'lib/usher/node.rb', line 14

def request_methods
  @request_methods
end

#terminatesObject

Returns the value of attribute terminates.



14
15
16
# File 'lib/usher/node.rb', line 14

def terminates
  @terminates
end

#valueObject

Returns the value of attribute value.



14
15
16
# File 'lib/usher/node.rb', line 14

def value
  @value
end

Class Method Details

.root(route_set, request_methods) ⇒ Object



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

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



29
30
31
# File 'lib/usher/node.rb', line 29

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

#activate_normal!Object



25
26
27
# File 'lib/usher/node.rb', line 25

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

#activate_request!Object



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

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

#add(route) ⇒ Object



87
88
89
90
91
# File 'lib/usher/node.rb', line 87

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

#delete(route) ⇒ Object



93
94
95
96
97
# File 'lib/usher/node.rb', line 93

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

#depthObject



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

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

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



114
115
116
117
118
119
120
121
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
# File 'lib/usher/node.rb', line 114

def find(usher, request_object, original_path, path, params = [], position = 0)
  if terminates? && (path.empty? || terminates.route.partial_match?)
    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? && (greedy? && (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
    params << [next_path.value.name, whole_path.slice!(0, matched_part.size)]
    next_path.find(usher, request_object, original_path, whole_path.empty? ? whole_path : usher.splitter.url_split(whole_path), params, position)
  elsif !path.empty? && normal && (next_part = normal[part = path.shift] || normal[nil])
    position += part.size
    case next_part.value
    when Route::Variable::Glob
      params << [next_part.value.name, []] unless params.last && params.last.first == next_part.value.name
      while true
        if (next_part.value.look_ahead === part || (!usher.delimiter_chars.include?(part[0]) && next_part.value.regex_matcher && !next_part.value.regex_matcher.match(part)))
          path.unshift(part)
          position -= part.size
          if usher.delimiter_chars.include?(next_part.parent.value[0])
            path.unshift(next_part.parent.value)
            position -= next_part.parent.value.size
          end
          break
        elsif !usher.delimiter_chars.include?(part[0])
          next_part.value.valid!(part)
          params.last.last << part
        end
        if path.empty?
          break
        else
          part = path.shift
        end
      end
    when Route::Variable::Single
      var = next_part.value
      var.valid!(part)
      params << [var.name, part]
      until path.empty? || (var.look_ahead === path.first)
        next_path_part = path.shift
        position += next_path_part.size
        params.last.last << next_path_part
      end if var.look_ahead && usher.delimiter_chars.size > 1
    end
    next_part.find(usher, request_object, original_path, path, params, position)
  elsif request_method_type
    if (specific_node = request[request_object.send(request_method_type)]) && (ret = specific_node.find(usher, request_object, original_path, path.dup, params.dup, position))
      ret
    elsif (general_node = request[nil]) && (ret = general_node.find(usher, request_object, original_path, path.dup, params.dup, position))
      ret
    else
      nil
    end
  else
    nil
  end
end

#greedy?Boolean

Returns:

  • (Boolean)


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

def greedy?
  @greedy && !@greedy.empty?
end

#ppObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/usher/node.rb', line 67

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)


63
64
65
# File 'lib/usher/node.rb', line 63

def terminates?
  @terminates
end

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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/usher/node.rb', line 99

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



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

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

#upgrade_normal!Object



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

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

#upgrade_request!Object



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

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