Module: Map::Options
- Defined in:
- lib/map/options.rb
Instance Attribute Summary collapse
-
#arguments ⇒ Object
Returns the value of attribute arguments.
Class Method Summary collapse
Instance Method Summary collapse
- #del_opt(opts) ⇒ Object (also: #delopt)
- #del_opts(*opts) ⇒ Object (also: #delopts, #delopts!)
- #get_opt(opts, options = {}) ⇒ Object (also: #getopt)
- #get_opts(*opts) ⇒ Object (also: #getopts)
- #has_opt(opts) ⇒ Object (also: #hasopt, #hasopt?, #has_opt?)
- #has_opts(*opts) ⇒ Object (also: #hasopts?, #has_opts?)
- #pop ⇒ Object
- #pop! ⇒ Object
- #popped? ⇒ Boolean
- #set_opt(opts, value = nil) ⇒ Object (also: #setopt, #setopt!)
- #set_opts(opts) ⇒ Object (also: #setopts, #setopts!)
Instance Attribute Details
#arguments ⇒ Object
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 |
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, = {}) = Map.for(.is_a?(Hash) ? : {:default => }) default = [: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 |
#pop ⇒ Object
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
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 |