Class: Fear::Success

Inherits:
Object
  • Object
show all
Includes:
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 Try

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

Constructor Details

#initialize(value) ⇒ Success

Returns a new instance of Success.

Parameters:

  • (any)


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

def initialize(value)
  @value = value
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • other (Any)

Returns:

  • (Boolean)


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

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

#deconstruct<any>

Returns:

  • (<any>)


112
113
114
# File 'lib/fear/success.rb', line 112

def deconstruct
  [value]
end

#failure?false

Returns:

  • (false)


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

def failure?
  false
end

#flat_mapTry

Returns:



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

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

#flattenTry

Returns:



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

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

#getany

Returns:

  • (any)


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

def get
  @value
end

#inspectString Also known as: to_s

Returns:

  • (String)


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

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

#mapTry

Returns:



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

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

#or_elseSuccess

Returns:



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

def or_else
  self
end

#recoverSuccess

Returns:



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

def recover
  self
end

#recover_withSuccess

Returns:



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

def recover_with
  self
end

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

Yield Parameters:

  • value (any)

Yield Returns:

  • (Boolean)

Returns:



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

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

Returns:

  • (Boolean)


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

def success?
  true
end

#to_eitherRight

Returns:



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

def to_either
  Right.new(value)
end