Module: Options

Defined in:
lib/options.rb

Constant Summary collapse

VERSION =
'2.0.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



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

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

def parse(args)
  args.extend(Arguments) unless args.is_a?(Arguments)
  args.options.pop
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



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

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



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

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

#getopt(key, default = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/options.rb', line 62

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



73
74
75
# File 'lib/options.rb', line 73

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

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



77
78
79
80
81
82
83
84
85
86
# File 'lib/options.rb', line 77

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?



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

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

#normalizeObject Also known as: to_options



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

def normalize
  Options.normalize(self)
end

#normalize!Object Also known as: to_options!



131
132
133
# File 'lib/options.rb', line 131

def normalize!
  Options.normalize!(self)
end

#popObject



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

def pop
  pop! unless popped?
  self
end

#pop!Object



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

def pop!
  @popped = arguments.pop
end

#popped?Boolean

Returns:

  • (Boolean)


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

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

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



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

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

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



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

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!



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

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

#stringifyObject Also known as: stringified



148
149
150
# File 'lib/options.rb', line 148

def stringify
  Options.stringify(self)
end

#stringify!Object Also known as: stringified!



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

def stringify!
  Options.stringify!(self)
end