Class: Kamaze::Project::Tools::Rubocop

Inherits:
BaseTool show all
Includes:
Concern::Cli::WithExitOnFailure
Defined in:
lib/kamaze/project/tools/rubocop.rb

Overview

Tool to run Rubocop::CLI

Sample of use:

Rubocop.new.prepare do |c|
  c.patterns = ["#{Dir.pwd}/test/cs.rb"]
  c.options = ['--fail-level', 'E']
end.run

Defined Under Namespace

Classes: Arguments, Config

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from Kamaze::Project::Tools::BaseTool

Instance Attribute Details

#argumentsArguments

Arguments used by CLI (during execution/run)

Returns:



62
63
64
65
66
67
68
69
70
# File 'lib/kamaze/project/tools/rubocop.rb', line 62

def arguments
  @arguments = defaults.clone if @arguments.to_a.size.zero?

  if caller_locations(1..1).first.path == __FILE__
    @arguments
  else
    @arguments.clone.freeze
  end
end

#defaultsArguments

Default arguments used by Rubocop::CLI

Returns:



38
39
40
# File 'lib/kamaze/project/tools/rubocop.rb', line 38

def defaults
  @defaults
end

#observer_peersHash|nil (readonly, protected) Originally defined in module Concern::Observable

Returns:

  • (Hash|nil)

Instance Method Details

#coreRuboCop::CLI (protected)

Returns:

  • (RuboCop::CLI)


115
116
117
# File 'lib/kamaze/project/tools/rubocop.rb', line 115

def core
  RuboCop::CLI.new
end

#failure?Boolean Also known as: failed? Originally defined in module Concern::Cli

Denote execution is a failure.

Returns:

  • (Boolean)

#mutable_attributesObject



40
41
42
# File 'lib/kamaze/project/tools/rubocop.rb', line 40

def mutable_attributes
  [:defaults]
end

#prepareObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/kamaze/project/tools/rubocop.rb', line 44

def prepare
  self.tap do
    reset

    if block_given?
      Config.new.tap do |config|
        yield(config)
        arguments.concat(config.freeze.to_a)
      end
    end

    arguments.freeze
  end
end

#resetself (protected)

Reset arguments + retcode

Returns:

  • (self)


107
108
109
110
111
112
# File 'lib/kamaze/project/tools/rubocop.rb', line 107

def reset
  self.tap do
    @arguments = nil
    self.retcode = nil if retcode.to_i.zero?
  end
end

#retcodeFixnum Originally defined in module Concern::Cli

Status code usable to eventually initiates the termination.

Returns:

  • (Fixnum)

#runself

Returns:

  • (self)

Raises:

  • (SystemExit)


83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/kamaze/project/tools/rubocop.rb', line 83

def run
  prepare if arguments.to_a.empty?

  if runnable?
    with_exit_on_failure do
      core.run(arguments.to_a).tap { |retcode| self.retcode = retcode }
      reset
    end
  end

  self
end

#runnable?Boolean

Denote runnable

When last argument is -- we suppose there is no files

Returns:

  • (Boolean)


77
78
79
# File 'lib/kamaze/project/tools/rubocop.rb', line 77

def runnable?
  '--' != arguments.last
end

#setupObject (protected)



100
101
102
# File 'lib/kamaze/project/tools/rubocop.rb', line 100

def setup
  @defaults = Arguments.new((@defaults || ['--only-recognized-file-types']).to_a)
end

#success?Boolean Also known as: successful? Originally defined in module Concern::Cli

Denote execution is a success.

Returns:

  • (Boolean)

#with_exit_on_failure {|Object| ... } ⇒ Object (protected) Originally defined in module Concern::Cli::WithExitOnFailure

Initiates termination by raising SystemExit exception depending on success of given block.

Yields:

Yield Parameters:

  • (self)

Returns:

Raises:

  • (SystemExit)