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



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

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

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



107
108
109
# File 'lib/action_tree/basic/node.rb', line 107

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

#apply(layer) ⇒ Object



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

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

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



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

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

#capture_namesObject



65
66
67
68
69
70
71
72
# File 'lib/action_tree/basic/node.rb', line 65

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



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

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

#descend(path) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/action_tree/basic/node.rb', line 136

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

#display_nameObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/action_tree/basic/node.rb', line 31

def display_name
  case @token
    when String then @token
    when Symbol then @token.inspect
    when Regexp then @token.source
    when nil    then '(nil)'
  end + ' ' + [
    @actions.size.to_s,
    @before_hooks.size,
    @after_hooks.size
    ].join(', ')
end

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



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

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

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



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

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)} #{display_name} >"
end

#match(path = []) ⇒ Object

LOOKUPS



132
133
134
# File 'lib/action_tree/basic/node.rb', line 132

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

#match?(path_fragment) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/action_tree/basic/node.rb', line 61

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

#mount(node, location = nil) ⇒ Object



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

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



111
112
113
# File 'lib/action_tree/basic/node.rb', line 111

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

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



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

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

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



116
117
118
# File 'lib/action_tree/basic/node.rb', line 116

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

#printout(stack = '') ⇒ Object



44
45
46
47
# File 'lib/action_tree/basic/node.rb', line 44

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

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



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

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

#regexpObject

MATCHING



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

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



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

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