Sha256: ec5f3de2475618fa217f9b5a601954bfd45ee10fd34f0f62e032f1a4f74fcc47

Contents?: true

Size: 1.22 KB

Versions: 405

Compression:

Stored size: 1.22 KB

Contents

require 'travis/cli'
require 'optparse'

module Travis
  module CLI
    module Parser
      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

      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

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

Version data entries

405 entries across 405 versions & 3 rubygems

Version Path
travis-1.8.5.travis.781.4 lib/travis/cli/parser.rb
travis-1.8.4 lib/travis/cli/parser.rb
travis-1.8.5.travis.779.4 lib/travis/cli/parser.rb
travis-1.8.3 lib/travis/cli/parser.rb
travis-1.8.4.travis.777.4 lib/travis/cli/parser.rb
travis-1.8.4.travis.775.4 lib/travis/cli/parser.rb
travis-1.8.3.travis.773.4 lib/travis/cli/parser.rb
travis-async-listener-1.8.3 lib/travis/cli/parser.rb
travis-1.8.3.travis.745.4 lib/travis/cli/parser.rb
travis-1.8.3.travis.726.4.pre.debug lib/travis/cli/parser.rb
travis-1.8.3.travis.724.4 lib/travis/cli/parser.rb
travis-1.8.2 lib/travis/cli/parser.rb
travis-1.8.2.travis.722.4 lib/travis/cli/parser.rb
travis-1.8.2.travis.719.4 lib/travis/cli/parser.rb
travis-1.8.1 lib/travis/cli/parser.rb
travis-1.8.1.travis.717.4 lib/travis/cli/parser.rb
travis-1.8.1.travis.713.4 lib/travis/cli/parser.rb
travis-1.8.1.travis.705.5 lib/travis/cli/parser.rb
travis-1.8.1.travis.704.5 lib/travis/cli/parser.rb
travis-1.8.1.travis.699.5 lib/travis/cli/parser.rb