Class: ActionTree::Basic::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DialectHelper

#dialect

Constructor Details

#initialize(path = [], &blk) ⇒ Node

Returns a new instance of Node.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/action_tree/basic/node.rb', line 12

def initialize(path=[], &blk)
  path = parse_path(path)
  @token          = path.shift
  @children       = Set.new
  @helper_scope   = Module.new
  @before_hooks   = []
  @after_hooks    = []
  @postprocessors = []
  @action_namespaces = {}
  @not_found_handler = nil
  route(path, &blk) if block_given?
end

Instance Attribute Details

#action_namespacesObject (readonly)

Returns the value of attribute action_namespaces.



6
7
8
# File 'lib/action_tree/basic/node.rb', line 6

def action_namespaces
  @action_namespaces
end

#after_hooksObject (readonly)

Returns the value of attribute after_hooks.



6
7
8
# File 'lib/action_tree/basic/node.rb', line 6

def after_hooks
  @after_hooks
end

#before_hooksObject (readonly)

Returns the value of attribute before_hooks.



6
7
8
# File 'lib/action_tree/basic/node.rb', line 6

def before_hooks
  @before_hooks
end

#childrenObject (readonly)

Returns the value of attribute children.



6
7
8
# File 'lib/action_tree/basic/node.rb', line 6

def children
  @children
end

#helper_scopeObject (readonly)

Returns the value of attribute helper_scope.



6
7
8
# File 'lib/action_tree/basic/node.rb', line 6

def helper_scope
  @helper_scope
end

#not_found_handlerObject

Returns the value of attribute not_found_handler.



9
10
11
# File 'lib/action_tree/basic/node.rb', line 9

def not_found_handler
  @not_found_handler
end

#postprocessorsObject (readonly)

Returns the value of attribute postprocessors.



6
7
8
# File 'lib/action_tree/basic/node.rb', line 6

def postprocessors
  @postprocessors
end

#tokenObject (readonly)

Returns the value of attribute token.



6
7
8
# File 'lib/action_tree/basic/node.rb', line 6

def token
  @token
end

Instance Method Details

#action(location = nil, namespace = :default, &blk) ⇒ Object Also known as: a, o



74
75
76
77
# File 'lib/action_tree/basic/node.rb', line 74

def action(location=nil, namespace=:default, &blk)
  descend(location).action_namespaces[namespace] = blk
  self
end

#after(location = nil, &blk) ⇒ Object



94
95
96
# File 'lib/action_tree/basic/node.rb', line 94

def after(location=nil, &blk)
  descend(location).after_hooks << blk if blk
end

#apply(layer) ⇒ Object



72
# File 'lib/action_tree/basic/node.rb', line 72

def apply(layer); route(&layer); end

#before(location = nil, &blk) ⇒ Object Also known as: b



89
90
91
# File 'lib/action_tree/basic/node.rb', line 89

def before(location=nil, &blk)
  descend(location).before_hooks << blk if blk
end

#capture_namesObject



52
53
54
55
56
57
58
59
# File 'lib/action_tree/basic/node.rb', line 52

def capture_names
  case @token
    when Regexp then ['match']
    when Symbol then [@token.to_s]
    when String
      @token.scan(/:\w+/).map {|m| m[1..-1] }
  end
end

#delete(loc = nil, &blk) ⇒ Object



83
# File 'lib/action_tree/basic/node.rb', line 83

def delete(loc=nil, &blk); action(loc, :delete, &blk);  end

#descend(path) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/action_tree/basic/node.rb', line 123

def descend(path)
  loc = parse_path(path)
  if loc.empty? then self else
    token = loc.shift
    (get_child(token) || make_child(token)).descend(loc)
  end
end

#get(loc = nil, &blk) ⇒ Object



80
# File 'lib/action_tree/basic/node.rb', line 80

def get(   loc=nil, &blk); action(loc, :get,    &blk);  end

#helpers(location = nil, &blk) ⇒ Object



85
86
87
# File 'lib/action_tree/basic/node.rb', line 85

def helpers(location=nil, &blk)
  descend(location).helper_scope.module_eval(&blk)
end

#inspectObject

INSPECTION



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

def inspect
  "#<#{self.class}:#{self.object_id.to_s(16)} #{@token.inspect} >"
end

#match(path = []) ⇒ Object

LOOKUPS



119
120
121
# File 'lib/action_tree/basic/node.rb', line 119

def match(path=[])
  dialect::Match.new(self, nil, nil).match(path)
end

#match?(path_fragment) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/action_tree/basic/node.rb', line 48

def match?(path_fragment)
  !!path_fragment.match(regexp)
end

#mount(node, location = nil) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/action_tree/basic/node.rb', line 108

def mount(node, location=nil)
    if node.token
      descend(location).children < node
    else
      raise 'root nodes can not be mounted. apply a layer instead.'
    end
end

#not_found(location = nil, &blk) ⇒ Object Also known as: x



98
99
100
# File 'lib/action_tree/basic/node.rb', line 98

def not_found(location=nil, &blk)
  descend(location).not_found_handler = blk if blk
end

#post(loc = nil, &blk) ⇒ Object



82
# File 'lib/action_tree/basic/node.rb', line 82

def post(  loc=nil, &blk); action(loc, :post,   &blk);  end

#postprocessor(location = nil, &blk) ⇒ Object Also known as: p



103
104
105
# File 'lib/action_tree/basic/node.rb', line 103

def postprocessor(location=nil, &blk)
  descend(location).postprocessors << blk if blk
end

#printout(stack = '') ⇒ Object



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

def printout(stack='')
  stack + @token.inspect + "\n" +
  @children.map {|c| c.printout(stack + '  ') }.join
end

#put(loc = nil, &blk) ⇒ Object



81
# File 'lib/action_tree/basic/node.rb', line 81

def put(   loc=nil, &blk); action(loc, :put,    &blk);  end

#regexpObject

MATCHING



38
39
40
41
42
43
44
45
46
# File 'lib/action_tree/basic/node.rb', line 38

def regexp
  @regexp ||=
    Regexp.new('^' + case @token
      when Regexp then "(#{@token.source})"
      when Symbol then '(.+)'
      when String then @token.gsub(/:\w+/, '(.+)')
      #when nil    then '\/*'
    end + '$')
end

#route(location = nil, &blk) ⇒ Object Also known as: with, w, r, _

DSL



64
65
66
67
# File 'lib/action_tree/basic/node.rb', line 64

def route(location=nil, &blk)
  descend(location).instance_eval(&blk) if blk
  self
end