Module: Options

Defined in:
lib/options.rb

Constant Summary collapse

VERSION =
'2.2.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



168
169
170
# File 'lib/options.rb', line 168

def arguments
  @arguments
end

Class Method Details

.for(hash) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/options.rb', line 31

def for(hash)
  hash =
    case hash
      when Hash
        hash
      when Array
        Hash[*hash.flatten]
      when String, Symbol
        {hash => true}
      else
        hash.to_hash
    end
  normalize!(hash)
ensure
  hash.extend(Options) unless hash.is_a?(Options)
end

.normalize(hash) ⇒ Object



15
16
17
# File 'lib/options.rb', line 15

def normalize(hash)
  normalize!(hash.dup)
end

.normalize!(hash) ⇒ Object



9
10
11
12
# File 'lib/options.rb', line 9

def normalize!(hash)
  hash.keys.each{|key| hash[key.to_s.to_sym] = hash.delete(key) unless Symbol===key}
  hash
end

.parse(args) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/options.rb', line 48

def parse(args)
  case args
  when Array
    args.extend(Arguments) unless args.is_a?(Arguments)
    [args, args.options.pop]
  when Hash
    Options.for(args)
  else
    raise ArgumentError, "`args` should be and Array or Hash"
  end
end

.stringifiedObject



29
30
31
# File 'lib/options.rb', line 29

def stringify(hash)
  stringify!(hash)
end

.stringified!Object



24
25
26
27
# File 'lib/options.rb', line 24

def stringify!(hash)
  hash.keys.each{|key| hash[key.to_s] = hash.delete(key) unless String===key}
  hash
end

.stringify(hash) ⇒ Object



26
27
28
# File 'lib/options.rb', line 26

def stringify(hash)
  stringify!(hash)
end

.stringify!(hash) ⇒ Object



20
21
22
23
# File 'lib/options.rb', line 20

def stringify!(hash)
  hash.keys.each{|key| hash[key.to_s] = hash.delete(key) unless String===key}
  hash
end

.to_optionsObject



18
19
20
# File 'lib/options.rb', line 18

def normalize(hash)
  normalize!(hash.dup)
end

.to_options!Object



13
14
15
16
# File 'lib/options.rb', line 13

def normalize!(hash)
  hash.keys.each{|key| hash[key.to_s.to_sym] = hash.delete(key) unless Symbol===key}
  hash
end

.versionObject



5
6
7
# File 'lib/options.rb', line 5

def version
  Options::VERSION
end

Instance Method Details

#delopt(key, default = nil) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/options.rb', line 109

def delopt key, default = nil
  [ key ].flatten.each do |key|
    return delete(key) if has_key?(key)
    key = key.to_s
    return delete(key) if has_key?(key)
    key = key.to_sym
    return delete(key) if has_key?(key)
  end
  default
end

#delopts(*args) ⇒ Object



120
121
122
# File 'lib/options.rb', line 120

def delopts *args
  args.flatten.map{|arg| delopt arg}
end

#getopt(key, default = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/options.rb', line 69

def getopt key, default = nil
  [ key ].flatten.each do |key|
    return fetch(key) if has_key?(key)
    key = key.to_s
    return fetch(key) if has_key?(key)
    key = key.to_sym
    return fetch(key) if has_key?(key)
  end
  # did not find the option; fall back on default
  
  default = Options.for(default).getopt(:default) if Hash === default

  if default.respond_to?(:call)
    default.call
  else
    default
  end
end

#getopts(*args) ⇒ Object



88
89
90
# File 'lib/options.rb', line 88

def getopts *args
  args.flatten.map{|arg| getopt arg}
end

#hasopt(key, default = nil) ⇒ Object Also known as: hasopt?



92
93
94
95
96
97
98
99
100
101
# File 'lib/options.rb', line 92

def hasopt key, default = nil
  [ key ].flatten.each do |key|
    return true if has_key?(key)
    key = key.to_s
    return true if has_key?(key)
    key = key.to_sym
    return true if has_key?(key)
  end
  default
end

#hasopts(*args) ⇒ Object Also known as: hasopts?



104
105
106
# File 'lib/options.rb', line 104

def hasopts *args
  args.flatten.map{|arg| hasopt arg}
end

#normalizeObject Also known as: normalized, to_options



152
153
154
# File 'lib/options.rb', line 152

def normalize
  Options.normalize(self)
end

#normalize!Object Also known as: normalized!, to_options!



146
147
148
# File 'lib/options.rb', line 146

def normalize!
  Options.normalize!(self)
end

#popObject



169
170
171
172
# File 'lib/options.rb', line 169

def pop
  pop! unless popped?
  self
end

#pop!Object



178
179
180
# File 'lib/options.rb', line 178

def pop!
  @popped = arguments.pop
end

#popped?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/options.rb', line 174

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

#select!(*a, &b) ⇒ Object



142
143
144
# File 'lib/options.rb', line 142

def select! *a, &b
  replace select(*a, &b).to_hash
end

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



124
125
126
127
128
129
130
131
132
133
# File 'lib/options.rb', line 124

def setopt key, value = nil
  [ key ].flatten.each do |key|
    return self[key]=value if has_key?(key)
    key = key.to_s
    return self[key]=value if has_key?(key)
    key = key.to_sym
    return self[key]=value if has_key?(key)
  end
  return self[key]=value
end

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



136
137
138
139
# File 'lib/options.rb', line 136

def setopts opts 
  opts.each{|key, value| setopt key, value}
  opts
end

#stringifyObject Also known as: stringified



163
164
165
# File 'lib/options.rb', line 163

def stringify
  Options.stringify(self)
end

#stringify!Object Also known as: stringified!



158
159
160
# File 'lib/options.rb', line 158

def stringify!
  Options.stringify!(self)
end

#validate(*acceptable_options) ⇒ Object

Validates that the options provided are acceptable.

Parameters:

  • *acceptable_options (Symbol)

    List of options that are allowed

Raises:

  • (ArgumentError)


186
187
188
189
190
191
# File 'lib/options.rb', line 186

def validate(*acceptable_options)
  remaining = (provided_options - acceptable_options).map{|opt| opt.to_s}.sort
  raise ArgumentError, "Unrecognized options: #{remaining.join(', ')}" unless remaining.empty?
  
  self
end