module Excom::Plugins::Args

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/excom/plugins/args.rb, line 5
def initialize(*args)
  args, opts = resolve_args!(args)

  assert_valid_args!(args)
  assert_valid_opts!(opts)

  @args = args
  @opts = opts

  super()
end

Public Instance Methods

initialize_clone(*) click to toggle source
Calls superclass method
# File lib/excom/plugins/args.rb, line 17
def initialize_clone(*)
  super
  @args = @args.dup unless @args.nil?
  @opts = @opts.dup unless @opts.nil?
end
with_args(*args) click to toggle source
# File lib/excom/plugins/args.rb, line 23
def with_args(*args)
  clone.tap{ |copy| copy.args.replace(args) }
end
with_opts(opts) click to toggle source
# File lib/excom/plugins/args.rb, line 27
def with_opts(opts)
  clone.tap{ |copy| copy.opts.merge!(opts) }
end

Protected Instance Methods

args() click to toggle source
# File lib/excom/plugins/args.rb, line 35
          def args
  @args
end
opts() click to toggle source
# File lib/excom/plugins/args.rb, line 31
          def opts
  @opts
end

Private Instance Methods

assert_valid_args!(actual) click to toggle source
# File lib/excom/plugins/args.rb, line 55
        def assert_valid_args!(actual)
  allowed = self.class.args_list.length

  if actual.length > allowed
    raise ArgumentError, "wrong number of args (given #{actual.length}, expected 0..#{allowed})"
  end
end
assert_valid_opts!(actual) click to toggle source
# File lib/excom/plugins/args.rb, line 63
        def assert_valid_opts!(actual)
  unexpected = actual.keys - self.class.opts_list

  if unexpected.any?
    raise ArgumentError, "wrong opts #{unexpected} given"
  end
end
resolve_args!(args) click to toggle source
# File lib/excom/plugins/args.rb, line 39
        def resolve_args!(args)
  opts = args.last.is_a?(Hash) ? args.pop : {}

  if args.length < self.class.args_list.length
    rest = opts
    opts = self.class.opts_list.each_with_object({}) do |key, ops|
      ops[key] = rest[key]
      rest.delete(key)
    end

    args.push(rest) unless rest.empty?
  end

  return args, opts
end