Module: Rrutils::Options
- Included in:
- Dumon::App, Dumon::OutDeviceManager
- Defined in:
- lib/rrutils/options.rb
Overview
This modul represents a mixin for a set of convenience methods that work with options representing method’s parameters.
Instance Method Summary collapse
-
#assert(test, msg = nil) ⇒ Object
Fails unless
test
is a true value. -
#keys_to_sym(options) ⇒ Object
Goes recursively through given Hash and converst all key into Symbol.
-
#verify_and_sanitize_options(options, pattern, cloned = true) ⇒ Object
The same as
verify_options
with opportunity to define default values of paramaters that will be set if missing in options. -
#verify_options(options, pattern) ⇒ Object
Verifies given options against a pattern that defines checks applied on each option.
Instance Method Details
#assert(test, msg = nil) ⇒ Object
Fails unless test
is a true value.
msg
may be a String or a Proc. If no msg
is given, a default message will be used.
13 14 15 16 17 18 19 |
# File 'lib/rrutils/options.rb', line 13 def assert(test, msg=nil) msg ||= 'failed assertion' unless test msg = msg.call if Proc === msg raise ArgumentError, msg end end |
#keys_to_sym(options) ⇒ Object
Goes recursively through given Hash and converst all key into Symbol.
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/rrutils/options.rb', line 77 def keys_to_sym() raise ArgumentError, 'not a hash' unless .is_a? Hash opts = .clone .each do |k,v| opts[k.to_sym] = opts.delete k unless k.is_a? Symbol opts[k.to_sym] = keys_to_sym(v) if v.is_a? Hash end opts end |
#verify_and_sanitize_options(options, pattern, cloned = true) ⇒ Object
The same as verify_options
with opportunity to define default values of paramaters that will be set if missing in options.
Usage: verify_and_sanitize_options(options_to_be_verified, :bar=>100)
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rrutils/options.rb', line 63 def (, pattern, cloned=true) opts = cloned ? .clone : (opts, pattern) # set default values if missing in options pattern.select { |k,v| !v.nil? and v != :optional }.each do |k,v| opts[k] = v if !opts.keys.include? k end opts end |
#verify_options(options, pattern) ⇒ Object
Verifies given options against a pattern that defines checks applied on each option. The pattern is a Hash where property is an expected option’s property and value can be:
-
:optional - corresponding option may or may not be presented
-
:mandatory - corresponding option has to be presented
-
Array - corresponding option has to be presented and value has to be in the given list
Usage: verify_options(options_to_be_verified, :bar=>[true, false], :baz=>:optional)
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rrutils/options.rb', line 31 def (, pattern) raise ArgumentError, 'options cannot be nil' if .nil? raise ArgumentError, 'options is not Hash' unless .is_a? Hash raise ArgumentError, 'pattern cannot be nil' if pattern.nil? raise ArgumentError, 'pattern cannot be empty' if pattern.empty? raise ArgumentError, 'pattern is not Hash' unless pattern.is_a? Hash # unknown key? .keys.each do |k| raise ArgumentError, "unknow option: #{k}" unless pattern.keys.include? k end # missing mandatory option? pattern.each do |k,v| # :mandatory if v == :mandatory raise ArgumentError, "missing mandatory option: #{k}" if !.keys.include?(k) or [k].nil? elsif v.is_a? Array raise ArgumentError, "value '#{[k]}' not in #{v.inspect}, key=#{k}" unless v.include?([k]) end end end |