Class: RubyCritic::Generator::Html::CodeFile

Inherits:
Base
  • Object
show all
Defined in:
lib/rubycritic/generators/html/code_file.rb

Constant Summary collapse

LINE_NUMBER_OFFSET =
1
TEMPLATE =
erb_template('code_file.html.erb')

Constants inherited from Base

Base::LAYOUT_TEMPLATE, Base::TEMPLATES_DIR

Instance Method Summary collapse

Methods inherited from Base

erb_template, #file_href, #file_pathname

Methods included from ViewHelpers

#asset_path, #code_index_path, #file_path, #smell_location_path, #timeago_tag

Constructor Details

#initialize(analysed_module) ⇒ CodeFile

Returns a new instance of CodeFile.


13
14
15
16
17
# File 'lib/rubycritic/generators/html/code_file.rb', line 13

def initialize(analysed_module)
  @analysed_module = analysed_module
  @pathname = @analysed_module.pathname
  set_header_links if Config.compare_branches_mode?
end

Instance Method Details

#file_directoryObject


25
26
27
# File 'lib/rubycritic/generators/html/code_file.rb', line 25

def file_directory
  @file_directory ||= root_directory + @pathname.dirname
end

#file_locationObject


33
34
35
# File 'lib/rubycritic/generators/html/code_file.rb', line 33

def file_location
  @pathname.sub_ext('.html')
end

#file_nameObject


29
30
31
# File 'lib/rubycritic/generators/html/code_file.rb', line 29

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

#renderObject


37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubycritic/generators/html/code_file.rb', line 37

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 = @analysed_module.smells_at_location(location)
    file_code << Line.new(file_directory, line_text, line_smells).render
  end

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

19
20
21
22
23
# File 'lib/rubycritic/generators/html/code_file.rb', line 19

def set_header_links
  @base_path = code_index_path(Config.base_root_directory, file_location)
  @feature_path = code_index_path(Config.feature_root_directory, file_location)
  @build_path = code_index_path(Config.compare_root_directory, file_location)
end