Class: CC::Engine::Location

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

Overview

Represents a location in a source file

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line:, path:) ⇒ Location

Instantiates a new location

Examples:

CC::Engine::Location.new(line: 42, path: "a.haml")

Parameters:

  • line (Integer)

    the line number in the file

  • path (String)

    the absolute path to the file


38
39
40
41
# File 'lib/cc/engine/location.rb', line 38

def initialize(line:, path:)
  @line = line
  @path = path
end

Instance Attribute Details

#lineInteger (readonly)

The line in the file

Examples:

location = CC::Engine::Location.new(line: 42, path: "a.haml")
location.line  #=> 42

Returns:

  • (Integer)

51
52
53
# File 'lib/cc/engine/location.rb', line 51

def line
  @line
end

#pathString (readonly)

The absolute path to the file

Examples:

location = CC::Engine::Location.new(line: 42, path: "a.haml")
location.path  #=> "a.haml"

Returns:

  • (String)

61
62
63
# File 'lib/cc/engine/location.rb', line 61

def path
  @path
end

Class Method Details

.from_haml_lint(location:, path:, root:) ⇒ CC::Engine::Location

Converts from the HamlLint location storage to Code Climate format

Examples:

CC::Engine::Location.from_haml_lint(
  location: {line: 42}
  path: "a.haml",
  root: "/tmp"
)

Parameters:

  • location (Hash)

    a Hash with a :line key

  • path (String)

    the absolute path to the file

  • root (String)

    the root path for the file

Returns:


25
26
27
28
# File 'lib/cc/engine/location.rb', line 25

def self.from_haml_lint(location:, path:, root:)
  root = Pathname.new(root).realpath.to_s
  new(line: location[:line], path: path.sub(%r{^#{root}/}, ''))
end

Instance Method Details

#to_hHash

Converts the location to the Code Climate hash format

Examples:

location = CC::Engine::Location.new(line: 42, path: "a.haml")
location.to_h  #=> {path: "a.haml", lines: {begin: 42, end: 42}}

Returns:

  • (Hash)

71
72
73
74
75
76
77
78
79
# File 'lib/cc/engine/location.rb', line 71

def to_h
  {}.tap do |hash|
    hash[:path] = path
    hash[:lines] = {}.tap do |lines|
      lines[:begin] = line
      lines[:end] = line
    end
  end
end

#to_json(*args) ⇒ String

Converts the location to the Code Climate JSON format

Examples:

location = CC::Engine::Location.new(line: 42, path: "a.haml")
location.to_json  #=> '{"path": "a.haml", "lines": {"begin": 42, "end": 42"}}'

Returns:

  • (String)

    a JSON document of the location


89
90
91
# File 'lib/cc/engine/location.rb', line 89

def to_json(*args)
  to_h.to_json(*args)
end