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.



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

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
20
# File 'lib/map/options.rb', line 4

def for(arg)
  hash =
    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
  map = Map.coerce(hash)
ensure
  map.extend(Options) unless map.is_a?(Options)
end

.parse(arg) ⇒ Object



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

def parse(arg)
  case arg
    when Array
      arg.extend(Arguments) unless arg.is_a?(Arguments)
      arg.options
    when Hash
      Options.for(arg)
    else
      raise(ArgumentError, "`arg` should be an Array or Hash")
  end
end

Instance Method Details

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



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

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!



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

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

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



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

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



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

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

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



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

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?



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

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

#popObject



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

def pop
  pop! unless popped?
  self
end

#pop!Object



115
116
117
118
119
120
121
# File 'lib/map/options.rb', line 115

def pop!
  if arguments.last.is_a?(Hash)
    @popped = arguments.pop
  else
    @popped = true
  end
end

#popped?Boolean

Returns:

  • (Boolean)


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

def popped?
  defined?(@popped) and @popped
end

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



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

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!



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

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