Skip to content
A DSL to define use cases
Ruby
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
spec Rewrite how we wrap exceptions from use cases Dec 15, 2017
.gitignore Creating use case support to define use cases pragmatically Nov 26, 2014
.travis.yml
CHANGELOG.md
Gemfile renaming to Cazé Feb 5, 2015
LICENSE.txt
README.md
Rakefile
caze.gemspec

README.md

Caze

Build status Gem version Downloads

This is a simple DSL to declare use cases as entry points of a module. The purpose is to avoid the verbose declarations.

Usage

Instead of doing this:

module Project
  def self.sum(x, y)
    UseCases::Sum.sum(x, y)
  end

  def self.subtract(x, y)
    UseCases::Subtract.subtract(x, y)
  end
end

You can do this:

require 'caze'

module Project
  include Caze

  has_use_case :sum, UseCases::Sum
  has_use_case :subtract, UseCases::Subtract
end

Using transactions

You can use transactions in your use cases by providing a transaction_handler in your module. The only method that transaction handler should respond is #transaction.

Project.transaction_handler = ActiveRecord::Base

While declaring which use cases your app has, you can set the option transactional to true.

has_use_case :wow, UseCases::Wow, transactional: true

Note that the transaction handler must implement #transaction and return the value inside the block. It will also be responsible for handle errors and rollback if necessary.

Using exception interceptor

You can use the raise use case exception in order to know the use case name when an error happen.

While declaring which use case your app has, you can set the option raise_use_case_exception to true.

has_use_case :say_my_name, UseCases::SayMyName, raise_use_case_exception: true

It should intercept yours exceptions and write the use case name on it following the pattern: SayMyName: This is you error message Heisenberg

Exporting instance methods as class methods

Inside the use case classes you can use the .export method, so in the UseCases::Sum instead of this:

module Project
  module UseCases
    class Sum
      def self.sum(x, y)
        new(x, y).sum
      end

      def initialize(x, y)
        @x = x
        @y = y
      end

      def sum
        x + y
      end
    end
  end
end

You can define a class method based on an instance method with export:

module Project
  module UseCases
    class Foo
      include Caze

      export :sum, as: :execute

      def initialize(x, y)
        @x = x
        @y = y
      end

      def sum
        x + y
      end
    end
  end
end

The as param, tells how the class method must be named, if it is not passed the class method will have the same name of the instance method.

With this you can call your project use cases without the need to know its internals:

Project.sum(4, 2) # This will call sum inside the use case `UseCases::Sum`

Installation

Add to your Gemfile:

gem 'caze', '~> 0.4.0'

Run bundle install.

Apache License 2.0

Check LICENSE.txt

You can’t perform that action at this time.