Class: Literal::Failure

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

Constant Summary collapse

EXCEPTION_DECONSTRUCT_KEYS =
[:detailed_message, :backtrace, :backtrace_locations, :cause, :full_message, :message, :exception]
DECONSTRUCTION_TEMPLATE =
EXCEPTION_DECONSTRUCT_KEYS.each_with_object({}) do |key, hash|
	hash[key] = -> (error) { error.send(key) if error.respond_to?(key) }
end

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



51
# File 'lib/literal/failure.rb', line 51

def bind = self

#deconstruct_keys(keys) ⇒ Object



53
54
55
# File 'lib/literal/failure.rb', line 53

def deconstruct_keys(keys)
	DECONSTRUCTION_TEMPLATE.slice(*keys).transform_values { |v| v.call(@value) }
end

#failure(error) ⇒ Literal::Some

Returns:



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/literal/failure.rb', line 22

def failure(error)
	if error === @value
		if block_given?
			yield(@value)
			self
		else
			Literal::Some.new(@value)
		end
	else
		block_given? ? self : Literal::Nothing
	end
end

#failure?true

Returns:

  • (true)


16
# File 'lib/literal/failure.rb', line 16

def failure? = true

#filterObject



47
# File 'lib/literal/failure.rb', line 47

def filter = self

#fmapObject



50
# File 'lib/literal/failure.rb', line 50

def fmap = self

#inspectString

Returns:

  • (String)


10
# File 'lib/literal/failure.rb', line 10

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

#lift(&block) ⇒ Object



57
58
59
# File 'lib/literal/failure.rb', line 57

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

#lift!(&block) ⇒ Object



61
62
63
# File 'lib/literal/failure.rb', line 61

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

#map(type) ⇒ Object



48
# File 'lib/literal/failure.rb', line 48

def map(type) = self

#map_failure(type = Exception) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/literal/failure.rb', line 65

def map_failure(type = Exception)
	output = yield(@value)

	unless Exception === output
		return Literal::Failure.new(
			Literal::TypeError.expected(output, to_be_a: Exception)
		)
	end

	unless type === output
		return Literal::Failure.new(
			Literal::TypeError.expected(output, to_be_a: type)
		)
	end

	Literal::Failure.new(output)
rescue StandardError => e
	Literal::Failure.new(e)
end

#raise!Object

Raises:



35
36
37
# File 'lib/literal/failure.rb', line 35

def raise!
	raise(@value)
end

#successLiteral::NothingClass



19
# File 'lib/literal/failure.rb', line 19

def success = Literal::Nothing

#success?false

Returns:

  • (false)


13
# File 'lib/literal/failure.rb', line 13

def success? = false

#then(type) ⇒ Object



49
# File 'lib/literal/failure.rb', line 49

def then(type) = self

#then_on_failure(result_type) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/literal/failure.rb', line 85

def then_on_failure(result_type)
	output = yield(@value)

	unless Literal::Result(result_type) === output
		return Literal::Failure.new(
			Literal::TypeError.expected(output, to_be_a: Literal::Result(result_type))
		)
	end

	output
rescue StandardError => e
	Literal::Failure.new(e)
end

#value_or {|@value| ... } ⇒ Object

Yields:



43
44
45
# File 'lib/literal/failure.rb', line 43

def value_or
	yield(@value)
end

#value_or_raise!Object

Raises:



39
40
41
# File 'lib/literal/failure.rb', line 39

def value_or_raise!
	raise(@value)
end