Class: Adder

Inherits:
Object
  • Object
show all
Includes:
BasicLogging
Defined in:
lib/adder.rb

Constant Summary

Constants included from BasicLogging

BasicLogging::DEBUG, BasicLogging::ERROR, BasicLogging::FATAL, BasicLogging::INFO, BasicLogging::Levels, BasicLogging::UNKNOWN, BasicLogging::WARN

Instance Attribute Summary

Attributes included from BasicLogging

#log_level, #target

Class Method Summary collapse

Methods included from BasicLogging

is_muted?, #log, mute, #set_level, #set_target

Class Method Details

.add(options) ⇒ Object



27
28
29
30
31
32
33
34
35
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
# File 'lib/adder.rb', line 27

def self::add(options) 
  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
    error('To add events, you must name a file on the command-line. Aborting')
    exit false
  end
  if(!options.event || !options.year)
    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

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

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