Skip to content

Instantly share code, notes, and snippets.

@serradura
Last active August 25, 2020 19:00
Show Gist options
  • Save serradura/eb0dbf5ff1832a3773278a342735e714 to your computer and use it in GitHub Desktop.
Save serradura/eb0dbf5ff1832a3773278a342735e714 to your computer and use it in GitHub Desktop.
u-case ideas - Skip e Chech
module ContractTemplates
module Update
class TryCreateRevision < Micro::Case
attributes :company, :params
def call!
return Success(:skipped_contract_revision) unless term_changed?
ContractRevision.create!(contract_revision_params)
Success(:contract_revision_was_created)
end
private
def term_changed?; end
def contract_revision_params; end
end
end
end
# --
module ContractTemplates
module Update
class TryCreateRevision < Micro::Case
attributes :company, :params
def call!
Skip if: !term_changed?, or: apply(:create_contract_revision)
end
private
def term_changed?; end
def contract_revision_params; end
def create_contract_revision
ContractRevision.create!(contract_revision_params)
Success(:contract_revision_was_created)
end
end
end
end
@MatheusRich
Copy link

We could add an or method on Results. It's basically then, but for failures.

module ContractTemplates
  module Update
    class TryCreateRevision < Micro::Case
      attributes :company, :params

      def call!
        check_term_changed
          .or apply(:create_contract_revision)
      end

      private

        def check_term_changed
          Check { !term_changed? }
        end

        def term_changed?; end
        def contract_revision_params; end

        def create_contract_revision
          ContractRevision.create!(contract_revision_params)

          Success(:contract_revision_was_created)
        end
    end
  end
end

@serradura
Copy link
Author

serradura commented Aug 25, 2020

class User::Registration < Micro::Case
  attributes :email, :password

  def call!
    Skip(if: user_already_exist?, or: apply(:create_user))
      .then(apply(:log))
  end
end

class User::Registration < Micro::Case
  attributes :email, :password

  def call!
    Skip(if: user_already_exist?, or: ::User::Create)
      .then(apply(:log))
  end
end

@serradura
Copy link
Author

class User::Registration < Micro::Case
  attributes :email, :password

  def call!
    Skip(if: user_already_exist?, or: apply(:create_user))
      .then(apply(:log))
  end
end

class User::Registration < Micro::Case
  attributes :email, :password

  def call!
    Check(:email, result: email_already_exist?)
      .then(apply(:create_user))
      .then(apply(:log))
  end
end

@serradura
Copy link
Author

serradura commented Aug 25, 2020

Check

class User::Registration < Micro::Case
  attributes :email, :password

  def call!
    Check(:email, result: email_already_exist?)
      .or(apply(:create_user))
      .then(apply(:log))
  end
end

class User::Registration < Micro::Case
  attributes :email, :password

  def call!
    Check(:email, result: email_already_exist?)
      .then(apply(:create_user))
      .then(apply(:log))
  end
end

Try

class User::Registration < Micro::Case
  attributes :email, :password

  def call!
    Try(result: { user: -> { User.create!(attributes) } })
      .then(apply(:log))
  end
end

class User::Registration < Micro::Case
  attributes :email, :password

  def call!
    Try(result: { user: apply(:create_user) })
      .or(apply(:log))
  end

  def create_user; User.create!(attributes); end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment