Class: Denso::Calendar::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/denso/calendar/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Cli

Returns a new instance of Cli.



15
16
17
18
19
20
21
22
23
# File 'lib/denso/calendar/cli.rb', line 15

def initialize(argv)
  require 'optparse'
  opt = OptionParser.new
  opt.on('-t TYPE', '--type TYPE') { |v| v }
  opt.on('-f FORMATTER', '--formatter FORMATTER') { |v| v }

  @options = {}
  opt.parse(argv, into: @options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/denso/calendar/cli.rb', line 13

def options
  @options
end

Class Method Details

.run(options) ⇒ Object



9
10
11
# File 'lib/denso/calendar/cli.rb', line 9

def self.run(options)
  new(options).run
end

Instance Method Details

#load_formatterObject



32
33
34
35
36
37
# File 'lib/denso/calendar/cli.rb', line 32

def load_formatter
  formatter_type = options.fetch(:formatter, :plain)

  require "denso/calendar/formatter/#{formatter_type}"
  Denso::Calendar::Formatter.const_get(formatter_type.to_s.capitalize).new
end

#runObject



25
26
27
28
29
30
# File 'lib/denso/calendar/cli.rb', line 25

def run
  calendar = Denso::Calendar.load(options[:type].to_sym)

  formatter = load_formatter
  puts formatter.format(calendar)
end