diff --git a/README.md b/README.md index 74a0c55..e56bd53 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,44 @@ result.error #=> nil or raises an exception result.type #=> :integer ``` +### Step or Check + +```ruby +result = Zx.Success(5, type: :integer) + .step{ |number| number + 5 } + .on_success(:integer) {|number| puts number } #=> 10 + .on(:success, :integer) {|number| puts number } #=> 10 + .on_success {|number| puts number } #=> 10 + +result.success? #=> true +result.failure? #=> false +result.value #=> 10 +result.value! #=> 10 or raise +result.error #=> nil or raises a n exception +result.type #=> :integer +``` + +```ruby +result = Zx.Success(5, type: :integer) + .step{ |number| number + 5 } + .check { |number| number == 10 } + .on_success{|number| puts number } #=> 10 + +result.success? #=> true +result.failure? #=> false +result.value #=> 10 +result.value! #=> 10 or raise +result.error #=> nil or raises an exception +result.type #=> :integer +``` + +```ruby +result = Zx.Success(5, type: :integer) + .step{ |number| number + 5 } + .check { |number| number == 15 } + .on_failure{|error| puts error } #=> 10 +``` + You can use one or multiples listeners in your result. We see some use cases. **Simple composition** diff --git a/lib/zx/result.rb b/lib/zx/result.rb index 9ea77e2..03189a1 100644 --- a/lib/zx/result.rb +++ b/lib/zx/result.rb @@ -71,6 +71,10 @@ def then(&block) fmap(&block) end + def step(&block) + fmap(&block) + end + def fmap(&block) return self if failure? @@ -80,6 +84,12 @@ def fmap(&block) self end + def check(&block) + return self if !!block.call(@value) + + failure! + end + def failure!(value = nil, type: :error) @type = type.to_sym @success = false @@ -106,7 +116,7 @@ def __execute__(tag = nil, &block) def Success(value = nil, options = {}) success!(value, type: options.fetch(:type, :ok)) end - + def Success!(value = nil, options = {}) success!(value, type: options.fetch(:type, :ok)) end @@ -114,7 +124,7 @@ def Success!(value = nil, options = {}) def Failure(value = nil, options = {}) failure!(value, type: options.fetch(:type, :error)) end - + def Failure!(value = nil, options = {}) failure!(value, type: options.fetch(:type, :error)) end diff --git a/lib/zx/version.rb b/lib/zx/version.rb index c4cca28..b5b4f38 100644 --- a/lib/zx/version.rb +++ b/lib/zx/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Zx - VERSION = '0.0.1'.freeze -end \ No newline at end of file + VERSION = '0.0.2' +end diff --git a/spec/lib/zx_spec.rb b/spec/lib/zx_spec.rb index 648e064..d1eb8a2 100644 --- a/spec/lib/zx_spec.rb +++ b/spec/lib/zx_spec.rb @@ -53,6 +53,43 @@ def apply(value) end end + context 'using step' do + it 'success' do + order = OrderService.new(tax: 0.1) + result = order.apply(100) + + result + .step { |r| r[:price] + 1 } + .step { |r| r + 2 } + .on_success { expect(_1).to eq(113) } + .on_failure { |error| expect(error).to eq(:priceless) } + end + end + + context 'using check' do + it 'success' do + order = OrderService.new(tax: 0.1) + result = order.apply(100) + + result + .step { |r| r[:price] + 1 } + .check { |r| r == 111 } + + expect(result.type).to eq(:ok) + end + + it 'failure' do + order = OrderService.new(tax: 0.1) + result = order.apply(100) + + result + .step { |r| r[:price] + 1 } + .check { |r| r == 112 } + + expect(result.type).to eq(:error) + end + end + context 'as callable' do it 'using Success directly as method' do result = Zx.Success(1)