Class: Rubycritic::FileGenerator

Inherits:
BaseGenerator show all
Defined in:
lib/rubycritic/report_generators/file_generator.rb

Constant Summary collapse

LINE_NUMBER_OFFSET =
1
TEMPLATE =
erb_template("file.html.erb")

Constants inherited from BaseGenerator

BaseGenerator::LAYOUT_TEMPLATE, BaseGenerator::TEMPLATES_DIR

Instance Method Summary collapse

Methods inherited from BaseGenerator

erb_template, #file_href, #file_pathname, #get_binding

Methods included from ViewHelpers

#analysed_file_name, #asset_path, #code_index_path, #javascript_path, #overview_path, #root_directory, #smell_location_path, #smells_index_path, #stylesheet_path

Constructor Details

#initialize(pathname, smells) ⇒ FileGenerator



11
12
13
14
# File 'lib/rubycritic/report_generators/file_generator.rb', line 11

def initialize(pathname, smells)
  @pathname = pathname
  @smells = smells
end

Instance Method Details

#file_directoryObject



16
17
18
# File 'lib/rubycritic/report_generators/file_generator.rb', line 16

def file_directory
  File.join(root_directory, @pathname.dirname)
end

#file_has_smells?Boolean



36
37
38
# File 'lib/rubycritic/report_generators/file_generator.rb', line 36

def file_has_smells?
  !@smells.empty?
end

#file_nameObject



20
21
22
# File 'lib/rubycritic/report_generators/file_generator.rb', line 20

def file_name
  @pathname.basename.sub_ext(".html")
end

#renderObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rubycritic/report_generators/file_generator.rb', line 24

def render
  file_code = ""
  File.readlines(@pathname).each.with_index(LINE_NUMBER_OFFSET) do |line_text, line_number|
    location = Location.new(@pathname, line_number)
    line_smells = @smells.select { |smell| smell.located_in?(location) }
    file_code << LineGenerator.new(line_text, line_smells).render
  end

  file_body = TEMPLATE.result(get_binding { file_code })
  LAYOUT_TEMPLATE.result(get_binding { file_body })
end