Class: Rubopolis::Cop::MigrationFilename

Inherits:
RuboCop::Cop::Base
  • Object
show all
Includes:
RuboCop::Cop::RangeHelp
Defined in:
lib/rubopolis/cop/migration_filename.rb

Overview

Examples:

# bad
Manipulating the filename timestamp to be the future

# good
System generated filename

Constant Summary collapse

MSG_FUTURE_TIMESTAMP =
'The name of this file (`%<basename>s`) should not use future timestamp.'

Instance Method Summary collapse

Instance Method Details

#for_bad_filename(file_path) {|source_range(processed_source.buffer, 1, 0), msg| ... } ⇒ Object

Yields:

  • (source_range(processed_source.buffer, 1, 0), msg)


28
29
30
31
32
33
34
35
36
37
# File 'lib/rubopolis/cop/migration_filename.rb', line 28

def for_bad_filename(file_path)
  basename = File.basename(file_path)

  filename_datetime = basename.split('_', 2).first
  current_timestamp = Time.now.strftime('%Y%m%d%H%M%S')

  msg = format(MSG_FUTURE_TIMESTAMP, basename: basename) if filename_datetime > current_timestamp

  yield source_range(processed_source.buffer, 1, 0), msg if msg
end

#on_new_investigationObject



20
21
22
23
24
25
26
# File 'lib/rubopolis/cop/migration_filename.rb', line 20

def on_new_investigation
  file_path = processed_source.file_path

  return unless file_path.include?('/db/')

  for_bad_filename(file_path) { |range, msg| add_offense(range, message: msg) }
end