Class: Usher::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/usher/node.rb,
lib/usher/node/root.rb,
lib/usher/node/response.rb,
lib/usher/node/failed_response.rb,
lib/usher/node/root_ignoring_trailing_delimiters.rb

Overview

The node class used to walk the tree looking for a matching route. The node has three different things that it looks for. ## Normal The normal hash is used to normally find matching parts. As well, the reserved key, ‘nil` is used to denote a variable match. ## Greedy The greedy hash is used when you want to match on the entire path. This match can trancend delimiters (unlike the normal match) and match as much of the path as needed. ## Request The request hash is used to find request method restrictions after the entire path has been consumed.

Once the node finishes looking for matches, it looks for a ‘terminates` on the node that is usable. If it finds one, it wraps it into a Response and returns that. All actual matching though should normally be done off of Root#lookup

See Also:

Direct Known Subclasses

Root

Defined Under Namespace

Classes: FailedResponse, Response, Root, RootIgnoringTrailingDelimiters, Terminates

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, value) ⇒ Node

Returns a new instance of Node.



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

def initialize(parent, value)
  @parent, @value = parent, value
end

Instance Attribute Details

#default_terminatesObject

Returns the value of attribute default_terminates.



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

def default_terminates
  @default_terminates
end

#greedyObject (readonly)

Returns the value of attribute greedy.



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

def greedy
  @greedy
end

#metaObject (readonly)

Returns the value of attribute meta.



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

def meta
  @meta
end

#normalObject (readonly)

Returns the value of attribute normal.



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

def normal
  @normal
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

#requestObject (readonly)

Returns the value of attribute request.



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

def request
  @request
end

#request_method_typeObject

Returns the value of attribute request_method_type.



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

def request_method_type
  @request_method_type
end

#request_methodsObject

Returns the value of attribute request_methods.



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

def request_methods
  @request_methods
end

#terminatesObject (readonly)

Returns the value of attribute terminates.



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

def terminates
  @terminates
end

#unique_terminating_routesObject (readonly)

Returns the value of attribute unique_terminating_routes.



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

def unique_terminating_routes
  @unique_terminating_routes
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#add_meta(obj) ⇒ Object



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

def add_meta(obj)
  create_meta << obj
end

#add_terminate(path) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/usher/node.rb', line 49

def add_terminate(path)
  if path.route.when_proc
    create_terminate.choices << path
  else
    create_terminate.default = path
  end
  unique_terminating_routes << path.route
end

#inspectObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/usher/node.rb', line 33

def inspect
  out = ''
  out << " " * depth
  out << "#{terminates ? '* ' : ''}#{depth}: #{value.inspect}\n"
  [:normal, :greedy, :request].each do |node_type|
    send(node_type).each do |k,v|
      out << (" " * (depth + 1)) << "#{node_type.to_s[0].chr} #{k.inspect} ==> \n" << v.inspect
    end if send(node_type)
  end
  out
end

#remove_terminate(path) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/usher/node.rb', line 58

def remove_terminate(path)
  if terminates
    terminates.choices.delete(path)
    terminates.default = nil if terminates.default == path
  end
  unique_terminating_routes.delete_if{|r| r == path.route}
end