Class: SongkickQueue::CLI
- Inherits:
-
Object
- Object
- SongkickQueue::CLI
- Defined in:
- lib/songkick_queue/cli.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(argv) ⇒ CLI
constructor
A new instance of CLI.
-
#parse_options(argv) ⇒ Object
Parse the command line arguments using OptionParser.
-
#run ⇒ Object
Instantiates and runs a new Worker for the parsed options.
Constructor Details
#initialize(argv) ⇒ CLI
Returns a new instance of CLI.
9 10 11 12 13 14 15 16 17 |
# File 'lib/songkick_queue/cli.rb', line 9 def initialize(argv) @options = OpenStruct.new( libraries: [], consumers: [], process_name: 'songkick_queue', ) (argv) end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/songkick_queue/cli.rb', line 6 def @options end |
Instance Method Details
#parse_options(argv) ⇒ Object
Parse the command line arguments using OptionParser
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/songkick_queue/cli.rb', line 22 def (argv) option_parser = OptionParser.new do |opts| opts. = 'Usage: songkick_queue [options]' opts.on('-r', '--require LIBRARY', 'Path to require LIBRARY. Usually this will be a file that ', 'requires some consumers') do |lib| .libraries << lib end opts.on('-c', '--consumer CLASS_NAME', 'Register consumer with CLASS_NAME') do |class_name| .consumers << class_name end opts.on('-n', '--name NAME', 'Set the process name to NAME') do |name| .process_name = name end opts.on_tail("-h", "--help", "Show this message") do puts opts exit end end option_parser.parse!(argv) end |
#run ⇒ Object
Instantiates and runs a new Worker for the parsed options. Calling this method blocks the main Thread. See Worker#run for more info
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/songkick_queue/cli.rb', line 54 def run .libraries.each do |lib| require lib end if .consumers.empty? puts 'No consumers provided, exiting. Run `songkick_queue --help` for more info.' exit 1 end consumers = .consumers.map do |class_name| Object.const_get(class_name) end Worker.new(.process_name, consumers).run end |