Sha256: 9e309987350ed9d85f8aeb8e1ca164c7fa8ec37d69d8fd2bd7be7588002c67fe

Contents?: true

Size: 929 Bytes

Versions: 1

Compression:

Stored size: 929 Bytes

Contents

# frozen_string_literal: true

# This class allows to run service object with context (parent class and custom config)
module Light
  module Services
    class BaseWithContext
      def initialize(service_class, parent_service, config)
        @service_class = service_class
        @config = config
        @parent_service = parent_service

        return if parent_service.nil? || parent_service.is_a?(Light::Services::Base)

        raise Light::Services::ArgTypeError, "#{parent_service.class} - must be a subclass of Light::Services::Base"
      end

      # TODO: Create `run!`
      def run(args = {})
        @service_class.new(extend_arguments(args), @config, @parent_service).tap(&:call)
      end

      private

      def extend_arguments(args)
        return args unless @parent_service

        # TODO: Do we need `.dup` here?
        @parent_service.arguments.extend_with_context(args)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
light-services-2.0.0.beta1 lib/light/services/base_with_context.rb