Class: Fear::Success

Inherits:
Object
  • Object
show all
Includes:
RightBiased::Right, Try
Defined in:
lib/fear/success.rb

Constant Summary collapse

EXTRACTOR =
proc do |try|
  if Fear::Success === try
    Fear.some([try.get])
  else
    Fear.none
  end
end

Instance Method Summary collapse

Methods included from RightBiased::Right

#===, #any?, #each, #get_or_else, #include?, included, #to_option

Methods included from Try

#any?, #each, #get_or_else, #include?, #left_class, #match, matcher, #right_class, #to_option

Constructor Details

#initialize(value) ⇒ Success


21
22
23
# File 'lib/fear/success.rb', line 21

def initialize(value)
  @value = value
end

Instance Method Details

#==(other) ⇒ Boolean


98
99
100
# File 'lib/fear/success.rb', line 98

def ==(other)
  other.is_a?(Success) && value == other.value
end

#failure?false


36
37
38
# File 'lib/fear/success.rb', line 36

def failure?
  false
end

#flat_mapTry


85
86
87
88
89
# File 'lib/fear/success.rb', line 85

def flat_map
  super
rescue StandardError => error
  Failure.new(error)
end

#flattenTry


46
47
48
49
50
51
52
# File 'lib/fear/success.rb', line 46

def flatten
  if value.is_a?(Try)
    value.flatten
  else
    self
  end
end

#getany


26
27
28
# File 'lib/fear/success.rb', line 26

def get
  @value
end

#inspectString Also known as: to_s


103
104
105
# File 'lib/fear/success.rb', line 103

def inspect
  "#<Fear::Success value=#{value.inspect}>"
end

#mapTry


78
79
80
81
82
# File 'lib/fear/success.rb', line 78

def map
  super
rescue StandardError => error
  Failure.new(error)
end

#or_elseSuccess


41
42
43
# File 'lib/fear/success.rb', line 41

def or_else
  self
end

#recoverSuccess


73
74
75
# File 'lib/fear/success.rb', line 73

def recover
  self
end

#recover_withSuccess


68
69
70
# File 'lib/fear/success.rb', line 68

def recover_with
  self
end

#select {|value| ... } ⇒ Try

Yield Parameters:

  • value (any)

Yield Returns:

  • (Boolean)

57
58
59
60
61
62
63
64
65
# File 'lib/fear/success.rb', line 57

def select
  if yield(value)
    self
  else
    raise NoSuchElementError, "Predicate does not hold for `#{value}`"
  end
rescue StandardError => error
  Failure.new(error)
end

#success?Boolean


31
32
33
# File 'lib/fear/success.rb', line 31

def success?
  true
end

#to_eitherRight


92
93
94
# File 'lib/fear/success.rb', line 92

def to_either
  Right.new(value)
end