Class: ShapeOf::Validator

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#objectObject (readonly)

Returns the value of attribute object.



93
94
95
# File 'lib/shape_of.rb', line 93

def object
  @object
end

#shapeObject (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(message)
  create_nested_error_structure

  @errors.dig(*@current_error_key_nesting)[:errors] << message.dup
end

#error_messageObject



112
113
114
# File 'lib/shape_of.rb', line 112

def error_message
  errors.pretty_inspect
end

#errorsObject



108
109
110
# File 'lib/shape_of.rb', line 108

def errors
  @errors[:base]
end

#pop_keyObject



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