class Tailor::Rulers::SpacesBeforeLbraceRuler

Detects spaces before a +{+ as given by +@config+. It skips checking when:

Public Class Methods

new(config, options) click to toggle source
Calls superclass method Tailor::Ruler::new
# File lib/tailor/rulers/spaces_before_lbrace_ruler.rb, line 13
def initialize(config, options)
  super(config, options)
  add_lexer_observers :lbrace
end

Public Instance Methods

count_spaces(lexed_line, column) click to toggle source

Counts the spaces before the '{'.

@param [LexedLine] lexed_line @param [Fixnum] column @return [Fixnum] The number of spaces before the lbrace.

# File lib/tailor/rulers/spaces_before_lbrace_ruler.rb, line 23
def count_spaces(lexed_line, column)
  current_index = lexed_line.event_index(column)
  log "Current event index: #{current_index}"
  previous_event = lexed_line.at(current_index - 1)
  log "Previous event: #{previous_event}"

  if column.zero? || previous_event.nil?
    log 'lbrace must be at the beginning of the line.'
    @do_measurement = false
    return 0
  end

  if previous_event[1] == :on_embexpr_beg
    log "lbrace comes after a '\#{'."
    @do_measurement = false
    return 0
  end

  if previous_event[1] == :on_lparen
    log "lbrace comes after a '('."
    @do_measurement = false
    return 0
  end

  if previous_event[1] == :on_lbracket
    log "lbrace comes after a '['."
    @do_measurement = false
    return 0
  end

  return 0 if previous_event[1] != :on_sp

  if current_index - 2 < 0
    log 'lbrace comes at the beginning of an indented line.'
    @do_measurement = false
    return previous_event.last.size
  end

  previous_event.last.size
end
lbrace_update(lexed_line, lineno, column) click to toggle source

Called by {Lexer} when :on_lbrace is encountered.

@param [LexedLine] lexed_line @param [Fixnum] lineno @param [Fixnum] column

# File lib/tailor/rulers/spaces_before_lbrace_ruler.rb, line 69
def lbrace_update(lexed_line, lineno, column)
  count = count_spaces(lexed_line, column)
  log "Found #{count} space(s) before lbrace."

  if !@do_measurement
    log 'Skipping measurement.'
  else
    measure(count, lineno, column)
  end

  @do_measurement = true
end
measure(actual_spaces, lineno, column) click to toggle source

Checks to see if the counted spaces before an lbrace equals the value at +@config+.

@param [Fixnum] actual_spaces The number of spaces before the lbrace. @param [Fixnum] lineno Line the potential problem is on. @param [Fixnum] column Column the potential problem is on.

# File lib/tailor/rulers/spaces_before_lbrace_ruler.rb, line 88
def measure(actual_spaces, lineno, column)
  if actual_spaces != @config
    msg = "Line has #{actual_spaces} space(s) before a {, "
    msg << "but should have #{@config}."

    @problems << Problem.new(problem_type, lineno, column, msg,
      @options[:level])
  end
end