Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve examples/service_objects #34

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: ' [email protected]',
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 = '[email protected]'

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: ' [email protected]',
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