class Tailor::Critic

An object of this type provides for starting the process of critiquing files. It handles initializing the Ruler objects it needs based on the configuration given to it.

Public Instance Methods

check_file(file, style) click to toggle source

Adds problems found from Lexing to the #problems list.

@param [String] file The file to open, read, and check. @return [Hash] The Problems for that file.

# File lib/tailor/critic.rb, line 64
def check_file(file, style)
  log "<#{self.class}> Checking style of file: #{file}."
  lexer = Tailor::Lexer.new(file)
  ruler = Ruler.new
  log 'Style:'
  style.each { |property, values| log "#{property}: #{values}" }
  init_rulers(style, lexer, ruler)

  lexer.lex
  problems[file] = ruler.problems

  { file => problems[file] }
end
critique(file_sets) { |problems, label| ... } click to toggle source

The instance method that starts the process of looking for problems in files. It checks for problems in each file in each file set. It yields the problems found and the label they were found for.

@param [Hash] file_sets The file sets to critique. @yieldparam [Hash] problems The problems found for the label. @yieldparam [Symbol] label The label the problems were found for.

# File lib/tailor/critic.rb, line 23
def critique(file_sets)
  log "file sets keys: #{file_sets.keys}"

  file_sets.each do |label, file_set|
    log "Critiquing file_set: #{file_set}"

    file_set[:file_list].each do |file|
      log "Critiquing file: #{file}"

      begin
        problems = check_file(file, file_set[:style])
      rescue => ex
        $stderr.puts "Error while parsing file #{file}"
        raise(ex)
      end

      yield [problems, label] if block_given?
    end
  end
end
problem_count(type=nil) click to toggle source

@return [Fixnum] The number of problems found so far.

# File lib/tailor/critic.rb, line 52
def problem_count(type=nil)
  if type.nil?
    problems.values.flatten.size
  else
    problems.values.flatten.find_all { |v| v[:level] == :error }.size
  end
end
problems() click to toggle source

@return [Hash{String => Array}] The list of problems, where the keys are

the file names in which the problems were found, and the values are the
respective lists of problems for each file.
# File lib/tailor/critic.rb, line 47
def problems
  @problems ||= {}
end

Private Instance Methods

camelize(string) click to toggle source

Converts a snake-case String to a camel-case String.

@param [String] string The String to convert. @return [String] The converted String.

# File lib/tailor/critic.rb, line 114
def camelize(string)
  string.split(/_/).map { |word| word.capitalize }.join
end
init_rulers(style, lexer, parent_ruler) click to toggle source

Creates Rulers for each ruler given in style and adds the Ruler's defined observers to the given lexer.

@param [Hash] style The Hash that defines the style to be measured

against.

@param [Tailor::Lexer] lexer The Lexer object the Rulers should observe. @param [Tailor::Ruler] parent_ruler The main Ruler to add the child

Rulers to.
# File lib/tailor/critic.rb, line 88
def init_rulers(style, lexer, parent_ruler)
  style.each do |ruler_name, values|
    ruler = "Tailor::Rulers::#{camelize(ruler_name.to_s)}Ruler"

    if values.last[:level] == :off || values.last[:level] == 'off'
      msg = "Style option set to '#{values.last[:level]}'; "
      log msg << "skipping init of '#{ruler}'"
      next
    end

    log "Initializing ruler: #{ruler}"
    ruler = instance_eval("#{ruler}.new(#{values.first}, #{values.last})")
    parent_ruler.add_child_ruler(ruler)

    ruler.lexer_observers.each do |observer|
      log "Adding #{observer} to lexer..."
      meth = "add_#{observer}_observer".to_sym
      lexer.send(meth, ruler)
    end
  end
end