Sha256: 7eefd3add73ac07a9f1b992142aab1f385a1a34f47311268e5e73302e3bc465d

Contents?: true

Size: 998 Bytes

Versions: 2

Compression:

Stored size: 998 Bytes

Contents

# frozen_string_literal: true

module Marko
  # Service like ServiceObject, Command, etc.
  #
  # @example just call without parameters
  #   class BoldService < Service
  #     def call
  #       42
  #     end
  #   end
  #
  # @example with parameters
  #   class PlusService < Service
  #     def initialize(a, b)
  #       @a, @b = a, b
  #     end
  #
  #     def call
  #       42
  #     end
  #   end
  #
  class Service
    Failure = Class.new(StandardError)

    class << self
      def call(*args, **kwargs, &block)
        new(*args, **kwargs, &block).call
      end

      def inherited(klass)
        klass.const_set(:Failure, Class.new(klass::Failure))
        super
      end
    end

    private_class_method :new

    def initialize(*args, **kwargs, &block)
      @args = args
      @kwargs = kwargs
      @block = block
    end

    def call
      fail Failure, "#{self.class.name}#call must be overrided"
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
marko-0.3.0 lib/marko/gadgets/service.rb
marko-0.1.0 lib/marko/gadgets/service.rb