Module: VisibilityChecker

Defined in:
lib/visibility_checker.rb

Defined Under Namespace

Classes: VisibilityChange

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.visibility_changes(klass, stop_at: Object, skip_owners: [Kernel]) ⇒ Object

Return an array of VisibilityChanges for any changes in method visibility in any of the the ancestors of klass before stop_at, unless the owner of the method is in skip_owners. This will be an empty array if there are no visibility changes.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/visibility_checker.rb', line 8

def self.visibility_changes(klass, stop_at: Object, skip_owners: [Kernel])
  meths = {}
  changes = []

  klass.ancestors.each do |mod|
    break if mod == stop_at

    [:public, :private, :protected].each do |vis_type|
      mod.send("#{vis_type}_instance_methods").each do |meth|
        prev_vis_type, prev_mod = meths[meth]
        unless prev_vis_type.nil? || prev_vis_type == vis_type
          owner = mod.instance_method(meth).owner
          unless skip_owners.include?(owner)
            changes << VisibilityChange.new(meth, mod.instance_method(meth).owner, vis_type, prev_mod, prev_vis_type)
          end
        end
        meths[meth] = [vis_type, mod]
      end
    end
  end

  changes
end

Instance Method Details

#visibility_changesObject

Detect visibility changes in the receiver’s ancestors. This should only be used if you are extending a class or module with VisibilityChecker or including it in Class or Module.



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

def visibility_changes
  VisibilityChecker.visibility_changes(self)
end