Class: ShapeOf::Validator
- Inherits:
-
Object
- Object
- ShapeOf::Validator
- Defined in:
- lib/shape_of.rb
Overview
Used in Assertions, and can also be used separately. It is used to keep track of places where the shape of data does not match, so that you can have greater insight than just a simple true/false.
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#shape ⇒ Object
readonly
Returns the value of attribute shape.
Instance Method Summary collapse
- #add_error(message) ⇒ Object
- #error_message ⇒ Object
- #errors ⇒ Object
-
#initialize(shape:, object:) ⇒ Validator
constructor
A new instance of Validator.
- #pop_key ⇒ Object
- #push_key(key) ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(shape:, object:) ⇒ Validator
95 96 97 98 99 100 101 102 |
# File 'lib/shape_of.rb', line 95 def initialize(shape:, object:) @current_error_key_nesting = [:base] # stack of the current error key. @errors = {} @object = object @shape = shape validate end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
93 94 95 |
# File 'lib/shape_of.rb', line 93 def object @object end |
#shape ⇒ Object (readonly)
Returns the value of attribute shape.
93 94 95 |
# File 'lib/shape_of.rb', line 93 def shape @shape end |
Instance Method Details
#add_error(message) ⇒ Object
116 117 118 119 120 |
# File 'lib/shape_of.rb', line 116 def add_error() create_nested_error_structure @errors.dig(*@current_error_key_nesting)[:errors] << .dup end |
#error_message ⇒ Object
112 113 114 |
# File 'lib/shape_of.rb', line 112 def errors.pretty_inspect end |
#errors ⇒ Object
108 109 110 |
# File 'lib/shape_of.rb', line 108 def errors @errors[:base] end |
#pop_key ⇒ Object
126 127 128 |
# File 'lib/shape_of.rb', line 126 def pop_key @current_error_key_nesting.pop end |
#push_key(key) ⇒ Object
122 123 124 |
# File 'lib/shape_of.rb', line 122 def push_key(key) @current_error_key_nesting.push(key.dup) end |
#valid? ⇒ Boolean
104 105 106 |
# File 'lib/shape_of.rb', line 104 def valid? @errors.empty? end |