Class: Literal::Failure

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

Defined Under Namespace

Classes: Generic

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Result

#handle

Constructor Details

#initialize(error, success_type:, failure_type:) ⇒ Failure

Returns a new instance of Failure.



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

def initialize(error, success_type:, failure_type:)
	@error = error

	@success_type = success_type
	@failure_type = failure_type

	Literal.check(error, failure_type)

	freeze
end

Instance Attribute Details

#failure_typeObject (readonly)

Returns the value of attribute failure_type.



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

def failure_type
  @failure_type
end

#success_typeObject (readonly)

Returns the value of attribute success_type.



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

def success_type
  @success_type
end

Instance Method Details

#deconstructObject



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

def deconstruct
	[@error]
end

#deconstruct_keys(keys) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/literal/failure.rb', line 54

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

#error!Object



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

def error!
	@error
end

#failure?Boolean

Returns:

  • (Boolean)


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

def failure?
	true
end

#map(type) ⇒ Object

Raises:



66
67
68
69
70
71
72
73
74
# File 'lib/literal/failure.rb', line 66

def map(type)
	raise ArgumentError unless block_given?

	Literal::Failure.new(
		@error,
		success_type: type,
		failure_type: @failure_type
	)
end

#success?Boolean

Returns:

  • (Boolean)


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

def success?
	false
end

#thenObject

Raises:



76
77
78
79
# File 'lib/literal/failure.rb', line 76

def then
	raise ArgumentError unless block_given?
	self
end

#value!Object



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

def value!
	raise Literal::ArgumentError.new("Failure has no value")
end

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

Yields:

  • (@error)

Raises:



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

def value_or
	raise ArgumentError unless block_given?
	yield(@error)
end