Class: ImproveYourCode::Context::VisibilityTracker

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

Constant Summary collapse

VISIBILITY_MODIFIERS =
%i[
  private public protected
  module_function
].freeze
VISIBILITY_MAP =
{
  public_class_method: :public,
  private_class_method: :private
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeVisibilityTracker

Returns a new instance of VisibilityTracker.



15
16
17
# File 'lib/improve_your_code/context/visibility_tracker.rb', line 15

def initialize
  @tracked_visibility = :public
end

Instance Method Details

#set_child_visibility(child) ⇒ Object



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

def set_child_visibility(child)
  child.apply_current_visibility tracked_visibility
end

#track_singleton_visibility(children:, visibility:, names:) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/improve_your_code/context/visibility_tracker.rb', line 31

def track_singleton_visibility(children:, visibility:, names:)
  return if names.empty?

  visibility = VISIBILITY_MAP[visibility]

  return unless visibility

  track_visibility(
    children: children, visibility: visibility, names: names
  )
end

#track_visibility(children:, visibility:, names:) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/improve_your_code/context/visibility_tracker.rb', line 19

def track_visibility(children:, visibility:, names:)
  return unless VISIBILITY_MODIFIERS.include? visibility

  if names.any?
    children.each do |child|
      child.visibility = visibility if names.include?(child.name)
    end
  else
    self.tracked_visibility = visibility
  end
end