Class: ClassSource::ClassMethodIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/class_source/class_method_index.rb

Overview

An index of all class methods available for a class

Instance Method Summary collapse

Constructor Details

#initialize(target_class) ⇒ ClassMethodIndex

Returns a new instance of ClassMethodIndex.



4
5
6
# File 'lib/class_source/class_method_index.rb', line 4

def initialize(target_class)
  @target_class = target_class
end

Instance Method Details

#ancestral_sources(method) ⇒ Array

Returns An array of ancestral sources for a given method.

Returns:

  • (Array)

    An array of ancestral sources for a given method



33
34
35
# File 'lib/class_source/class_method_index.rb', line 33

def ancestral_sources(method)
  superclasses.map { |mod| mod.respond_to?(method) && mod.method(method).source_location }.compact
end

#extendedArray

Returns An array of method names for all methods included into a class via class extension.

Returns:

  • (Array)

    An array of method names for all methods included into a class via class extension.



14
15
16
17
18
# File 'lib/class_source/class_method_index.rb', line 14

def extended
  @target_class.singleton_class.ancestors.map do |mod|
    mod.instance_methods.select { |m| mod.instance_method(m).source_location == @target_class.method(m).source_location }
  end.flatten
end

#overriddenArray

Returns An array of method names with new source in this class vs its ancestors.

Returns:

  • (Array)

    An array of method names with new source in this class vs its ancestors



26
27
28
29
30
# File 'lib/class_source/class_method_index.rb', line 26

def overridden
  (@target_class.methods - uniquely_named).select do |m|
    !ancestral_sources(m).include?(@target_class.method(m).source_location)
  end
end

#superclassesObject

All ancestors of the target class @return An array of classes and modules



39
40
41
# File 'lib/class_source/class_method_index.rb', line 39

def superclasses
  @target_class.ancestors - [@target_class]
end

#uniqueArray

Returns An array of method names unique to or overridden in this class, not inherited from its ancestors or singleton_class ancestors.

Returns:

  • (Array)

    An array of method names unique to or overridden in this class, not inherited from its ancestors or singleton_class ancestors.



9
10
11
# File 'lib/class_source/class_method_index.rb', line 9

def unique
  uniquely_named + overridden - extended
end

#uniquely_namedArray

Returns An array of method names introduced for the first time in the current class.

Returns:

  • (Array)

    An array of method names introduced for the first time in the current class



21
22
23
# File 'lib/class_source/class_method_index.rb', line 21

def uniquely_named
  @target_class.singleton_methods(false)
end