Class: Audition::Rewriters::Memoization::SingletonIvars

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/audition/rewriters.rb

Overview

Collects ivar operations that touch the class object: inside def self.x, inside class << self methods, or directly in the class body (recorded with body: true, which disqualifies the group). Instance-method ivars are ignored.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSingletonIvars

Returns a new instance of SingletonIvars.



153
154
155
156
157
158
159
# File 'lib/audition/rewriters.rb', line 153

def initialize
  @groups = Hash.new { |h, k| h[k] = [] }
  @namespace = []
  @sclass_depth = 0
  @def_stack = []
  super
end

Instance Attribute Details

#groupsObject (readonly)

Returns the value of attribute groups.



151
152
153
# File 'lib/audition/rewriters.rb', line 151

def groups
  @groups
end

Instance Method Details

#visit_class_node(node) ⇒ Object



161
162
163
164
165
166
# File 'lib/audition/rewriters.rb', line 161

def visit_class_node(node)
  @namespace.push(node.constant_path.location.slice)
  super
ensure
  @namespace.pop
end

#visit_def_node(node) ⇒ Object



188
189
190
191
192
193
194
195
# File 'lib/audition/rewriters.rb', line 188

def visit_def_node(node)
  kind =
    node.receiver.is_a?(Prism::SelfNode) ? :self : :plain
  @def_stack.push(kind)
  super
ensure
  @def_stack.pop
end

#visit_module_node(node) ⇒ Object



168
169
170
171
172
173
# File 'lib/audition/rewriters.rb', line 168

def visit_module_node(node)
  @namespace.push(node.constant_path.location.slice)
  super
ensure
  @namespace.pop
end

#visit_singleton_class_node(node) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/audition/rewriters.rb', line 175

def visit_singleton_class_node(node)
  if node.expression.is_a?(Prism::SelfNode)
    @sclass_depth += 1
    begin
      super
    ensure
      @sclass_depth -= 1
    end
  else
    super
  end
end