Sha256: 0dd41085b9764c8c022a941c6e73653fb2920c5c84f9a390b96db665899efe7f

Contents?: true

Size: 1.02 KB

Versions: 6

Compression:

Stored size: 1.02 KB

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

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

      def run!(args = {})
        @config[:raise_on_error] = true
        run(args)
      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

6 entries across 6 versions & 1 rubygems

Version Path
light-services-2.1 lib/light/services/base_with_context.rb
light-services-2.0 lib/light/services/base_with_context.rb
light-services-2.0.0.rc9 lib/light/services/base_with_context.rb
light-services-2.0.0.rc8 lib/light/services/base_with_context.rb
light-services-2.0.0.rc7 lib/light/services/base_with_context.rb
light-services-2.0.0.rc6 lib/light/services/base_with_context.rb