class RBT::Cookbooks::ConvertDateFormat

Public Class Methods

[](i = ARGV) click to toggle source
#

RBT::Cookbooks::ConvertDateFormat[]

#
# File lib/rbt/cookbooks/convert_date_format.rb, line 165
def self.[](i = ARGV)
  new(i)
end
new( i = ARGV, run_already = true ) click to toggle source
#

initialize

#
# File lib/rbt/cookbooks/convert_date_format.rb, line 23
def initialize(
    i           = ARGV,
    run_already = true
  )
  reset
  set_commandline_arguments(i)
  run if run_already
end

Public Instance Methods

create_the_log_directory_for_this_class() click to toggle source
#

create_the_log_directory_for_this_class

#
# File lib/rbt/cookbooks/convert_date_format.rb, line 140
def create_the_log_directory_for_this_class
  mkdir_p(@base_directory_for_the_date_converted_yaml_files)
end
do_process_this_file(i) click to toggle source
#

do_process_this_file

#
# File lib/rbt/cookbooks/convert_date_format.rb, line 48
def do_process_this_file(i)
  _ = default_read(i)
  if _.include? 'last_update:'
    last_update_entry = _.scan(/ last_update: (.*)$/)
    if last_update_entry.is_a? Array
      last_update_entry = last_update_entry.flatten.first
    end
    if is_this_date_already_in_the_correct_format?(last_update_entry, i)
      opne 'All is fine - nothing has to be changed. '+steelblue('\o/')
    else
      do_change_this_file(i, last_update_entry)
    end
  else
    e 'No last_update entry exists in the file at '+sfile(i)+'.'
  end
end
is_this_date_already_in_the_correct_format?( i, optional_this_file = nil ) click to toggle source
#

is_this_date_already_in_the_correct_format?

#
# File lib/rbt/cookbooks/convert_date_format.rb, line 68
def is_this_date_already_in_the_correct_format?(
    i, optional_this_file = nil
  )
  split_char_to_be_used = ' '
  if i.include?('.') and !i.include?(' ')
    split_char_to_be_used = '.'
  end
  splitted = i.split(split_char_to_be_used)
  month_entry = splitted[1]
  if month_entry =~ /^\d{1,2}$/ # Only numbers.
    opne tomato('Only numbers were returned for the entry '+optional_this_file.to_s+
         '. Changing this to')
    opne tomato('an incorrect date deliberately so.')
    month_entry = return_month_based_on_this_number(month_entry)
  end
  months?.include?(month_entry)
end
reset() click to toggle source
#

reset (reset tag)

#
Calls superclass method RBT::Base#reset
# File lib/rbt/cookbooks/convert_date_format.rb, line 35
def reset
  super()
  infer_the_namespace
  # ===================================================================== #
  # === @base_directory_for_the_date_converted_yaml_files
  # ===================================================================== #
  @base_directory_for_the_date_converted_yaml_files = rbt_log_dir?+
                                                      'date_converted_yaml_files/'
end
run() click to toggle source
#

run (run tag)

#
# File lib/rbt/cookbooks/convert_date_format.rb, line 147
def run
  create_the_log_directory_for_this_class
  _ = commandline_arguments?
  # ======================================================================= #
  # Operate in a batch-centric manner next:
  # ======================================================================= #
  _.each {|this_file|
    if this_file and File.exist?(this_file)
      do_process_this_file(this_file)
    else
      opnn; no_file_exists_at(this_file)
    end
  }
end

Private Instance Methods

do_change_this_file( i, last_update_entry ) click to toggle source
#

do_change_this_file

The first argument to this method is the name of the locally existing file.

When this method is called then we can be certain that there already was a prior check, determining that this file HAS to be modified.

#
# File lib/rbt/cookbooks/convert_date_format.rb, line 95
def do_change_this_file(
    i, last_update_entry
  )
  # ===================================================================== #
  # First, we have to determine the correct date.
  # ===================================================================== #
  months = months?
  new_date = last_update_entry.sub(//, '') # From "Mar" to "March", for instance.

  split_char_to_be_used = ' '
  if last_update_entry.include?('.') and !last_update_entry.include?(' ')
    split_char_to_be_used = '.'
  end
  splitted = last_update_entry.split(split_char_to_be_used)
  month_entry = splitted[1]
  new_month = months.select {|entry|
    entry.start_with?(month_entry)
  }.first
  if new_month.nil?
    opne 'No new month entry was found for the file '+sfile(i)+'.'
    opne 'Please fix the .yml file.'
    exit
  end
  new_date = splitted[0]+' '+new_month+' '+splitted[2]
  dataset = default_read(i)
  dataset.gsub!(/ last_update: (.+)$/, " last_update: #{new_date}")
  # ======================================================================= #
  # First, create a backup of the file:
  # ======================================================================= #
  target = @base_directory_for_the_date_converted_yaml_files+
           'backup_of_'+File.basename(i)
  opne 'Creating a backup of the file at '+sfile(i)
  opne 'towards '+sfile(target)
  copy_file(i, target)
  opne 'Now modifying the file '+sfile(i)+'. The '\
       'new date is '+tomato(new_date)+'.'
  # ======================================================================= #
  # Now we can modify the original file as-is:
  # ======================================================================= #
  write_what_into(dataset, i)
end