Class: AIRAC::Executable

Inherits:
Object
  • Object
show all
Defined in:
lib/airac/executable.rb

Overview

Executable instantiated by the console tools

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExecutable

Returns a new instance of Executable.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/airac/executable.rb', line 7

def initialize
  @options = {
    format: nil,
    offset: 0
  }
  OptionParser.new do |o|
    o.banner = <<~END
      Calculate AIRAC cycle from four digit ID or date within (default: today).
      Usage: #{File.basename($0)} [options] [cycle]
    END
    o.on('-f', '--format TEMPLATE', String, "Template for strftime with %i for cycle ID") { @options[:format] = _1 }
    o.on('-o', '--offset INTEGER', Integer, "Offset by this many cycles") { @options[:offset] = _1.to_i }
    o.on('-A', '--about', 'show author/license information and exit') { about }
    o.on('-V', '--version', 'show version and exit') { version }
  end.parse!
  @options[:raw_cycle] = ARGV.shift || Date.today
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/airac/executable.rb', line 5

def options
  @options
end

Instance Method Details

#runObject



25
26
27
28
29
30
# File 'lib/airac/executable.rb', line 25

def run
  puts (AIRAC::Cycle.new(options[:raw_cycle]) + options[:offset].to_i).to_s(options[:format])
rescue => error
  message = error.respond_to?(:original_message) ? error.original_message : error.message
  puts "ERROR: #{message}"
end