Class: ActionTree::Basic::Query

Inherits:
Object
  • Object
show all
Includes:
Shared, Errors
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

Templated::Query

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Shared

#dialect

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

#fragmentString (readonly)

The path fragment matched by this single Query.

Returns:

  • (String)


28
29
30
# File 'lib/action_tree/basic/query.rb', line 28

def fragment
  @fragment
end

#namespaceString (readonly)

The action namespace specified for this Query.

Returns:

  • (String)


32
33
34
# File 'lib/action_tree/basic/query.rb', line 32

def namespace
  @namespace
end

#nodeNode (readonly)

The Node wrapped by this query.

Returns:



20
21
22
# File 'lib/action_tree/basic/query.rb', line 20

def node
  @node
end

#parentQuery (readonly)

The parent Query in the chain, or nil if first.

Returns:



24
25
26
# File 'lib/action_tree/basic/query.rb', line 24

def parent
  @parent
end

Instance Method Details

#capturesObject

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

#configObject

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.

Returns:

  • (Boolean)


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.

Returns:



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_pathArray

Same as #query_path, but only include ActionTree::Basic::Query objects that map to a real node.

Returns:

  • (Array)


67
68
69
# File 'lib/action_tree/basic/query.rb', line 67

def match_path
  @match_path ||= query_path.select(&:node)
end

#node_pathArray

The nodes along the #match_path

Returns:

  • (Array)


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?

Returns:

  • (Boolean)


55
# File 'lib/action_tree/basic/query.rb', line 55

def not_found?; !found?;  end

#pathString

The matched request path

Returns:

  • (String)


79
80
81
# File 'lib/action_tree/basic/query.rb', line 79

def path
  query_path.map(&:fragment).join('/')
end

#query_pathArray

The chain of Query objects wrapping the matched nodes along the requested path.

Returns:

  • (Array)


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.

Parameters:

  • vars (Hash) (defaults to: {})

    Instance variables to be set in the action

Returns:

  • (Object)

    The (processed) result of running the action.



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