Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

Move result classes into their own files

+68 -65
-65
lib/literal/result.rb
··· 1 1 # frozen_string_literal: true 2 2 3 3 class Literal::Result 4 - class Thrown 5 - def initialize(result) 6 - @result = result 7 - freeze 8 - end 9 - 10 - attr_reader :result 11 - end 12 - 13 - class Emitter 14 - def initialize(type:, ball:) 15 - @type = type 16 - @ball = ball 17 - freeze 18 - end 19 - 20 - def success(value) 21 - throw(@ball, Thrown.new(@type.success(value))) 22 - end 23 - 24 - def failure(error) 25 - throw(@ball, Thrown.new(@type.failure(error))) 26 - end 27 - end 28 - 29 - class Generic 30 - include Literal::Type 31 - 32 - def initialize(success_type, failure_type) 33 - @success_type = success_type 34 - @failure_type = failure_type 35 - 36 - freeze 37 - end 38 - 39 - attr_reader :success_type, :failure_type 40 - 41 - def ===(object) 42 - case object 43 - when Literal::Success 44 - @success_type === object.value! 45 - when Literal::Failure 46 - @failure_type === object.error! 47 - end 48 - end 49 - 50 - def success(value) 51 - Literal::Success.new( 52 - value, 53 - success_type: @success_type, 54 - failure_type: @failure_type 55 - ) 56 - end 57 - 58 - def failure(error) 59 - Literal::Failure.new( 60 - error, 61 - success_type: @success_type, 62 - failure_type: @failure_type 63 - ) 64 - end 65 - 66 - freeze 67 - end 68 - 69 4 def handle(&) 70 5 Literal::ResultHandler.new(self).handle(&) 71 6 end
+17
lib/literal/result/emitter.rb
··· 1 + # frozen_string_literal: true 2 + 3 + class Literal::Result::Emitter 4 + def initialize(type:, ball:) 5 + @type = type 6 + @ball = ball 7 + freeze 8 + end 9 + 10 + def success(value) 11 + throw(@ball, Literal::Result::Thrown.new(@type.success(value))) 12 + end 13 + 14 + def failure(error) 15 + throw(@ball, Literal::Result::Thrown.new(@type.failure(error))) 16 + end 17 + end
+41
lib/literal/result/generic.rb
··· 1 + # frozen_string_literal: true 2 + 3 + class Literal::Result::Generic 4 + include Literal::Type 5 + 6 + def initialize(success_type, failure_type) 7 + @success_type = success_type 8 + @failure_type = failure_type 9 + 10 + freeze 11 + end 12 + 13 + attr_reader :success_type, :failure_type 14 + 15 + def ===(object) 16 + case object 17 + when Literal::Success 18 + @success_type === object.value! 19 + when Literal::Failure 20 + @failure_type === object.error! 21 + end 22 + end 23 + 24 + def success(value) 25 + Literal::Success.new( 26 + value, 27 + success_type: @success_type, 28 + failure_type: @failure_type 29 + ) 30 + end 31 + 32 + def failure(error) 33 + Literal::Failure.new( 34 + error, 35 + success_type: @success_type, 36 + failure_type: @failure_type 37 + ) 38 + end 39 + 40 + freeze 41 + end
+10
lib/literal/result/thrown.rb
··· 1 + # frozen_string_literal: true 2 + 3 + class Literal::Result::Thrown 4 + def initialize(result) 5 + @result = result 6 + freeze 7 + end 8 + 9 + attr_reader :result 10 + end