class CucumberLint::LintedFile

A class that represents a file being linted

Attributes

content[R]
errors[R]
lines[R]

Public Class Methods

new(path) click to toggle source
# File lib/cucumber_lint/linted_file.rb, line 9
def initialize path
  @path = Pathname.new path
  @content = IO.read path
  @lines = @content.lines

  @errors = []
  @fixes = {}
  @marked_for_deletion = false
end

Public Instance Methods

add_error(error) click to toggle source
# File lib/cucumber_lint/linted_file.rb, line 20
def add_error error
  @errors << "#{@path}:#{error}"
end
add_fix(line_number, fix) click to toggle source
# File lib/cucumber_lint/linted_file.rb, line 25
def add_fix line_number, fix
  @fixes[line_number] ||= []
  @fixes[line_number] += Array(fix)
end
mark_for_deletion() click to toggle source
# File lib/cucumber_lint/linted_file.rb, line 31
def mark_for_deletion
  @marked_for_deletion = true
end
resolve() click to toggle source
# File lib/cucumber_lint/linted_file.rb, line 36
def resolve
  if @marked_for_deletion
    delete
    :deleted
  elsif !errors.empty? || fixable?
    fix
    errors.empty? ? :written : :failed
  else
    :passed
  end
end

Private Instance Methods

apply_fixes() click to toggle source
# File lib/cucumber_lint/linted_file.rb, line 52
def apply_fixes
  lines.each_with_index.map do |line, index|
    line_number = index + 1

    @fixes.fetch(line_number, []).each do |fix|
      line = fix.call(line)
    end

    line
  end
end
delete() click to toggle source
# File lib/cucumber_lint/linted_file.rb, line 65
def delete
  @path.delete
end
fix() click to toggle source
# File lib/cucumber_lint/linted_file.rb, line 70
def fix
  IO.write @path, apply_fixes.join
end
fixable?() click to toggle source
# File lib/cucumber_lint/linted_file.rb, line 74
def fixable?
  !@fixes.empty?
end