Class: Usher::Node
- Inherits:
-
Object
- Object
- Usher::Node
- Defined in:
- lib/usher/node.rb
Defined Under Namespace
Classes: Response
Instance Attribute Summary collapse
-
#exclusive_type ⇒ Object
Returns the value of attribute exclusive_type.
-
#greedy_lookup ⇒ Object
readonly
Returns the value of attribute greedy_lookup.
-
#lookup ⇒ Object
readonly
Returns the value of attribute lookup.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#request_methods ⇒ Object
Returns the value of attribute request_methods.
-
#terminates ⇒ Object
Returns the value of attribute terminates.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #add(route) ⇒ Object
- #delete(route) ⇒ Object
- #depth ⇒ Object
- #find(usher, request, original_path, path, params = [], position = 0) ⇒ Object
- #greedy? ⇒ Boolean
-
#initialize(parent, value) ⇒ Node
constructor
A new instance of Node.
- #pp ⇒ Object
- #terminates? ⇒ Boolean
- #unique_routes(node = self, routes = []) ⇒ Object
- #upgrade_greedy_lookup ⇒ Object
- #upgrade_lookup ⇒ Object
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_type ⇒ Object
Returns the value of attribute exclusive_type.
10 11 12 |
# File 'lib/usher/node.rb', line 10 def exclusive_type @exclusive_type end |
#greedy_lookup ⇒ Object (readonly)
Returns the value of attribute greedy_lookup.
9 10 11 |
# File 'lib/usher/node.rb', line 9 def greedy_lookup @greedy_lookup end |
#lookup ⇒ Object (readonly)
Returns the value of attribute lookup.
9 10 11 |
# File 'lib/usher/node.rb', line 9 def lookup @lookup end |
#parent ⇒ Object
Returns the value of attribute parent.
10 11 12 |
# File 'lib/usher/node.rb', line 10 def parent @parent end |
#request_methods ⇒ Object
Returns the value of attribute request_methods.
10 11 12 |
# File 'lib/usher/node.rb', line 10 def request_methods @request_methods end |
#terminates ⇒ Object
Returns the value of attribute terminates.
10 11 12 |
# File 'lib/usher/node.rb', line 10 def terminates @terminates end |
#value ⇒ Object
Returns the value of attribute value.
10 11 12 |
# File 'lib/usher/node.rb', line 10 def value @value end |
Class Method Details
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 |
#depth ⇒ Object
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
32 33 34 |
# File 'lib/usher/node.rb', line 32 def greedy? !@greedy_lookup.empty? end |
#pp ⇒ Object
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
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_lookup ⇒ Object
24 25 26 |
# File 'lib/usher/node.rb', line 24 def upgrade_greedy_lookup @greedy_lookup = FuzzyHash.new(@greedy_lookup) end |
#upgrade_lookup ⇒ Object
20 21 22 |
# File 'lib/usher/node.rb', line 20 def upgrade_lookup @lookup = FuzzyHash.new(@lookup) end |