Class: ActionTree::Basic::Match

Inherits:
Object
  • Object
show all
Includes:
DialectHelper
Defined in:
lib/action_tree/basic/match.rb

Constant Summary collapse

DEFAULT_HELPERS =
[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DialectHelper

#dialect

Constructor Details

#initialize(node, parent, fragment) ⇒ Match

Returns a new instance of Match.



13
14
15
# File 'lib/action_tree/basic/match.rb', line 13

def initialize(node, parent, fragment)
  @node, @parent, @fragment = node, parent, fragment
end

Instance Attribute Details

#fragmentObject (readonly)

Returns the value of attribute fragment.



11
12
13
# File 'lib/action_tree/basic/match.rb', line 11

def fragment
  @fragment
end

#nodeObject (readonly)

Returns the value of attribute node.



11
12
13
# File 'lib/action_tree/basic/match.rb', line 11

def node
  @node
end

#parentObject (readonly)

Returns the value of attribute parent.



11
12
13
# File 'lib/action_tree/basic/match.rb', line 11

def parent
  @parent
end

#selected_scopeObject (readonly)

Returns the value of attribute selected_scope.



11
12
13
# File 'lib/action_tree/basic/match.rb', line 11

def selected_scope
  @selected_scope
end

Instance Method Details

#capturesObject



19
20
21
# File 'lib/action_tree/basic/match.rb', line 19

def captures
  @captures ||= read_captures
end

#found?Boolean

Returns:

  • (Boolean)


17
# File 'lib/action_tree/basic/match.rb', line 17

def found?; @node; end

#match(path) ⇒ Object

Return @self@ or a descendant matching @path@, or a NotFound if not found.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/action_tree/basic/match.rb', line 59

def match(path)
  path = parse_path(path)
  return self if path.empty?
  return self unless found?

  fragment = path.shift
  @node.children.each do |child|
    if child.match?(fragment)
      return self.class.new(
        child, self, fragment).match(path)
    end
  end

  self.class.new(nil, self, fragment) # not found
end

#match_chainObject



23
24
25
# File 'lib/action_tree/basic/match.rb', line 23

def match_chain
  @match_chain ||= @parent ? @parent.match_chain << self : [self]
end

#node_chainObject



31
32
33
# File 'lib/action_tree/basic/match.rb', line 31

def node_chain
  @node_chain ||= valid_match_chain.map(&:node)
end

#pathObject



35
36
37
# File 'lib/action_tree/basic/match.rb', line 35

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

#run(namespace = :default, *scope_sources) ⇒ Object

Run the action; first the before filters within the match chain, then the actions, then the postprocessors, then the after filters, returning the return value of the last evaluated action.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/action_tree/basic/match.rb', line 40

def run(namespace=:default, *scope_sources)

  eval_scope = ::ActionTree::EvalScope.new(
    *helper_mixins, *scope_sources, captures,
    *DEFAULT_HELPERS,
    {:__match => self}
    )

  hooks(:before_hooks).each {|prc| eval_scope.instance_eval(&prc) }
  result = eval_scope.instance_eval(&main_action(namespace))
  hooks(:after_hooks).each  {|prc| eval_scope.instance_eval(&prc) }

  postprocessors.inject(result) do |result, postprocessor|
    eval_scope.instance_exec(result, &postprocessor)
  end
end

#valid_match_chainObject



27
28
29
# File 'lib/action_tree/basic/match.rb', line 27

def valid_match_chain
  @valid_match_chain ||= match_chain.select(&:node)
end