Class: Adder

Inherits:
Object
  • Object
show all
Extended by:
Logging
Defined in:
lib/adder.rb

Constant Summary collapse

@@log =
self::init_logger

Class Method Summary collapse

Methods included from Logging

init_logger, log_label=, log_level=, log_target=

Methods included from File_Checking

#file_check, file_check

Class Method Details

.add(options) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/adder.rb', line 36

def self::add(options) 
  @log.debug('options are ' << options.to_s)
  if options.file
    @@events_file = options.file
    @@events_backup = File.dirname(@@events_file) << File::Separator << 'bak_' << File.basename(@@events_file)
  else
    @log.error('To add events, you must name a file on the command-line. Aborting')
    exit false
  end
  if(!options.event || !options.year)
    @log.error("To add events, you must provide them on the command-line.\n\tPSE start the program with option -h or --help to see an option-overview.")
    exit false
  end

  @@log.level = $LOG_LEVEL if $LOG_LEVEL 
  fields = [:event, :year, :background]
  @log.debug('options are ' << options.to_s)
  if options[:event] && options[:year]
    if !options[:background]
      @log.warn('Optional background information is missing')
      bg = ''
    else
      bg = options.background
    end

    ev_def = "$events << [\"" << options.event << "\", " << options.year << ", \"" << bg << "\"]" 
    @log.debug('writing events-backup to ' << @@events_backup)
    File.open(@@events_backup, 'w+') {|bak| bak.write( File.read(@@events_file))}
    @log.debug('adding new event to the list: ' << ev_def)
    File.open(@@events_file, 'a') {|evf| evf.puts(ev_def)}
    @log.info('A new event has been added to ' << @@events_file << '.')
  else
    opts = options.to_h.keys
    @log.error('Option missing: ' << fields.each {|f| opts.delete(f) }.join(', '))
    exit false
  end
end