Class: RSchema::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/rschema/result.rb

Overview

The return value when calling a schema

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(valid, value, error) ⇒ Result

Returns a new instance of Result.



24
25
26
27
28
29
# File 'lib/rschema/result.rb', line 24

def initialize(valid, value, error)
  @valid = valid
  @value = value
  @error = error
  freeze
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



44
45
46
# File 'lib/rschema/result.rb', line 44

def error
  @error
end

Class Method Details

.failure(error = nil) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/rschema/result.rb', line 16

def self.failure(error = nil)
  if error.nil?
    NIL_FAILURE
  else
    new(false, nil, error)
  end
end

.success(value = nil) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/rschema/result.rb', line 8

def self.success(value = nil)
  if value.nil?
    NIL_SUCCESS
  else
    new(true, value, nil)
  end
end

Instance Method Details

#invalid?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/rschema/result.rb', line 35

def invalid?
  !valid?
end

#valid?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/rschema/result.rb', line 31

def valid?
  @valid
end

#valueObject

Raises:



39
40
41
42
# File 'lib/rschema/result.rb', line 39

def value
  raise RSchema::Invalid.new(error) if invalid?
  @value
end