Class: Plinko::Validation

Inherits:
Object
  • Object
show all
Defined in:
lib/plinko/validation.rb

Direct Known Subclasses

Error, Valid

Defined Under Namespace

Classes: Error, Valid

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, errors = []) ⇒ Validation

Returns a new instance of Validation.



5
6
7
8
# File 'lib/plinko/validation.rb', line 5

def initialize(value, errors = [])
  self.value = value
  self.errors = errors
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



3
4
5
# File 'lib/plinko/validation.rb', line 3

def errors
  @errors
end

#valueObject

Returns the value of attribute value.



3
4
5
# File 'lib/plinko/validation.rb', line 3

def value
  @value
end

Instance Method Details

#with(validator) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/plinko/validation.rb', line 10

def with(validator)
  new_value, validation_errors = validator.validate(value)
  accumulated_errors = errors + Array(validation_errors)

  return Valid.new(new_value) if accumulated_errors.empty?

  Error.new(new_value, accumulated_errors)
end