Class: ClassSource::Index

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

Overview

An index of all source code available for a class

Instance Method Summary collapse

Constructor Details

#initialize(target_class, options = {}) ⇒ Index

Returns a new instance of Index.

Parameters:

  • options, (Hash)

    may include a :file => ‘path_to_expected_source’ param for tricky classes. Classes with no methods will need this hint to be sourced correctly.



6
7
8
9
# File 'lib/class_source/index.rb', line 6

def initialize(target_class, options = {})
  @target_class = target_class
  @options = options
end

Instance Method Details

#==(value) ⇒ Object

Convenience method for comparing sources as string values



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

def ==(value)
  to_s == value
end

#all(options = {}) ⇒ Hash

Returns a hash of source code fragments indexed by location

Parameters:

  • options (Hash) (defaults to: {})

    may contain a key :include_nested => false to return the source without the body of any nested classes

Returns:

  • (Hash)

    a hash of source code fragments with keys being a tuple in the form of [file_path, line_number]



23
24
25
# File 'lib/class_source/index.rb', line 23

def all(options={})
  @collator ||= Collator.new(@target_class, self).to_hash(options)
end

#class_methodsObject

Returns an index of all methods found for the class



47
48
49
# File 'lib/class_source/index.rb', line 47

def class_methods
  methods.klass
end

#filesObject

Returns an array of file containing relevant source code



59
60
61
# File 'lib/class_source/index.rb', line 59

def files
  locator.files
end

#locations(options = {}) ⇒ Array

Returns an array of source code locations

Returns:

  • (Array)

    an array of tuples in the form of [file_path, line_number]



29
30
31
# File 'lib/class_source/index.rb', line 29

def locations(options={})
  locator.to_a
end

#locatorObject

Returns an instance of the source locator object that searches out source locations



53
54
55
# File 'lib/class_source/index.rb', line 53

def locator
  @locator ||= Locator.new(@target_class, @options)
end

#methodsObject

Returns an index of all methods found for the class



41
42
43
# File 'lib/class_source/index.rb', line 41

def methods
  @method_details ||= MethodIndex.new(@target_class)
end

#to_s(options = {}) ⇒ String

This returns a string containing all the class’ source code. Order is not guaranteed to match evaluation order.

Parameters:

  • options (Hash) (defaults to: {})

    may contain a key :include_nested => false to return the source without the body of any nested classes

Returns:

  • (String)

    the joined value of all source code for the class



16
17
18
# File 'lib/class_source/index.rb', line 16

def to_s(options={})
  all(options).values.join("")
end