Class: Xampl::Visitor

Inherits:
Object
  • Object
show all
Defined in:
lib/xamplr/visitor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVisitor

Returns a new instance of Visitor.



6
7
8
# File 'lib/xamplr/visitor.rb', line 6

def initialize
  reset
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object



35
36
37
# File 'lib/xamplr/visitor.rb', line 35

def method_missing(symbol, *args)
  return nil
end

Instance Attribute Details

#doneObject

Returns the value of attribute done.



4
5
6
# File 'lib/xamplr/visitor.rb', line 4

def done
  @done
end

#no_childrenObject

Returns the value of attribute no_children.



4
5
6
# File 'lib/xamplr/visitor.rb', line 4

def no_children
  @no_children
end

#no_siblingsObject

Returns the value of attribute no_siblings.



4
5
6
# File 'lib/xamplr/visitor.rb', line 4

def no_siblings
  @no_siblings
end

Instance Method Details

#after_visit(xampl) ⇒ Object



47
48
49
# File 'lib/xamplr/visitor.rb', line 47

def after_visit(xampl)
  xampl.after_visit(self)
end

#around_visit(xampl) ⇒ Object



51
52
53
# File 'lib/xamplr/visitor.rb', line 51

def around_visit(xampl)
  return false
end

#before_visit(xampl) ⇒ Object



43
44
45
# File 'lib/xamplr/visitor.rb', line 43

def before_visit(xampl)
  xampl.before_visit(self)
end

#cycle(xampl) ⇒ Object



24
25
26
# File 'lib/xamplr/visitor.rb', line 24

def cycle(xampl)
  return false
end

#resetObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/xamplr/visitor.rb', line 10

def reset
  @no_children = false
  @no_siblings = false
  @done = false

  @short_circuit = false

  @visited = {}
  @visiting = {}

  @revisiting = false
  @cycling = false
end

#revisit(xampl) ⇒ Object



28
29
30
# File 'lib/xamplr/visitor.rb', line 28

def revisit(xampl)
  return false
end

#short_circuitObject



32
33
# File 'lib/xamplr/visitor.rb', line 32

def short_circuit
end

#start(xampl_in) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/xamplr/visitor.rb', line 58

def start(xampl_in)
  xampl = substitute_in_visit(xampl_in)

  n = @visiting[xampl]
  if n then
    @visiting[xampl] = n + 1
  else
    @visiting[xampl] = 1
  end

  if 1 < @visiting[xampl] then
    return self unless cycle(xampl)
    @cycling = true
    @revisiting = true
  elsif @visited.has_key? xampl then
    return self unless revisit(xampl)
    @revisiting = true
  end

  @visited[xampl] = xampl

  before_visit(xampl)
  if @no_children then
    @no_children = false
    return self
  end

  xampl.visit(self) unless around_visit(xampl) or !xampl.respond_to? "visit"

  return self if @done
  return self if @no_siblings

  if @no_children then
    after_visit(xampl)
    @no_children = false
    return self
  end

  if @short_circuit then
    short_circuit
    @short_circuit = false
  else
    xampl.children.each do | child |
      if child.kind_of?(XamplObject) then
        start(child)
      else
        visit_string(child)
      end

      after_visit(xampl) if @done
      return self if @done

      if @no_siblings then
        @no_siblings = false
        after_visit(xampl)
        return self
      end
    end if xampl.respond_to? "children"
  end

  after_visit(xampl)
  return self

#rescue  => e
#  puts "visit failed !!!!! #{ e }"
#  e.backtrace.each do | trace |
#    puts "  #{trace}"
#    break if /actionpack/ =~ trace
#  end
#  raise e

ensure
  n = @visiting[xampl]
  if 1 == n then
    @visiting.delete(xampl)
  else
    @visiting[xampl] = n - 1
  end
  @revisiting = false
  @cycling = false
end

#substitute_in_visit(xampl) ⇒ Object



39
40
41
# File 'lib/xamplr/visitor.rb', line 39

def substitute_in_visit(xampl)
  return xampl.substitute_in_visit(self)
end

#visit_string(string) ⇒ Object



55
56
# File 'lib/xamplr/visitor.rb', line 55

def visit_string(string)
end