Sha256: 2f3e5d75d8e6ca2fd35fd94fc73b3f89fcb5a1075551adc98bf5ea3fe0761819

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

require 'caze/version'
require 'active_support'
require 'active_support/concern'
require 'active_support/core_ext/module/delegation'

module Caze
  class NoTransactionMethodError < StandardError; end
  extend ActiveSupport::Concern

  module ClassMethods
    attr_accessor :transaction_handler

    def has_use_case(use_case_name, use_case_class, options = {})
      transactional = options.fetch(:transactional) { false }

      define_singleton_method(use_case_name, Proc.new do |*args|
        use_case = get_use_case_class(use_case_class)

        if transactional
          handler = self.transaction_handler

          unless handler
            raise NoTransactionMethodError,
              "This action should be executed inside a transaction. But no transaction handler was configured."
          end

          handler.transaction { use_case.send(use_case_name, *args) }
        else
          use_case.send(use_case_name, *args)
        end
      end)
    end

    def export(method_name, options = {})
      method_to_define = options.fetch(:as) { method_name }

      define_singleton_method(method_to_define, Proc.new { |*args|
        use_case_object = args.empty? ? new : new(*args)
        use_case_object.send(method_name)
      })
    end

    private

    def get_use_case_class(use_case_class)
      if use_case_class.respond_to?(:upcase)
        return Object.const_get(use_case_class)
      end

      use_case_class
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
caze-0.1.0 lib/caze.rb