Module: Map::Options

Defined in:
lib/map/options.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



36
37
38
# File 'lib/map/options.rb', line 36

def arguments
  @arguments
end

Class Method Details

.for(arg) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 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
  options.extend(Options) unless options.is_a?(Options)
  options
end

.parse(arg) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/map/options.rb', line 21

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

Instance Method Details

#del_opt(opts) ⇒ Object Also known as: delopt



76
77
78
79
80
81
# File 'lib/map/options.rb', line 76

def del_opt(opts)
  [ opts ].flatten.each do |opt|
    return delete(opt) if has_key?(opt)
  end
  nil
end

#del_opts(*opts) ⇒ Object Also known as: delopts, delopts!



84
85
86
87
# File 'lib/map/options.rb', line 84

def del_opts(*opts)
  opts.flatten.map{|opt| delopt(opt)}
  opts
end

#get_opt(opts, options = {}) ⇒ Object Also known as: getopt



45
46
47
48
49
50
51
52
# File 'lib/map/options.rb', line 45

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) ⇒ Object Also known as: getopts



55
56
57
# File 'lib/map/options.rb', line 55

def get_opts(*opts)
  opts.flatten.map{|opt| getopt(opt)}
end

#has_opt(opts) ⇒ Object Also known as: hasopt, hasopt?, has_opt?



60
61
62
63
64
65
# File 'lib/map/options.rb', line 60

def has_opt(opts)
  [ opts ].flatten.each do |opt|
    return true if has_key?(opt)
  end
  false
end

#has_opts(*opts) ⇒ Object Also known as: hasopts?, has_opts?



70
71
72
# File 'lib/map/options.rb', line 70

def has_opts(*opts)
  opts.flatten.all?{|opt| hasopt(opt)}
end

#popObject



107
108
109
110
# File 'lib/map/options.rb', line 107

def pop
  pop! unless popped?
  self
end

#pop!Object



116
117
118
# File 'lib/map/options.rb', line 116

def pop!
  arguments.pop if !popped?
end

#popped?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/map/options.rb', line 112

def popped?
  arguments and arguments.last != self
end

#set_opt(opts, value = nil) ⇒ Object Also known as: setopt, setopt!



91
92
93
94
95
96
# File 'lib/map/options.rb', line 91

def set_opt(opts, value = nil)
  [ opts ].flatten.each do |opt|
    return self[opt]=value
  end
  return value
end

#set_opts(opts) ⇒ Object Also known as: setopts, setopts!



100
101
102
103
# File 'lib/map/options.rb', line 100

def set_opts(opts)
  opts.each{|key, value| setopt(key, value)}
  opts
end