Class: CC::Engine::SourceFile

Inherits:
Object
  • Object
show all
Defined in:
lib/cc/engine/source_file.rb

Overview

Encapsulates a source file to be analyzed by HamlLint

Instance Method Summary collapse

Constructor Details

#initialize(io:, linter_config:, path:, root:) ⇒ SourceFile

Instantiates a new source file

Examples:

CC::Engine::SourceFile.new(
  io: StringIO.new,
  linter_config: ::HamlLint::ConfigurationLoader.default_configuration,
  path: "a.haml",
  root: "/tmp"
)

Parameters:

  • io (IO)

    the IO object to output the analysis to

  • linter_config (HamlLint::Configuration)

    the configuration for the HamlLint linters

  • path (String)

    the absolute path to the file to analyze

  • root (String)

    the root path of the file to analyze


27
28
29
30
31
32
# File 'lib/cc/engine/source_file.rb', line 27

def initialize(io:, linter_config:, path:, root:)
  @io = io
  @linter_config = linter_config
  @full_path = path
  @root = root
end

Instance Method Details

#pathString

The relative path to the file

Examples:

source_file = CC::Engine::SourceFile.new(
  io: StringIO.new,
  linter_config: ::HamlLint::ConfigurationLoader.default_configuration,
  path: "a.haml",
  root: "/tmp"
)
source_file.path #=> "a.haml"

Returns:

  • (String)

47
48
49
50
# File 'lib/cc/engine/source_file.rb', line 47

def path
  real_path = Pathname.new(root).realpath.to_s
  full_path.sub(%r{^#{real_path}/}, '')
end

#processvoid

This method returns an undefined value.

Processes the file with HamlLint and outputs to the configured IO

Examples:

source_file = CC::Engine::SourceFile.new(
  io: StringIO.new,
  linter_config: ::HamlLint::ConfigurationLoader.default_configuration,
  path: "a.haml",
  root: "/tmp"
)
source_file.process

65
66
67
68
69
70
# File 'lib/cc/engine/source_file.rb', line 65

def process
  ReportAdapter.new(report: runner.run(run_options), root: root).each do |issue|
    io.print issue.to_json
    io.print "\0"
  end
end