Skip to content

Commit

Permalink
Merge pull request #34 from B-CDD/improve-service-objects-example
Browse files Browse the repository at this point in the history
Improve examples/service_objects
  • Loading branch information
serradura authored Mar 7, 2024
2 parents cbe44b4 + ccd84c0 commit 3c5c228
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 62 deletions.
74 changes: 18 additions & 56 deletions examples/service_objects/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,24 @@ end

desc 'creates an account and an owner user through BCDD::Result'
task bcdd_result_transitions: %i[config] do
result = nil

bench = Benchmark.measure do
result = Account::OwnerCreation.call(
owner: {
name: "\tJohn Doe \n",
email: ' JOHN.doe@email.com',
password: '123123123',
password_confirmation: '123123123'
}
)
end

p result

puts "\nBenchmark: #{bench}"
end

desc 'creates an account and an owner user directly through ActiveRecord'
task raw_active_record: %i[config] do
require_relative 'config'

result = nil

bench = Benchmark.measure do
email = 'john.doe@email.com'

ActiveRecord::Base.transaction do
User.exists?(email:) and raise "User with email #{email} already exists"

user = User.create!(
uuid: ::SecureRandom.uuid,
name: 'John Doe',
email:,
password: '123123123',
password_confirmation: '123123123'
)

executed_at = ::Time.current

user.token.nil? or raise "User with email #{email} already has a token"

user.create_token!(
access_token: ::SecureRandom.hex(24),
refresh_token: ::SecureRandom.hex(24),
access_token_expires_at: executed_at + 15.days,
refresh_token_expires_at: executed_at + 30.days
)

account = Account.create!(uuid: ::SecureRandom.uuid)

Account::Member.create!(account: account, user: user, role: :owner)

result = { account: account, user: user }
result1 = Account::OwnerCreation.call(
owner: {
name: "\tJohn Doe \n",
email: ' JOHN.doe@email.com',
password: '123123123',
password_confirmation: '123123123'
}
)

puts result1.inspect
puts

result2 = Account::OwnerCreation.call(
uuid: "",
owner: {}
).on_failure(:invalid_input) do |output|
output[:input].errors.full_messages.each do |message|
puts message
end
end

puts "\nBenchmark: #{bench}"
end
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def call(attributes)

def create_owner(owner:, **)
::User::Creation.call(owner).handle do |on|
on.success { |output| Continue(user: { record: output[:user], token: output[:token] }) }
on.success { |output| Continue(user: output[:user], token: output[:token]) }
on.failure { |output| Failure(:invalid_owner, **output) }
end
end
Expand All @@ -39,7 +39,7 @@ def create_account(uuid:, **)
end

def link_owner_to_account(account:, user:, **)
Member.create!(account:, user: user.fetch(:record), role: :owner)
Member.create!(account:, user:, role: :owner)

Continue()
end
Expand Down
8 changes: 4 additions & 4 deletions examples/service_objects/app/services/application_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class << self
def input=(klass)
const_defined?(:Input, false) and raise ArgumentError, 'Attributes class already defined'

unless klass.is_a?(::Class) && klass < (ApplicationService::Input)
raise ArgumentError, 'must be a ApplicationService::Attributes subclass'
unless klass.is_a?(::Class) && klass < Input
raise ArgumentError, 'must be a ApplicationService::Input subclass'
end

const_set(:Input, klass)
Expand All @@ -26,7 +26,7 @@ def input=(klass)
def input(&block)
return const_get(:Input, false) if const_defined?(:Input, false)

klass = ::Class.new(ApplicationService::Input)
klass = ::Class.new(Input)
klass.class_eval(&block)

self.input = klass
Expand All @@ -52,7 +52,7 @@ def initialize(input)
def call!
::BCDD::Result.transitions(name: self.class.name) do
if input.invalid?
Failure(:invalid_input, **input.errors.messages)
Failure(:invalid_input, input: input)
else
call(input.attributes.deep_symbolize_keys)
end
Expand Down

0 comments on commit 3c5c228

Please sign in to comment.