Class: Optionoids::Checker
- Inherits:
-
Object
- Object
- Optionoids::Checker
- Defined in:
- lib/optionoids/checker.rb
Overview
Class to wrap an options Hash and perform checks on the keys and values. All method return the same instance of the Checker, allowing for method chaining.
Instance Attribute Summary collapse
-
#hard ⇒ Object
readonly
Returns the value of attribute hard.
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
Instance Method Summary collapse
-
#and ⇒ Object
(also: #all)
Removes all option Hash filtering.
-
#blank ⇒ Object
(also: #all_blank)
Checks that the current option Hash entries all have blank values.
-
#current_options ⇒ Object
Returns the current ‘filtered’ option Hash.
-
#errors ⇒ Object
SOFT ERROR HANDLING ##.
-
#exist ⇒ Object
Checks that all the currently set filter keys are present in the current option Hash.
- #failed? ⇒ Boolean
-
#flag ⇒ Object
Checks that the current option Hash entries are usable as flags.
-
#global_options ⇒ Object
Returns the ‘unfiltered’ options Hash.
-
#identifier ⇒ Object
Checks that the current option Hash entries are usable as identifiers.
-
#initialize(options, keys: nil, hard: true) ⇒ Checker
constructor
A new instance of Checker.
-
#just_one ⇒ Object
Checks that the current option Hash contains exactly one key.
-
#minus(*keys) ⇒ Object
Removes the keys from the current option Hash filter.
-
#nil_values ⇒ Object
Checks that the current option Hash entries all have nil values.
-
#not_nil_values ⇒ Object
Checks that the current option Hash entries all have non-nil values.
-
#of_types(*types) ⇒ Object
(also: #of_type, #types, #type)
Checks that the current option Hash entries are of the given types.
-
#one_of_more ⇒ Object
Checks that the current option Hash contains one or more keys.
-
#one_or_none ⇒ Object
Checks that the current option Hash contains no more that one key.
-
#only_these(keys) ⇒ Object
Checks that the current option Hash contains only the given keys.
-
#plus(*keys) ⇒ Object
Adds the given keys to the current option Hash filter.
-
#populated ⇒ Object
(also: #all_populated)
Checks that the current option Hash entries all have non-blank values.
-
#possible_values(variants) ⇒ Object
Checks that the current option Hash entries are one of the given variants.
-
#required ⇒ Object
Checks that the current option Hash entries ate both present and populated.
-
#that(*keys) ⇒ Object
Add a key set filter to the current option Hash.
-
#with_params(params) ⇒ Object
A set of additional options to check against.
Constructor Details
#initialize(options, keys: nil, hard: true) ⇒ Checker
Returns a new instance of Checker.
16 17 18 19 20 21 22 |
# File 'lib/optionoids/checker.rb', line 16 def initialize(, keys: nil, hard: true) @options = @keys = [keys].flatten.compact @params = {} @hard = hard end |
Instance Attribute Details
#hard ⇒ Object (readonly)
Returns the value of attribute hard.
11 12 13 |
# File 'lib/optionoids/checker.rb', line 11 def hard @hard end |
#keys ⇒ Object (readonly)
Returns the value of attribute keys.
11 12 13 |
# File 'lib/optionoids/checker.rb', line 11 def keys @keys end |
Instance Method Details
#and ⇒ Object Also known as: all
Removes all option Hash filtering.
45 46 47 48 49 |
# File 'lib/optionoids/checker.rb', line 45 def and @keys = [] self end |
#blank ⇒ Object Also known as: all_blank
Checks that the current option Hash entries all have blank values. If any of the values are populated, an error (Optionoids::Errors::UnexpectedPopulatedValue) is raised.
115 116 117 118 |
# File 'lib/optionoids/checker.rb', line 115 def blank _error_on_check(:present?, Errors::UnexpectedPopulatedValue) self end |
#current_options ⇒ Object
Returns the current ‘filtered’ option Hash.
33 34 35 |
# File 'lib/optionoids/checker.rb', line 33 def @clipped_options end |
#errors ⇒ Object
SOFT ERROR HANDLING ##
219 220 221 |
# File 'lib/optionoids/checker.rb', line 219 def errors @errors ||= [] end |
#exist ⇒ Object
Checks that all the currently set filter keys are present in the current option Hash. If there are no entries in the current option Hash an error (Optionoids::Errors::RequiredDataUnavailable) is raised. If any of the keys are missing, an error (Optionoids::Errors::MissingKeys) is raised.
93 94 95 96 97 98 99 100 |
# File 'lib/optionoids/checker.rb', line 93 def exist return _error_or_log(Errors::RequiredDataUnavailable.new(nil, check: 'present')) if @keys.empty? missing_keys = @keys - @clipped_options.keys return self if missing_keys.empty? _error_or_log(Errors::MissingKeys.new(nil, keys: missing_keys)) end |
#failed? ⇒ Boolean
223 224 225 |
# File 'lib/optionoids/checker.rb', line 223 def failed? errors.any? end |
#flag ⇒ Object
Checks that the current option Hash entries are usable as flags. This means that the values are either TrueClass or FalseClass and are not blank.
207 208 209 210 |
# File 'lib/optionoids/checker.rb', line 207 def flag # Populated check mist use not nil because false is never 'present?' of_type(TrueClass, FalseClass).not_nil_values end |
#global_options ⇒ Object
Returns the ‘unfiltered’ options Hash.
38 39 40 |
# File 'lib/optionoids/checker.rb', line 38 def @options.merge(@params) end |
#identifier ⇒ Object
Checks that the current option Hash entries are usable as identifiers. This means that the values are either Strings or Symbols and are not blank.
201 202 203 |
# File 'lib/optionoids/checker.rb', line 201 def identifier of_type(String, Symbol).populated end |
#just_one ⇒ Object
Checks that the current option Hash contains exactly one key. If no keys are present, an error (Optionoids::Errors::RequiredDataUnavailable) is raised. If more than one key is present, an error (Optionoids::Errors::UnexpectedMultipleKeys) is raised.
153 154 155 156 157 158 159 |
# File 'lib/optionoids/checker.rb', line 153 def just_one _error_or_log(Errors::RequiredDataUnavailable.new(nil, check: 'one_required')) if @clipped_options.empty? return self if @clipped_options.one? msg = "Expected exactly one key, but found: #{@clipped_options.keys.to_sentence}" _error_or_log(Errors::UnexpectedMultipleKeys.new(msg, keys: @clipped_options.keys)) end |
#minus(*keys) ⇒ Object
Removes the keys from the current option Hash filter. No error will be raised if the given keys are not present in the current option Hash.
64 65 66 67 68 |
# File 'lib/optionoids/checker.rb', line 64 def minus(*keys) @keys -= keys self end |
#nil_values ⇒ Object
Checks that the current option Hash entries all have nil values. If any of the values are not nil, an error (Optionoids::Errors::UnexpectedNonNilValue) is raised.
131 132 133 134 135 136 |
# File 'lib/optionoids/checker.rb', line 131 def nil_values failed_keys = @clipped_options.compact.keys return self if failed_keys.empty? _error_or_log(Errors::UnexpectedNonNilValue.new(nil, keys: failed_keys)) end |
#not_nil_values ⇒ Object
Checks that the current option Hash entries all have non-nil values. If any of the values are nil, an error (Optionoids::Errors::UnexpectedNilValue) is raised.
124 125 126 127 |
# File 'lib/optionoids/checker.rb', line 124 def not_nil_values _error_on_check(:nil?, Errors::UnexpectedNilValue) self end |
#of_types(*types) ⇒ Object Also known as: of_type, types, type
Checks that the current option Hash entries are of the given types. If any of the values are not of the given types, an error (Optionoids::Errors::UnexpectedValueType) is raised. ‘nil’ values are ignored in the type check.
174 175 176 177 178 179 |
# File 'lib/optionoids/checker.rb', line 174 def of_types(*types) pairs = @clipped_options.compact.select { |_k, v| types.none? { |t| v.is_a?(t) } } return self if pairs.empty? _error_or_log(Errors::UnexpectedValueType.new(nil, keys: pairs.keys, types: types.map(&:name))) end |
#one_of_more ⇒ Object
Checks that the current option Hash contains one or more keys. If no keys are present, an error (Optionoids::Errors::ExpectedMultipleKeys).
163 164 165 166 167 |
# File 'lib/optionoids/checker.rb', line 163 def one_of_more return self if @clipped_options.count >= 1 _error_or_log(Errors::ExpectedMultipleKeys.new) end |
#one_or_none ⇒ Object
Checks that the current option Hash contains no more that one key. If more than one key id present, an error (Optionoids::Errors::UnexpectedMultipleKeys) is raised. If there are no keys present, no error is raised.
143 144 145 146 147 148 |
# File 'lib/optionoids/checker.rb', line 143 def one_or_none msg = "Expected a maximum or one key, but found: #{@clipped_options.keys.to_sentence}" _error_or_log(Errors::UnexpectedMultipleKeys.new(msg, keys: @clipped_options.keys)) if @clipped_options.count > 1 self end |
#only_these(keys) ⇒ Object
Checks that the current option Hash contains only the given keys. If any unexpected keys are present, an error is raised. If no keys are given, the current option Hash is not checked. Error: Optionoids::Errors::UnexpectedKeys
83 84 85 86 87 88 |
# File 'lib/optionoids/checker.rb', line 83 def only_these(keys) unexpected_keys = @clipped_options.keys - [keys].flatten _error_or_log(Errors::UnexpectedKeys.new(nil, keys: unexpected_keys)) if unexpected_keys.any? self end |
#plus(*keys) ⇒ Object
Adds the given keys to the current option Hash filter. No error will be raised if the given keys are not present in the current option Hash.
72 73 74 75 76 |
# File 'lib/optionoids/checker.rb', line 72 def plus(*keys) @keys |= keys.flatten.compact.uniq self end |
#populated ⇒ Object Also known as: all_populated
Checks that the current option Hash entries all have non-blank values. If any of the values are blank, an error (Optionoids::Errors::UnexpectedBlankValue) is raised.
106 107 108 109 |
# File 'lib/optionoids/checker.rb', line 106 def populated _error_on_check(:blank?, Errors::UnexpectedBlankValue) self end |
#possible_values(variants) ⇒ Object
Checks that the current option Hash entries are one of the given variants. If any of the values are not one of the given variants, an error (Optionoids::Errors::UnexpectedValueVariant). If a value is nil it is ignored in the check.
190 191 192 193 194 195 |
# File 'lib/optionoids/checker.rb', line 190 def possible_values(variants) pairs = @clipped_options.compact.select { |_k, v| variants.none? { |variant| v == variant } } return self if pairs.empty? _error_or_log(Errors::UnexpectedValueVariant.new(nil, keys: pairs.keys, variants: variants)) end |
#required ⇒ Object
Checks that the current option Hash entries ate both present and populated.
213 214 215 |
# File 'lib/optionoids/checker.rb', line 213 def required exist.populated end |
#that(*keys) ⇒ Object
Add a key set filter to the current option Hash. The keys provided do not have to exist in the option Hash, but only those that do will be checked. Filters are not cumulative, so calling this method will replace any previous key filters.
56 57 58 59 60 |
# File 'lib/optionoids/checker.rb', line 56 def that(*keys) @keys = keys.flatten.compact self end |
#with_params(params) ⇒ Object
A set of additional options to check against. This is useful for checking other none optional parameters that are not part of the options Hash.
26 27 28 29 30 |
# File 'lib/optionoids/checker.rb', line 26 def with_params(params) @params = params.is_a?(Hash) ? params : params.to_h self end |