Class: PreCommit::RubySymbolHashrockets

Inherits:
Object
  • Object
show all
Defined in:
lib/pre-commit/checks/ruby_symbol_hashrockets.rb

Constant Summary collapse

HASHROCKET_PATTERN =
'[^:](:{1}(?:\$|@|@@|[_A-Za-z])?\w*[=!?]?\s*=>\s*)'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#error_messageObject

Returns the value of attribute error_message.



6
7
8
# File 'lib/pre-commit/checks/ruby_symbol_hashrockets.rb', line 6

def error_message
  @error_message
end

#staged_filesObject

Returns the value of attribute staged_files.



6
7
8
# File 'lib/pre-commit/checks/ruby_symbol_hashrockets.rb', line 6

def staged_files
  @staged_files
end

Class Method Details

.callObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/pre-commit/checks/ruby_symbol_hashrockets.rb', line 10

def self.call
  check = new
  check.staged_files = Utils.staged_files('*')
  result = check.run

  if !result
    $stderr.puts check.error_message
    $stderr.puts
    $stderr.puts 'pre-commit: You can bypass this check using `git commit -n`'
    $stderr.puts
  end

  result
end

Instance Method Details

#detected_bad_code?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/pre-commit/checks/ruby_symbol_hashrockets.rb', line 41

def detected_bad_code?
  violations[:success]
end

#runObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pre-commit/checks/ruby_symbol_hashrockets.rb', line 25

def run
  # There is nothing to check
  return true if staged_files.empty?

  if detected_bad_code?
    @error_message = "pre-commit: detected :symbol => value hashrocket:\n"
    @error_message += violations[:lines]

    @passed = false
  else
    @passed = true
  end

  @passed
end

#violationsObject



45
46
47
48
49
50
51
52
# File 'lib/pre-commit/checks/ruby_symbol_hashrockets.rb', line 45

def violations
  @violations ||= begin
    lines = `#{Utils.grep} '#{HASHROCKET_PATTERN}' #{staged_files}`
    success = $?.exitstatus == 0

    { :lines => lines, :success => success}
  end
end