solid-process/solid-result

View on GitHub
examples/single_listener/Rakefile

Summary

Maintainability
Test Coverage
# frozen_string_literal: true

if RUBY_VERSION <= '3.1'
  puts 'This example requires Ruby 3.1 or higher.'
  exit! 1
end

# Usage:
#
# rake DISABLE_EVENT_LOGS=t
# rake DISABLE_LISTENER=t
#
# rake HIDE_GIVEN_AND_CONTINUE=t
#
# rake BREAK_ACCOUNT_CREATION=t
# rake BREAK_USER_CREATION=t
# rake BREAK_USER_TOKEN_CREATION=t
#
# rake BREAK_ACCOUNT_CREATION=t HIDE_GIVEN_AND_CONTINUE=t
task default: %i[solid_result_event_logs]

task :config do
  require_relative 'config'
end

desc 'creates an account and an owner user through Solid::Result'
task solid_result_event_logs: %i[config] do
  Solid::Result.configuration do |config|
    config.feature.disable!(:event_logs) if ENV['DISABLE_EVENT_LOGS']

    config.event_logs.listener = SingleEventLogsListener unless ENV['DISABLE_LISTENER']
  end

  result = nil

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

  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 }
    end
  end

  puts "\nBenchmark: #{bench}"
end