Class: Literal::Success

Inherits:
Result show all
Defined in:
lib/literal/success.rb

Instance Attribute Summary

Attributes inherited from Result

#value

Instance Method Summary collapse

Methods inherited from Result

#call, #deconstruct, #initialize

Methods inherited from Monad

#handle

Constructor Details

This class inherits a constructor from Literal::Result

Instance Method Details

#bindObject



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

def bind
	yield @value
rescue StandardError => e
	Literal::Failure.new(e)
end

#deconstruct_keys(keys) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/literal/success.rb', line 91

def deconstruct_keys(keys)
	if @value.respond_to?(:deconstruct_keys)
		@value.deconstruct_keys(keys)
	else
		{ value: @value }
	end
end

#failure(error) ⇒ Literal::NothingClass



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

def failure(error)
	block_given? ? self : Literal::Nothing
end

#failure?false

Returns:

  • (false)


11
# File 'lib/literal/success.rb', line 11

def failure? = false

#filterLiteral::Result

Returns:



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/literal/success.rb', line 41

def filter
	if yield(@value)
		self
	else
		Literal::Failure.new(
			RuntimeError.new(
				"Filter condition not met."
			)
		)
	end
end

#fmapObject



81
82
83
# File 'lib/literal/success.rb', line 81

def fmap
	Literal::Success.new(yield @value)
end

#inspectString

Returns:

  • (String)


5
# File 'lib/literal/success.rb', line 5

def inspect = "Literal::Success(#{@value.inspect})"

#lift(&block) ⇒ Object



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

def lift(*, &block)
	block ? Literal::Lift.new(*, &block).with_success(@value) : self
end

#lift!(&block) ⇒ Object



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

def lift!(*, &block)
	block ? Literal::Lift.new(*, &block).with_success!(@value) : self
end

#map(type) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/literal/success.rb', line 53

def map(type)
	output = yield(@value)

	if type === output
		Literal::Success.new(output)
	else
		Literal::Failure.new(
			Literal::TypeError.expected(output, to_be_a: type)
		)
	end
rescue StandardError => e
	Literal::Failure.new(e)
end

#map_failure(value_type = Exception) ⇒ Object



107
# File 'lib/literal/success.rb', line 107

def map_failure(value_type = Exception) = self

#raise!Object



28
# File 'lib/literal/success.rb', line 28

def raise! = self

#successLiteral::Some

Returns:



14
15
16
17
18
19
20
21
# File 'lib/literal/success.rb', line 14

def success
	if block_given?
		yield(@value)
		self
	else
		Literal::Some.new(@value)
	end
end

#success?true

Returns:

  • (true)


8
# File 'lib/literal/success.rb', line 8

def success? = true

#then(type) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/literal/success.rb', line 67

def then(type)
	output = yield(@value)

	if Literal::Result(type) === output
		output
	else
		Literal::Failure.new(
			Literal::TypeError.expected(output, to_be_a: Literal::Result(type))
		)
	end
rescue StandardError => e
	Literal::Failure.new(e)
end

#then_on_failure(result_type) ⇒ Object



108
# File 'lib/literal/success.rb', line 108

def then_on_failure(result_type) = self

#value_orObject



32
33
34
35
36
37
38
# File 'lib/literal/success.rb', line 32

def value_or
	if block_given?
		@value
	else
		raise Literal::ArgumentError, "Expected block."
	end
end

#value_or_raise!Object



30
# File 'lib/literal/success.rb', line 30

def value_or_raise! = @value