Class: ClassSource::Collator

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

Overview

Responsible for collating class source information into a clear readable hash of source values.

Instance Method Summary collapse

Constructor Details

#initialize(target_class, index) ⇒ Collator

Returns a new instance of Collator.



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

def initialize(target_class, index)
  @klass = target_class
  @source = index
end

Instance Method Details

#full_file(location) ⇒ Object

A helper to return the full text of a file



62
63
64
# File 'lib/class_source/collator.rb', line 62

def full_file(location)
  File.read(location.first)
end

#nested_class_line_rangesObject

Returns a hash of nested data within the class source based on existing class constants.

Returns:

  • a hash of nested data within the class source based on existing class constants



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/class_source/collator.rb', line 48

def nested_class_line_ranges
  nested_classes = @klass.constants.select { |c| @klass.const_get(c).is_a?(Class) }.map {|c| @klass.const_get(c) }
  return @nested_class_ranges if @nested_class_ranges
  @nested_class_ranges = nested_classes.inject({}) do |ranges, nested_klass| 
    nested_klass.__source__.all.each do |(file, line), source| 
      ranges[file] ||= []
      ranges[file] << ((line - 1)..(line + source.lines.count - 2))
    end
    ranges
  end
end

#source_helper(source_location) ⇒ Object

source_helper and valid_expression? are from the method_source gem © 2011 John Mair (banisterfiend)



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/class_source/collator.rb', line 68

def source_helper(source_location)
  return nil if !source_location.is_a?(Array)

  file_name, line = source_location
  File.open(file_name) do |file|
    (line - 1).times { file.readline }

    code = ""
    loop do
      val = file.readline
      code << val

      return code if valid_expression?(code)
    end
  end
end

#source_without_nesting(location, source) ⇒ Hash

Returns A source string with the contained nested class values removed.

Returns:

  • (Hash)

    A source string with the contained nested class values removed



37
38
39
40
41
42
43
44
# File 'lib/class_source/collator.rb', line 37

def source_without_nesting(location, source)
  complete_file = full_file(location)
  target_range =  (location.last - 1)..(location.last + source.lines.count - 2)
  complete_file.lines.to_a.select.with_index do |line, index|
    target_range.include?(index) &&
    nested_class_line_ranges[location.first].all? { |range| !range.include?(index) }
  end.join("")
end

#sources_without_nestingHash

Returns A hash of sources filtered for the source of any nested classes.

Returns:

  • (Hash)

    A hash of sources filtered for the source of any nested classes



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/class_source/collator.rb', line 23

def sources_without_nesting
  @source.locations.inject({}) do |clean_sources, location|
    source = source_helper(location)
    if nested_class_line_ranges[location.first]
      clean_sources[location] = source_without_nesting(location, source)
    else
      clean_sources[location] = source
    end
    clean_sources
  end
end

#to_hash(options = {}) ⇒ Hash

Returns A hash with keys of [file_path, line_number] tuples pointing to values of source code segments.

Returns:

  • (Hash)

    A hash with keys of [file_path, line_number] tuples pointing to values of source code segments



12
13
14
15
16
17
18
19
# File 'lib/class_source/collator.rb', line 12

def to_hash(options = {})
  return sources_without_nesting if options[:include_nested] == false
  @source.locations(options).inject({}) do |results, location|
    results[ location ] = source_helper(location)
    results
  end

end

#valid_expression?(code) ⇒ Boolean

source_helper and valid_expression? are from the method_source gem © 2011 John Mair (banisterfiend)

Returns:

  • (Boolean)


87
88
89
90
91
92
93
# File 'lib/class_source/collator.rb', line 87

def valid_expression?(code)
  RubyParser.new.parse(code)
rescue Racc::ParseError, SyntaxError
  false
else
  true
end