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.



160
161
162
# File 'lib/options.rb', line 160

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



101
102
103
104
105
106
107
108
109
110
# File 'lib/options.rb', line 101

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



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

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
# 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
  default
end

#getopts(*args) ⇒ Object



80
81
82
# File 'lib/options.rb', line 80

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

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



84
85
86
87
88
89
90
91
92
93
# File 'lib/options.rb', line 84

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?



96
97
98
# File 'lib/options.rb', line 96

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

#normalizeObject Also known as: normalized, to_options



144
145
146
# File 'lib/options.rb', line 144

def normalize
  Options.normalize(self)
end

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



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

def normalize!
  Options.normalize!(self)
end

#popObject



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

def pop
  pop! unless popped?
  self
end

#pop!Object



170
171
172
173
174
175
176
# File 'lib/options.rb', line 170

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

#popped?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/options.rb', line 166

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

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



134
135
136
# File 'lib/options.rb', line 134

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

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



116
117
118
119
120
121
122
123
124
125
# File 'lib/options.rb', line 116

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!



128
129
130
131
# File 'lib/options.rb', line 128

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

#stringifyObject Also known as: stringified



155
156
157
# File 'lib/options.rb', line 155

def stringify
  Options.stringify(self)
end

#stringify!Object Also known as: stringified!



150
151
152
# File 'lib/options.rb', line 150

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)


182
183
184
185
186
187
# File 'lib/options.rb', line 182

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