Sha256: 30cb4d819f90d0b7dc1cc80e7d04174b3365477af6b1a16ed32f083dd881681c

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

module LightServiceExt
  class ApplicationContext < LightService::Context
    OVERRIDABLE_DEFAULT_KEYS = %i[errors params allow_raise_on_failure].freeze

    class << self
      def make_with_defaults(input = {}, overrides = {})
        allowed_overrides = overrides.slice(*OVERRIDABLE_DEFAULT_KEYS)
        make({ input: input.symbolize_keys }.merge(default_attrs, allowed_overrides))
      end

      private

      def default_attrs
        { errors: {}, params: {}, successful_actions: [], api_responses: [],
          allow_raise_on_failure: LightServiceExt.config.allow_raise_on_failure?,
          internal_only: { error_info: nil } }.freeze
      end
    end

    def add_params(**params)
      return if params.blank?

      self[:params].merge!(params.dup)
    end

    def add_internal_only(**attrs)
      return if attrs.blank?

      self[:internal_only].merge!(attrs.dup)
    end

    def add_errors(**errors)
      return if errors.blank?

      self[:errors].merge!(errors.dup)
      nil
    end

    def add_errors!(**errors)
      return if errors.blank?

      add_errors(**errors)
      fail_and_return!
    end

    def invoked_action
      self[:invoked_action]
    end

    def validation_errors
      self[:errors]
    end

    def allow_raise_on_failure?
      !!self[:allow_raise_on_failure]
    end

    def method_missing(method_name, *arguments, &block)
      return self[method_name] if key?(method_name)

      super
    end

    def respond_to_missing?(method_name, include_private = false)
      key?(method_name) || super
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
light-service-ext-0.1.2 lib/light-service-ext/application_context.rb