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.


12
13
14
15
16
17
18
# File 'lib/usher/node.rb', line 12

def initialize(parent, value)
  @parent = parent
  @value = value
  @lookup = Hash.new
  @greedy_lookup = Hash.new
  @exclusive_type = nil
end

Instance Attribute Details

#exclusive_typeObject

Returns the value of attribute exclusive_type.


10
11
12
# File 'lib/usher/node.rb', line 10

def exclusive_type
  @exclusive_type
end

#greedy_lookupObject (readonly)

Returns the value of attribute greedy_lookup.


9
10
11
# File 'lib/usher/node.rb', line 9

def greedy_lookup
  @greedy_lookup
end

#lookupObject (readonly)

Returns the value of attribute lookup.


9
10
11
# File 'lib/usher/node.rb', line 9

def lookup
  @lookup
end

#parentObject

Returns the value of attribute parent.


10
11
12
# File 'lib/usher/node.rb', line 10

def parent
  @parent
end

#request_methodsObject

Returns the value of attribute request_methods.


10
11
12
# File 'lib/usher/node.rb', line 10

def request_methods
  @request_methods
end

#terminatesObject

Returns the value of attribute terminates.


10
11
12
# File 'lib/usher/node.rb', line 10

def terminates
  @terminates
end

#valueObject

Returns the value of attribute value.


10
11
12
# File 'lib/usher/node.rb', line 10

def value
  @value
end

Class Method Details

.root(route_set, request_methods) ⇒ Object


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

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

Instance Method Details

#add(route) ⇒ Object


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

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

#delete(route) ⇒ Object


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

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

#depthObject


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

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

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


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
# File 'lib/usher/node.rb', line 80

def find(usher, request, original_path, path, params = [], position = 0)
  if exclusive_type
    [lookup[request.send(exclusive_type)], lookup[nil]].each do |n|
      if n && (ret = n.find(usher, request, original_path, path.dup, params.dup, position))
        return ret
      end
    end
    nil
  elsif terminates? && (path.size.zero? || terminates.route.partial_match?)
    if terminates.route.partial_match?
      Response.new(terminates, params, original_path[position, original_path.size], original_path[0, position])
    else
      Response.new(terminates, params, nil, original_path)
    end
    
  elsif !path.size.zero? && (greedy? && (match_with_result_output = greedy_lookup.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, original_path, whole_path.size.zero? ? whole_path : usher.splitter.url_split(whole_path), params, position)
  elsif !path.size.zero? && (next_part = lookup[part = path.shift] || lookup[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
      loop do
        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.size.zero?
          break
        else
          part = path.shift
        end
      end
    when Route::Variable::Single
      var = next_part.value
      var.valid!(part)
      params << [var.name, part]
      until (var.look_ahead === path.first) || path.empty?
        next_path_part = path.shift
        position += next_path_part.size
        params.last.last << next_path_part
      end
    end
    next_part.find(usher, request, original_path, path, params, position)
  else
    nil
  end
end

#greedy?Boolean

Returns:

  • (Boolean)

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

def greedy?
  !@greedy_lookup.empty?
end

#ppObject


46
47
48
49
50
51
52
53
54
# File 'lib/usher/node.rb', line 46

def pp
  $stdout << " " * depth
  $stdout << "#{depth}: #{value.inspect} #{!!terminates?}\n"
  @lookup.each do |k,v|
    $stdout << " " * (depth + 1)
    $stdout << "#{k} ==> \n"
    v.pp
  end
end

#terminates?Boolean

Returns:

  • (Boolean)

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

def terminates?
  @terminates
end

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


68
69
70
71
72
73
74
75
76
77
78
# File 'lib/usher/node.rb', line 68

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

#upgrade_greedy_lookupObject


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

def upgrade_greedy_lookup
  @greedy_lookup = FuzzyHash.new(@greedy_lookup)
end

#upgrade_lookupObject


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

def upgrade_lookup
  @lookup = FuzzyHash.new(@lookup)
end