GithubHelp home page GithubHelp logo

caze's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.