# 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