Module: Travis::CLI::Parser

Included in:
Command
Defined in:
lib/travis/cli/parser.rb

Instance Method Summary collapse

Instance Method Details

#newObject


34
35
36
37
38
39
40
# File 'lib/travis/cli/parser.rb', line 34

def new(*)
  attr_accessor :parser unless method_defined? :parser
  result        = super
  result.parser = OptionParser.new
  on_initialize.each { |b| b[result] }
  result
end

#on(*args, &block) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/travis/cli/parser.rb', line 17

def on(*args, &block)
  block ||= begin
    full_arg = args.detect { |a| a.start_with? '--' }
    name = full_arg.gsub(/^--(\[no-\])?(\S+).*$/, '\2').gsub('-', '_')
    attr_reader(name)               unless method_defined? name
    attr_writer(name)               unless method_defined? "#{name}="
    alias_method("#{name}?", name)  unless method_defined? "#{name}?"
    proc { |instance, value| instance.public_send("#{name}=", value) }
  end

  on_initialize do |instance|
    instance.parser.on(*args) do |value|
      block.call(instance, value)
    end
  end
end

#on_initialize(&block) ⇒ Object


7
8
9
10
11
12
13
14
15
# File 'lib/travis/cli/parser.rb', line 7

def on_initialize(&block)
  @on_initialize ||= []
  @on_initialize << block if block
  if superclass.respond_to? :on_initialize
    superclass.on_initialize + @on_initialize
  else
    @on_initialize
  end
end