Class: ImproveYourCode::Context::ModuleContext

Inherits:
CodeContext
  • Object
show all
Defined in:
lib/improve_your_code/context/module_context.rb

Instance Attribute Summary collapse

Attributes inherited from CodeContext

#children, #exp, #parent, #statement_counter

Instance Method Summary collapse

Methods inherited from CodeContext

#apply_current_visibility, #config_for, #each, #full_name, #instance_method?, #local_nodes, #matches?, #number_of_statements, #record_call_to, #record_use_of_self, #register_with_parent, #singleton_method?

Constructor Details

#initialize(exp) ⇒ ModuleContext

Returns a new instance of ModuleContext.



13
14
15
16
# File 'lib/improve_your_code/context/module_context.rb', line 13

def initialize(exp)
  super
  @visibility_tracker = VisibilityTracker.new
end

Instance Attribute Details

#visibility_trackerObject (readonly)

Returns the value of attribute visibility_tracker.



11
12
13
# File 'lib/improve_your_code/context/module_context.rb', line 11

def visibility_tracker
  @visibility_tracker
end

Instance Method Details

#append_child_context(child) ⇒ Object



18
19
20
21
# File 'lib/improve_your_code/context/module_context.rb', line 18

def append_child_context(child)
  visibility_tracker.set_child_visibility(child)
  super
end

#attribute_context_classObject



27
28
29
# File 'lib/improve_your_code/context/module_context.rb', line 27

def attribute_context_class
  AttributeContext
end

#defined_instance_methods(visibility: :public) ⇒ Object



31
32
33
34
35
# File 'lib/improve_your_code/context/module_context.rb', line 31

def defined_instance_methods(visibility: :public)
  instance_method_children.select do |context|
    context.visibility == visibility
  end
end

#descriptively_commented?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/improve_your_code/context/module_context.rb', line 47

def descriptively_commented?
  CodeComment.new(comment: exp.leading_comment).descriptive?
end

#instance_method_callsObject



37
38
39
40
41
# File 'lib/improve_your_code/context/module_context.rb', line 37

def instance_method_calls
  instance_method_children.flat_map do |context|
    context.children.grep(SendContext)
  end
end

#method_context_classObject



23
24
25
# File 'lib/improve_your_code/context/module_context.rb', line 23

def method_context_class
  MethodContext
end

#namespace_module?Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/improve_your_code/context/module_context.rb', line 51

def namespace_module?
  return false if exp.type == :casgn
  children = exp.direct_children
  children.any? && children.all? { |child| %i[casgn class module].include? child.type }
end

#node_instance_methodsObject



43
44
45
# File 'lib/improve_your_code/context/module_context.rb', line 43

def node_instance_methods
  local_nodes(:def)
end

#track_visibility(visibility, names) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/improve_your_code/context/module_context.rb', line 57

def track_visibility(visibility, names)
  visibility_tracker.track_visibility children: instance_method_children,
                                      visibility: visibility,
                                      names: names
  visibility_tracker.track_singleton_visibility children: singleton_method_children,
                                                visibility: visibility,
                                                names: names
end