Sha256: 1a68997748deb4aad7fb773149e447d7f0c782c2dd465aba28a05e5bb9e122dd

Contents?: true

Size: 973 Bytes

Versions: 4

Compression:

Stored size: 973 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)
        # TODO: Do we need `.dup` here?
        args = @parent_service.arguments.extend_with_context(args) if @parent_service
        args[:deepness] += 1 if args[:deepness]

        args
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
light-services-2.0.0.rc4 lib/light/services/base_with_context.rb
light-services-2.0.0.rc3 lib/light/services/base_with_context.rb
light-services-2.0.0.rc2 lib/light/services/base_with_context.rb
light-services-2.0.0.rc1 lib/light/services/base_with_context.rb