Class: ClassSource::MethodIndex

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

Overview

An index of all the methods in the target class

Instance Method Summary collapse

Constructor Details

#initialize(target_class) ⇒ MethodIndex

Returns a new instance of MethodIndex.



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

def initialize(target_class)
  @target_class = target_class
end

Instance Method Details

#all(options = {}) ⇒ Array

Returns An array of method names for all instance methods in the class.

Returns:

  • (Array)

    An array of method names for all instance methods in the class



29
30
31
32
33
34
35
# File 'lib/class_source/method_index.rb', line 29

def all(options={})
  include_inherited_methods = options.has_key?(:include_inherited_methods) ? options[:include_inherited_methods] : true
  target = options[:target] || @target_class
  target.public_instance_methods(include_inherited_methods) +
    target.private_instance_methods(include_inherited_methods) +
    target.protected_instance_methods(include_inherited_methods)
end

#klassClassSource::ClassMethodIndex

Returns A index of class methods.

Returns:



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

def klass
  ClassMethodIndex.new(@target_class)
end

#locationsArray

Returns An array of [file_path, line_number] tuples for all unique methods of the class.

Returns:

  • (Array)

    An array of [file_path, line_number] tuples for all unique methods of the class



10
11
12
13
14
15
16
# File 'lib/class_source/method_index.rb', line 10

def locations
  @locations ||= (unique.map do |m|
    @target_class.instance_method(m).source_location
  end + klass.unique.map do |m|
    @target_class.method(m).source_location
  end).compact
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.



19
20
21
22
23
24
25
# File 'lib/class_source/method_index.rb', line 19

def unique
  uniquely_named_methods = all(:include_inherited_methods => false)
  overridden_methods = (all - uniquely_named_methods).select do |m|
    @target_class.instance_method(m).source_location != @target_class.superclass.instance_method(m).source_location
  end
  overridden_methods + uniquely_named_methods
end