module Map::Options

Attributes

arguments[RW]

Public Class Methods

for(arg) click to toggle source
# File lib/map/options.rb, line 4
def for(arg)
  options =
    case arg
      when Hash
        arg
      when Array
        parse(arg)
      when String, Symbol
        {arg => true}
      else
        raise(ArgumentError, arg.inspect) unless arg.respond_to?(:to_hash)
        arg.to_hash
    end

  unless options.is_a?(Options)
    options = Map.for(options)
    options.extend(Options)
  end

  raise unless options.is_a?(Map)

  options
end
parse(arg) click to toggle source
# File lib/map/options.rb, line 28
def parse(arg)
  case arg
    when Array
      arguments = arg
      arguments.extend(Arguments) unless arguments.is_a?(Arguments)
      options = arguments.options
    when Hash
      options = arg
      options = Options.for(options)
    else
      raise(ArgumentError, "`arg` should be an Array or Hash")
  end
end

Public Instance Methods

del_opt(opts) click to toggle source
# File lib/map/options.rb, line 97
def del_opt(opts)
  [ opts ].flatten.each do |opt|
    return delete(opt) if has_key?(opt)
  end
  nil
end
del_opts(*opts) click to toggle source
# File lib/map/options.rb, line 105
def del_opts(*opts)
  opts.flatten.map{|opt| delopt(opt)}
  opts
end
get_opt(opts, options = {}) click to toggle source
# File lib/map/options.rb, line 66
def get_opt(opts, options = {})
  options = Map.for(options.is_a?(Hash) ? options : {:default => options})
  default = options[:default]
  [ opts ].flatten.each do |opt|
    return fetch(opt) if has_key?(opt)
  end
  default
end
get_opts(*opts) click to toggle source
# File lib/map/options.rb, line 76
def get_opts(*opts)
  opts.flatten.map{|opt| getopt(opt)}
end
has_opt(opts) click to toggle source
# File lib/map/options.rb, line 81
def has_opt(opts)
  [ opts ].flatten.each do |opt|
    return true if has_key?(opt)
  end
  false
end
has_opts(*opts) click to toggle source
# File lib/map/options.rb, line 91
def has_opts(*opts)
  opts.flatten.all?{|opt| hasopt(opt)}
end
pop() click to toggle source
# File lib/map/options.rb, line 45
def pop
  arguments.pop if arguments.last.object_id == object_id
  self
end
pop!() click to toggle source
# File lib/map/options.rb, line 54
def pop!
  arguments.pop if arguments.last.object_id == object_id
  self
end
popped?() click to toggle source
# File lib/map/options.rb, line 50
def popped?
  !(arguments.last.object_id == object_id)
end
set_opt(opts, value = nil) click to toggle source
# File lib/map/options.rb, line 112
def set_opt(opts, value = nil)
  [ opts ].flatten.each do |opt|
    return self[opt]=value
  end
  return value
end
set_opts(opts) click to toggle source
# File lib/map/options.rb, line 121
def set_opts(opts)
  opts.each{|key, value| setopt(key, value)}
  opts
end