Skip to content

Commit

Permalink
added support to use symbol instead block
Browse files Browse the repository at this point in the history
  • Loading branch information
thadeu committed Dec 18, 2024
1 parent 43c3b07 commit 174763c
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 18 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ class AccountCreation
def self.deliver(input)
new.deliver(input)
end

def self.deliver_with_sym(input)
new.deliver_with_sym(input)
end

def deliver(input)
Given(input)
Expand All @@ -157,6 +161,14 @@ class AccountCreation
.and_then(&method(:subscribe_mailer))
.and_then(&method(:send_welcome_email!))
end

def deliver_with_sym(input)
Given(input)
.and_then(:create_user)
.and_then(:create_account)
.and_then(:subscribe_mailer)
.and_then(:send_welcome_email!)
end

def create_user(input)
user = User.create(name: input[:name], email: input[:email])
Expand Down
2 changes: 1 addition & 1 deletion lib/zx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def Try(input = nil, options = {})
end

def Given(input = nil, options = {}, &block)
Try(input, options, &block)
Try(input, options.merge(ctx: self), &block)
end
end

Expand Down
11 changes: 8 additions & 3 deletions lib/zx/core/and_then.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
module Zx
module Core
class AndThen
def self.spawn(result, &block)
new(result).spawn!(&block)
def self.spawn(result, method_name = nil, &block)
new(result).spawn!(method_name, &block)
end

attr_reader :result
Expand All @@ -13,9 +13,14 @@ def initialize(result)
@result = result
end

def spawn!(&block)
def spawn!(method_name = nil, &block)
lastly = Core::Stack.last(result)

unless block_given?
first = Core::Stack.first(result)
block = first&.ctx.method(method_name) if first&.ctx
end

success = result.success?
type = result.type

Expand Down
2 changes: 2 additions & 0 deletions lib/zx/core/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module Zx
module Core
class Base
attr_reader :ctx

def initialize
@success = true
@type = :ok
Expand Down
10 changes: 8 additions & 2 deletions lib/zx/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ def otherwise(&block)
self
end

def then(&block)
@value, @type, @success = Core::AndThen.spawn(self, &block)
def then(method_name = nil, *_pargs, &block)
@value, @type, @success = Core::AndThen.spawn(
self,
method_name,
&block
)

self
end
Expand All @@ -82,6 +86,7 @@ def failure!(fvalue = nil, options = { type: :error })
@type = (options&.delete(:type) || :error)&.to_sym
@success = false
@value = fvalue
@ctx = options.delete(:ctx) if @ctx.nil?

self
end
Expand All @@ -92,6 +97,7 @@ def success!(svalue = nil, options = {})
@type = (options&.delete(:type) || :ok).to_sym
@success = true
@value = svalue
@ctx = options.delete(:ctx)

self
end
Expand Down
25 changes: 22 additions & 3 deletions spec/lib/zx_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -661,11 +661,30 @@ def expected_error(err, type = nil)
expect { result.value! }.to raise_error(Zx::Result::FailureError)
end

describe 'process composition' do
it 'Given process' do
describe 'process using method blocks' do
it 'returns a Given result' do
input = { name: 'Thadeu Esteves', email: '[email protected]' }

account = AccountCreation.deliver(input)
account = AccountCreation.create_with_method_blocks(input)
.on(:success, :user_created) { |user| p [:user_created, user] }
.on(:success, :account_created) { |acc| p [:account_created, acc] }
.on(:success, :mailer_subscribed) { |acc| p [:mailer_subscribed, acc] }
.on(:success, :email_sent) { |acc| expect(acc.user.name).to eq('Thadeu Esteves') }
.on(:failure, :user_not_created) { |error| p [:user_not_created, error] }
.otherwise { |error| p [:otherwise, error] }

expect(account.success?).to be_truthy
expect(account.type).to eq(:email_sent)
expect(account.unwrap.user.name).to eq('Thadeu Esteves')
expect(account.unwrap.user.email).to eq('[email protected]')
end
end

describe 'process methods symbols' do
it 'returns a Given result' do
input = { name: 'Thadeu Esteves', email: '[email protected]' }

account = AccountCreation.create_with_method_symbols(input)
.on(:success, :user_created) { |user| p [:user_created, user] }
.on(:success, :account_created) { |acc| p [:account_created, acc] }
.on(:success, :mailer_subscribed) { |acc| p [:mailer_subscribed, acc] }
Expand Down
30 changes: 21 additions & 9 deletions spec/support/account_creation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,46 @@ def subscribe!
class AccountCreation
include Zx

def self.deliver(input)
new.deliver(input)
def self.create_with_method_blocks(input)
new.create_with_method_blocks(input)
end

def deliver(input)
def self.create_with_method_symbols(input)
new.create_with_method_symbols(input)
end

def create_with_method_blocks(input)
Given(input)
.and_then(&method(:create_user))
.and_then(&method(:create_account))
.and_then(&method(:subscribe_mailer))
.and_then(&method(:send_welcome_email!))
end

def create_with_method_symbols(input)
Given(input)
.and_then(:create_user)
.and_then(:create_account)
.and_then(:subscribe_mailer)
.and_then(:send_welcome_email!)
end

def create_user(input)
user = User.create(name: input[:name], email: input[:email])
Success(user:, type: :user_created)
Success({ user: }, type: :user_created)
end

def create_account(user:, **)
def create_account(user:)
account = Account.create(user:)
Success(account:, type: :account_created)
Success({ account: }, type: :account_created)
end

def subscribe_mailer(account:, **)
def subscribe_mailer(account:)
NewsletterMailer.subscribe!(account:)
Success(account:, type: :mailer_subscribed)
Success({ account: }, type: :mailer_subscribed)
end

def send_welcome_email!(account:, **)
def send_welcome_email!(account:)
account.send_welcome_email!
Success(account, type: :email_sent)
end
Expand Down

0 comments on commit 174763c

Please sign in to comment.