Class: Options
Overview
Options is a utility class to support a method option parameter. Option argument parsing is described in Options.get.
Class Method Summary collapse
- .default(value) ⇒ Object private
- .detect_in_enumerable(key, opts) ⇒ Object private
-
.get(option, options, default = nil, &block) ⇒ Object
Returns the value of option in options as follows: * If options is a hash which contains the option key, then this method returns the option value.
-
.to_hash(*opts) ⇒ Hash
Returns the given option list as a hash, determined as follows: * If an item is a hash, then that hash is included in the result * If an item is a symbol s, then => true is included in the result.
- .validate(options, choices) ⇒ Object
Class Method Details
.default(value) ⇒ Object (private)
90 91 92 |
# File 'lib/jinx/helpers/options.rb', line 90 def self.default(value) value.nil? && block_given? ? yield : value end |
.detect_in_enumerable(key, opts) ⇒ Object (private)
84 85 86 87 88 |
# File 'lib/jinx/helpers/options.rb', line 84 def self.detect_in_enumerable(key, opts) opts.detect_value do |opt| Hash === opt ? opt[key] : opt == key end end |
.get(option, options, default = nil, &block) ⇒ Object
Returns the value of option in options as follows:
-
If options is a hash which contains the option key, then this method returns the option value. A non-collection options value is wrapped as a singleton collection to conform to a collection default type, as shown in the example below.
-
If options equals the option symbol, then this method returns
true
. -
If options is an Array of symbols which includes the given option, then this method returns
true
. -
Otherwise, this method returns the default.
If default is nil and a block is given to this method, then the default is determined by calling the block with no arguments. The block can also be used to raise a missing option exception, e.g.:
Options.get(:userid, ) { raise RuntimeError.new("Missing required option: userid") }
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/jinx/helpers/options.rb', line 33 def self.get(option, , default=nil, &block) return default(default, &block) if .nil? case when Hash then value = [option] if String === value then value.strip! end value.nil_or_empty? ? default(default, &block) : value when Enumerable then detect_in_enumerable(option, ) or default(default, &block) when Symbol then option == or default(default, &block) else raise ArgumentError.new("Options argument type is not supported; expected Hash or Symbol, found: #{.class}") end end |
.to_hash(*opts) ⇒ Hash
Returns the given option list as a hash, determined as follows:
-
If an item is a hash, then that hash is included in the result
-
If an item is a symbol s, then => true is included in the result
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/jinx/helpers/options.rb', line 61 def self.to_hash(*opts) hash = {} opts.compact! opts.each do |opt| case opt when Symbol then hash[opt] = true when Hash then hash.merge!(opt) else raise ArgumentError.new("Expected a symbol or hash option, found #{opt.qp}") end end hash end |
.validate(options, choices) ⇒ Object
76 77 78 79 80 |
# File 'lib/jinx/helpers/options.rb', line 76 def self.validate(, choices) to_hash().each_key do |opt| raise ValidationError.new("Option is not supported: #{opt}") unless choices.include?(opt) end end |