Class: ClassSource::Collator
- Inherits:
-
Object
- Object
- ClassSource::Collator
- Defined in:
- lib/class_source/collator.rb
Instance Method Summary collapse
- #full_file(location) ⇒ Object
-
#initialize(target_class, index) ⇒ Collator
constructor
A new instance of Collator.
- #nested_class_line_ranges ⇒ Object
-
#source_helper(source_location) ⇒ Object
source_helper and valid_expression? are lifted from method_source © 2011 John Mair (banisterfiend).
- #to_hash(options = {}) ⇒ Object
- #valid_expression?(code) ⇒ Boolean
Constructor Details
#initialize(target_class, index) ⇒ Collator
Returns a new instance of Collator.
5 6 7 8 |
# File 'lib/class_source/collator.rb', line 5 def initialize(target_class, index) @klass = target_class @source = index end |
Instance Method Details
#full_file(location) ⇒ Object
47 48 49 |
# File 'lib/class_source/collator.rb', line 47 def full_file(location) File.read(location.first) end |
#nested_class_line_ranges ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/class_source/collator.rb', line 32 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.each do |klass| # (klass.source_location.last-1)..(klass.source_location.last + klass.source.lines.count - 2) klass.__source__.all.each do |(file, line), source| @nested_class_ranges[file] ||= [] @nested_class_ranges[file] << ((line - 1)..(line + source.lines.count - 2)) end end @nested_class_ranges end |
#source_helper(source_location) ⇒ Object
source_helper and valid_expression? are lifted from method_source © 2011 John Mair (banisterfiend)
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/class_source/collator.rb', line 53 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 |
#to_hash(options = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/class_source/collator.rb', line 10 def to_hash( = {}) full_sources = @source.locations().inject({}) do |results, location| results[ location ] = source_helper(location) results end return full_sources unless [:include_nested] == false full_sources.inject({}) do |clean_sources, (location, source)| if nested_class_line_ranges[location.first] complete_file = full_file(location) target_range = (location.last - 1)..(location.last + source.lines.count - 2) clean_sources[location] = 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("") else clean_sources[location] = source end clean_sources end end |
#valid_expression?(code) ⇒ Boolean
71 72 73 74 75 76 77 |
# File 'lib/class_source/collator.rb', line 71 def valid_expression?(code) RubyParser.new.parse(code) rescue Racc::ParseError, SyntaxError false else true end |