Skip to content

Commit

Permalink
v0.0.2: add suport to check
Browse files Browse the repository at this point in the history
  • Loading branch information
thadeu committed Sep 18, 2023
1 parent fd84ffd commit 2a653a9
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
14 changes: 12 additions & 2 deletions lib/zx/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ def then(&block)
fmap(&block)
end

def step(&block)
fmap(&block)
end

def fmap(&block)
return self if failure?

Expand All @@ -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
Expand All @@ -106,15 +116,15 @@ 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

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
Expand Down
6 changes: 4 additions & 2 deletions lib/zx/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Zx
VERSION = '0.0.1'.freeze
end
VERSION = '0.0.2'
end
37 changes: 37 additions & 0 deletions spec/lib/zx_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2a653a9

Please sign in to comment.