Class: Aspect::Verifier
- Inherits:
-
Object
- Object
- Aspect::Verifier
- Includes:
- HasAttributes
- Defined in:
- lib/aspect/verifier.rb,
lib/aspect/verifier/check.rb
Overview
Verify a set of checks on an object.
The result is a ‘Hash` where the keys are the check name and the value is the expectation or `nil` when the object is verified to be valid.
This allows for it to easily be transformed into messages using MessageTransform, if needed.
Defined Under Namespace
Classes: Check
Instance Method Summary collapse
-
#equal_to ⇒ Boolean
Get whether to for equality against a given object.
-
#equal_to= ⇒ nil, Object
Set the object to check equality against.
-
#float= ⇒ Boolean
Set whether to check for ‘Float`.
-
#float? ⇒ Boolean
Get whether to check for ‘Float`.
-
#initialize(*arguments) ⇒ Verifier
constructor
A new instance of Verifier.
-
#integer= ⇒ Boolean
Set whether to check for ‘Integer`.
-
#integer? ⇒ Boolean
Get whether to check for ‘Integer`.
-
#presence= ⇒ Boolean
Set whether to check for presence.
-
#presence? ⇒ Boolean
Get whether to check for presence.
-
#validate(value, options = {}) ⇒ <Symbol, Object>
Validate a value.
Methods included from HasAttributes
Constructor Details
#initialize(*arguments) ⇒ Verifier
Returns a new instance of Verifier.
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/aspect/verifier.rb', line 113 def initialize(*arguments) attributes = {} if arguments.length == 1 && arguments[0].respond_to?(:to_h) attributes = arguments[0] else arguments = arguments[0] if arguments.length == 1 && arguments[0].is_a?(Array) arguments.collect(&:to_sym).each { |check| attributes[check] = true } end update_attributes(attributes) end |
Instance Method Details
#equal_to ⇒ Boolean
Get whether to for equality against a given object.
200 |
# File 'lib/aspect/verifier.rb', line 200 attribute(:equal_to) |
#equal_to= ⇒ nil, Object
Set the object to check equality against.
|
# File 'lib/aspect/verifier.rb', line 190
|
#float= ⇒ Boolean
Set whether to check for ‘Float`.
The values ‘123.4` and `“123.4”` will both pass.
|
# File 'lib/aspect/verifier.rb', line 176
|
#float? ⇒ Boolean
Get whether to check for ‘Float`.
188 |
# File 'lib/aspect/verifier.rb', line 188 attribute(:float, query: true) |
#integer= ⇒ Boolean
Set whether to check for ‘Integer`.
The values ‘123` and `“123”` will both pass, but `123.4` and `“123.4”` will both fail.
|
# File 'lib/aspect/verifier.rb', line 162
|
#integer? ⇒ Boolean
Get whether to check for ‘Integer`.
174 |
# File 'lib/aspect/verifier.rb', line 174 attribute(:integer, query: true) |
#presence= ⇒ Boolean
Set whether to check for presence.
Objects that are ‘nil`, or respond to `empty?` and return `true`, are considered blank.
|
# File 'lib/aspect/verifier.rb', line 148
|
#presence? ⇒ Boolean
Get whether to check for presence.
160 |
# File 'lib/aspect/verifier.rb', line 160 attribute(:presence, query: true) |
#validate(value, options = {}) ⇒ <Symbol, Object>
Validate a value.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/aspect/verifier.rb', line 133 def validate(value, ={}) = { break_on_fail: false }.merge(.to_h) errors = {} Check.registry.each do |check_name, check| check_iv = instance_variable_get("@#{check_name}") errors[check_name] = check_iv if check_iv && !check.call(value, check_iv) break if [:break_on_fail] end errors end |