Class: Plumb::Result

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

Direct Known Subclasses

Invalid, Valid

Defined Under Namespace

Classes: Invalid, Valid

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, errors: nil) ⇒ Result

Returns a new instance of Result.



23
24
25
26
# File 'lib/plumb/result.rb', line 23

def initialize(value, errors: nil)
  @value = value
  @errors = errors
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



21
22
23
# File 'lib/plumb/result.rb', line 21

def errors
  @errors
end

#valueObject (readonly)

Returns the value of attribute value.



21
22
23
# File 'lib/plumb/result.rb', line 21

def value
  @value
end

Class Method Details

.invalid(value = nil, errors: nil) ⇒ Object



10
11
12
# File 'lib/plumb/result.rb', line 10

def invalid(value = nil, errors: nil)
  Invalid.new(value, errors:)
end

.valid(value) ⇒ Object



6
7
8
# File 'lib/plumb/result.rb', line 6

def valid(value)
  Valid.new(value)
end

.wrap(value) ⇒ Object



14
15
16
17
18
# File 'lib/plumb/result.rb', line 14

def wrap(value)
  return value if value.is_a?(Result)

  valid(value)
end

Instance Method Details

#inspectObject



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

def inspect
  %(<#{self.class}##{object_id} value:#{value.inspect} errors:#{errors.inspect}>)
end

#invalid(val = value, errors: nil) ⇒ Object



45
46
47
# File 'lib/plumb/result.rb', line 45

def invalid(val = value, errors: nil)
  Result.invalid(val, errors:)
end

#invalid?Boolean

Returns:

  • (Boolean)


29
# File 'lib/plumb/result.rb', line 29

def invalid? = false

#reset(val) ⇒ Object



35
36
37
38
39
# File 'lib/plumb/result.rb', line 35

def reset(val)
  @value = val
  @errors = nil
  self
end

#valid(val = value) ⇒ Object



41
42
43
# File 'lib/plumb/result.rb', line 41

def valid(val = value)
  Result.valid(val)
end

#valid?Boolean

Returns:

  • (Boolean)


28
# File 'lib/plumb/result.rb', line 28

def valid? = true