Class: RDocRuboCop::Lang::Base::SourceFile

Inherits:
Object
  • Object
show all
Defined in:
lib/rdoc_rubocop/lang/base/source_file.rb

Direct Known Subclasses

C::SourceFile, Ruby::SourceFile

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, filename) ⇒ SourceFile

Returns a new instance of SourceFile.



23
24
25
26
27
28
# File 'lib/rdoc_rubocop/lang/base/source_file.rb', line 23

def initialize(source, filename)
  @source = source
  @filename = filename

  reset
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



8
9
10
# File 'lib/rdoc_rubocop/lang/base/source_file.rb', line 8

def filename
  @filename
end

#sourceObject (readonly)

Returns the value of attribute source.



7
8
9
# File 'lib/rdoc_rubocop/lang/base/source_file.rb', line 7

def source
  @source
end

Class Method Details

.build(filename) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rdoc_rubocop/lang/base/source_file.rb', line 10

def self.build(filename)
  klass =
    case filename
    when /\.c\z/ then Lang::C::SourceFile
    when /\.rb\z/ then Lang::Ruby::SourceFile
    end

  return unless klass

  source = File.open(filename, "r").read
  klass.new(source, filename)
end

Instance Method Details

#commentsObject



37
38
39
40
41
42
43
44
# File 'lib/rdoc_rubocop/lang/base/source_file.rb', line 37

def comments
  @comments ||=
    begin
      comment_extractor = comment_extractor_class.new(self)
      comment_extractor.extract
      comment_extractor.comments
    end
end

#correctObject



56
57
58
59
60
61
# File 'lib/rdoc_rubocop/lang/base/source_file.rb', line 56

def correct
  corrector = corrector_class.new(self)
  corrector.correct

  @source = corrector.corrected_source
end

#correct!Object

def comment_extractor_class

CommentExtractor

end



50
51
52
53
54
# File 'lib/rdoc_rubocop/lang/base/source_file.rb', line 50

def correct!
  correct
  save if changed?
  reset
end

#source_code_file_pathsObject



30
31
32
33
34
35
# File 'lib/rdoc_rubocop/lang/base/source_file.rb', line 30

def source_code_file_paths
  @source_code_file_paths ||=
    comments.
      flat_map(&:source_codes).
      map { |source_code| source_code.build_file_path(@filename) }
end