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)


19
20
21
# File 'lib/fear/success.rb', line 19

def initialize(value)
  @value = value
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • other (Any)

Returns:

  • (Boolean)


96
97
98
# File 'lib/fear/success.rb', line 96

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

#failure?false

Returns:

  • (false)


34
35
36
# File 'lib/fear/success.rb', line 34

def failure?
  false
end

#flat_mapTry

Returns:



83
84
85
86
87
# File 'lib/fear/success.rb', line 83

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

#flattenTry

Returns:



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

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

#getany

Returns:

  • (any)


24
25
26
# File 'lib/fear/success.rb', line 24

def get
  @value
end

#inspectString Also known as: to_s

Returns:

  • (String)


101
102
103
# File 'lib/fear/success.rb', line 101

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

#mapTry

Returns:



76
77
78
79
80
# File 'lib/fear/success.rb', line 76

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

#or_elseSuccess

Returns:



39
40
41
# File 'lib/fear/success.rb', line 39

def or_else
  self
end

#recoverSuccess

Returns:



71
72
73
# File 'lib/fear/success.rb', line 71

def recover
  self
end

#recover_withSuccess

Returns:



66
67
68
# File 'lib/fear/success.rb', line 66

def recover_with
  self
end

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

Yield Parameters:

  • value (any)

Yield Returns:

  • (Boolean)

Returns:



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

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)


29
30
31
# File 'lib/fear/success.rb', line 29

def success?
  true
end

#to_eitherRight

Returns:



90
91
92
# File 'lib/fear/success.rb', line 90

def to_either
  Right.new(value)
end