class RubyLint::Command

Class used for running the CLI.

@!attribute [r] options

@return [Hash|Slop]

Constants

OPTION_MAPPING

@return [Hash]

Attributes

options[R]

Public Class Methods

format_names(name) click to toggle source

Formats the keys of a particular Hash stored on class level in {RubyLint::Configuration}.

@param [Symbol] name @return [String]

# File lib/ruby-lint/command.rb, line 27
def self.format_names(name)
  return "* #{Configuration.send(name).keys.sort.join("\n  * ")}"
end
new(options = {}) click to toggle source

@param [Hash|Slop] options

# File lib/ruby-lint/command.rb, line 34
def initialize(options = {})
  @options = options
end

Public Instance Methods

configure(configuration, options) click to toggle source

@param [RubyLint::Configuration] configuration @param [Hash] options

# File lib/ruby-lint/command.rb, line 94
def configure(configuration, options)
  OPTION_MAPPING.each do |key, setter|
    configuration.send(setter, options[key]) if options[key]
  end
end
extract_files(files) click to toggle source

Returns an Array containing the file paths that exist.

@param [Array] files @return [Array]

# File lib/ruby-lint/command.rb, line 86
def extract_files(files)
  return RubyLint::FileList.new.process(files)
end
load_configuration() click to toggle source

@return [RubyLint::Configuration]

# File lib/ruby-lint/command.rb, line 68
def load_configuration
  if options[:config]
    unless File.file?(options[:config])
      raise Errno::ENOENT, options[:config]
    end

    return Configuration.load_from_file([options[:config]])
  else
    return Configuration.load_from_file
  end
end
run(args) click to toggle source

Runs the command with the supplied arguments.

@param [Array] args

# File lib/ruby-lint/command.rb, line 43
def run(args)
  start_time    = Time.now.to_f
  files         = extract_files(args)
  configuration = load_configuration

  configure(configuration, options)

  runner    = Runner.new(configuration)
  output    = runner.analyze(files)
  exec_time = Time.now.to_f - start_time
  status    = 0

  unless output.empty?
    status = 1
    puts output
  end

  show_benchmark_info(exec_time) if options[:benchmark]

  exit(status)
end
show_benchmark_info(exec_time) click to toggle source

@param [Float] exec_time

# File lib/ruby-lint/command.rb, line 103
def show_benchmark_info(exec_time)
  mem_kb = `ps -o rss= #{Process.pid}`.strip.to_f
  mem_mb = mem_kb / 1024

  stderr "Execution time: #{exec_time.round(4)} seconds"
  stderr "Memory usage: #{mem_mb.round(2)} MB (#{mem_kb.round(2)} KB)"
end

Private Instance Methods

stderr(text) click to toggle source

@param [String] text

# File lib/ruby-lint/command.rb, line 116
def stderr(text)
  STDERR.puts(text)
end