Class: ReSorcery::Result::Ok

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Ok

Returns a new instance of Ok.



14
15
16
# File 'lib/re_sorcery/result/ok.rb', line 14

def initialize(value)
  @value = value
end

Class Method Details

.new(value) ⇒ Object



7
8
9
10
11
# File 'lib/re_sorcery/result/ok.rb', line 7

def new(value)
  return Result::Err.new("`nil` was provided as a successful result value!") if value.nil?

  super
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  other.class == Ok && other.instance_eval { @value } == @value
end

#and_then(&block) ⇒ Object



18
19
20
# File 'lib/re_sorcery/result/ok.rb', line 18

def and_then(&block)
  ArgCheck['block', block.call(@value), Ok, Err]
end

#as_jsonObject



49
50
51
52
53
54
# File 'lib/re_sorcery/result/ok.rb', line 49

def as_json(*)
  {
    kind: :ok,
    value: @value,
  }
end

#assign(name, &block) ⇒ Object



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

def assign(name, &block)
  raise Error::NonHashAssignError, @value unless @value.is_a?(Hash)

  ArgCheck['block', block.call(@value), Ok, Err]
    .map { |k| @value.merge(name => k) }
end

#cata(ok:, err:) ⇒ Object



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

def cata(ok:, err:)
  ok.call(@value)
end

#map(&block) ⇒ Object



22
23
24
# File 'lib/re_sorcery/result/ok.rb', line 22

def map(&block)
  Ok.new(block.call(@value))
end

#map_errorObject



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

def map_error
  self
end

#or_elseObject



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

def or_else
  self
end