Sha256: 56c98d2a1096e294835bea73ed1473bdf55c1a08c2454e9263cfdc3f4b5d23b9

Contents?: true

Size: 1.23 KB

Versions: 6

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

# See /README.md
module SimpleRubyService
  module ServiceObject
    extend ActiveSupport::Concern
    include Service

    included do
      class << self
        undef_method :service_methods
      end
    end

    # Class level DSL for convenience.
    class_methods do
      def call(attributes = {}, &callback)
        new(attributes).call(&callback)
      end

      def call!(attributes = {}, &callback)
        new(attributes).call!(&callback)
      end
    end

    # Returns self (for chainability).
    # Memoizes to `value` the result of the blk provided.
    # Evaluates validity prior to executing the block provided.
    def call(&callback)
      self.value = perform(&callback) if valid?

      self
    end

    # Returns #value (i.e. the result of block provided).
    # Raises Invalid if validation reports any errors.
    # Raises Failure if the blk provided reports any errors.
    def call!(&callback)
      call(&callback)
      raise Invalid.new self, errors.full_messages unless valid?
      raise Failure.new self, errors.full_messages unless success?

      value
    end

    protected
    # Abstract method
    def perform
      raise NoMethodError, "#perform must be implemented."
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
simple_ruby_service-1.0.6 lib/simple_ruby_service/service_object.rb
simple_ruby_service-1.0.5 lib/simple_ruby_service/service_object.rb
simple_ruby_service-1.0.4 lib/simple_ruby_service/service_object.rb
simple_ruby_service-1.0.3 lib/simple_ruby_service/service_object.rb
simple_ruby_service-1.0.2 lib/simple_ruby_service/service_object.rb
simple_ruby_service-1.0.1 lib/simple_ruby_service/service_object.rb