Class: ActionTree::Basic::Query
- Inherits:
-
Object
- Object
- ActionTree::Basic::Query
- Defined in:
- lib/action_tree/basic/query.rb
Overview
When a request is matched against an action tree, (or rather, its root node), a chain of query objects is built, and the last one returned.
Each query object wraps a node, and the query path is the list of queries leading from the root down to the item matched.
When no node matches, a query object is still returned, with different behaviour, and #found? => false
Direct Known Subclasses
Instance Attribute Summary collapse
-
#fragment ⇒ String
readonly
The path fragment matched by this single
Query
. -
#namespace ⇒ String
readonly
The action namespace specified for this
Query
. -
#node ⇒ Node
readonly
The Node wrapped by this query.
-
#parent ⇒ Query
readonly
The parent
Query
in the chain, ornil
if first.
Instance Method Summary collapse
-
#captures ⇒ Object
A hash of values captured by the query.
-
#config ⇒ Object
The configuration inherited through the matched nodes, made available in the scope through @_conf.
-
#found? ⇒ Boolean
Wether the request was successfully matched.
-
#initialize(node, parent, fragment, namespace) ⇒ Query
constructor
private
Queries should be created by calling Node#match.
-
#match(path) ⇒ Query
(also: #query)
Do an additional lookup beneath this
Query
. -
#match_path ⇒ Array
Same as #query_path, but only include Query objects that map to a real node.
-
#node_path ⇒ Array
The nodes along the #match_path.
-
#not_found? ⇒ Boolean
Inverse of #found?.
-
#path ⇒ String
The matched request path.
-
#query_path ⇒ Array
The chain of
Query
objects wrapping the matched nodes along the requested path. -
#run(vars = {}) ⇒ Object
Run the matched action along with hooks and processors.
Methods included from Shared
Constructor Details
#initialize(node, parent, fragment, namespace) ⇒ Query
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Queries should be created by calling Node#match
40 41 42 43 44 45 |
# File 'lib/action_tree/basic/query.rb', line 40 def initialize(node, parent, fragment, namespace) @node = node @parent = parent @fragment = fragment @namespace = namespace end |
Instance Attribute Details
#fragment ⇒ String (readonly)
The path fragment matched by this single Query
.
28 29 30 |
# File 'lib/action_tree/basic/query.rb', line 28 def fragment @fragment end |
#namespace ⇒ String (readonly)
The action namespace specified for this Query
.
32 33 34 |
# File 'lib/action_tree/basic/query.rb', line 32 def namespace @namespace end |
#node ⇒ Node (readonly)
The Node wrapped by this query.
20 21 22 |
# File 'lib/action_tree/basic/query.rb', line 20 def node @node end |
#parent ⇒ Query (readonly)
The parent Query
in the chain, or nil
if first.
24 25 26 |
# File 'lib/action_tree/basic/query.rb', line 24 def parent @parent end |
Instance Method Details
#captures ⇒ Object
A hash of values captured by the query
119 120 121 122 123 124 125 |
# File 'lib/action_tree/basic/query.rb', line 119 def captures return parent.captures unless @node @captures ||= @parent ? @node.read_captures(@fragment, @parent.captures.dup) : ActionTree::CaptureHash.new end |
#config ⇒ Object
The configuration inherited through the matched nodes, made available in the scope through @_conf.
112 113 114 115 116 |
# File 'lib/action_tree/basic/query.rb', line 112 def config @config ||= node_path.inject(dialect::CONFIG) do |conf, node| conf.merge(node.config) end end |
#found? ⇒ Boolean
Wether the request was successfully matched.
49 50 51 |
# File 'lib/action_tree/basic/query.rb', line 49 def found? @node && @node.actions[@namespace] end |
#match(path) ⇒ Query Also known as: query
Do an additional lookup beneath this Query
.
85 86 87 88 |
# File 'lib/action_tree/basic/query.rb', line 85 def match(path) path = parse_path(path) path.empty? ? self : match_one(path.shift).match(path) end |
#match_path ⇒ Array
Same as #query_path, but only include ActionTree::Basic::Query objects that map to a real node.
67 68 69 |
# File 'lib/action_tree/basic/query.rb', line 67 def match_path @match_path ||= query_path.select(&:node) end |
#node_path ⇒ Array
The nodes along the #match_path
73 74 75 |
# File 'lib/action_tree/basic/query.rb', line 73 def node_path @node_path ||= match_path.map(&:node) end |
#not_found? ⇒ Boolean
Inverse of #found?
55 |
# File 'lib/action_tree/basic/query.rb', line 55 def not_found?; !found?; end |
#path ⇒ String
The matched request path
79 80 81 |
# File 'lib/action_tree/basic/query.rb', line 79 def path query_path.map(&:fragment).join('/') end |
#query_path ⇒ Array
The chain of Query
objects wrapping the matched nodes
along the requested path.
60 61 62 |
# File 'lib/action_tree/basic/query.rb', line 60 def query_path @query_path ||= @parent ? @parent.query_path << self : [self] end |
#run(vars = {}) ⇒ Object
Run the matched action along with hooks and processors
evaluation scope.
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/action_tree/basic/query.rb', line 97 def run(vars={}) scope = ActionTree::EvalScope.new( vars, default_vars, captures, helper_mixins) begin raise NotFound unless found? run_everything(scope, vars) rescue Exception => err handler = handler_for(err) handler ? scope.instance_exec(err, &handler) : raise end end |