Class: RSchema::Result

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

Defined Under Namespace

Classes: InvalidError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(valid, value, error) ⇒ Result

Returns a new instance of Result.



19
20
21
22
23
24
# File 'lib/rschema/result.rb', line 19

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

Class Method Details

.failure(error = nil) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/rschema/result.rb', line 11

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

.success(value = nil) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/rschema/result.rb', line 3

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

Instance Method Details

#errorObject



42
43
44
# File 'lib/rschema/result.rb', line 42

def error
  @error
end

#invalid?Boolean

Returns:

  • (Boolean)


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

def invalid?
  not valid?
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  @valid
end

#valueObject



34
35
36
37
38
39
40
# File 'lib/rschema/result.rb', line 34

def value
  if valid?
    @value
  else
    raise InvalidError
  end
end