Class: Validation

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

Overview

Conducts several function calls which do checks, of which each must return Success or Failure and returns a list of all failures. Validation is not a monad, but an deemed an applicative functor

Instance Method Summary collapse

Constructor Details

#initializeValidation

Returns a new instance of Validation.



9
10
11
# File 'lib/monadic/validation.rb', line 9

def initialize
  @result = Success([])
end

Instance Method Details

#call(&block) ⇒ Object



13
14
15
# File 'lib/monadic/validation.rb', line 13

def call(&block)
  instance_eval(&block)
end

#check(proc = nil, &block) ⇒ Object

Raises:



17
18
19
20
21
22
23
# File 'lib/monadic/validation.rb', line 17

def check(proc=nil, &block)
  result = (proc || block).call
  raise NotEitherError, "Expected #{result.inspect} to be an Either" unless result.is_a? Either
  @result = Failure(@result.fetch << result.fetch) if result.failure?

  @result
end