class LightService::Context
rubocop:disable Metrics/ClassLength
Attributes
Public Class Methods
Source
# File lib/light-service/context.rb, line 26 def self.make(context = {}) unless context.is_a?(Hash) || context.is_a?(LightService::Context) msg = 'Argument must be Hash or LightService::Context' raise ArgumentError, msg end context = new(context) unless context.is_a?(Context) context.assign_aliases(context.delete(:_aliases)) if context[:_aliases] context end
rubocop:enable Metrics/ParameterLists, Lint/MissingSuper
Source
# File lib/light-service/context.rb, line 13 def initialize(context = {}, outcome = Outcomes::SUCCESS, message = '', error_code = nil) @outcome = outcome @message = message @error_code = error_code @skip_remaining = false context.to_hash.each { |k, v| self[k] = v } end
rubocop:disable Metrics/ParameterLists, Lint/MissingSuper
Public Instance Methods
Source
# File lib/light-service/context.rb, line 141 def [](key) key = aliases.key(key) || key return super(key) end
Calls superclass method
Source
# File lib/light-service/context.rb, line 38 def add_to_context(values) merge! values end
Source
# File lib/light-service/context.rb, line 129 def assign_aliases(aliases) @aliases = aliases aliases.each_pair do |key, key_alias| self[key_alias] = self[key] end end
Source
# File lib/light-service/context.rb, line 118 def define_accessor_methods_for_keys(keys) return if keys.empty? Array(keys).each do |key| next if respond_to?(key.to_sym) define_singleton_method(key.to_s) { fetch(key) } define_singleton_method("#{key}=") { |value| self[key] = value } end end
Source
# File lib/light-service/context.rb, line 73 def fail!(message = nil, options_or_error_code = {}) options_or_error_code ||= {} if options_or_error_code.is_a?(Hash) error_code = options_or_error_code.delete(:error_code) options = options_or_error_code else error_code = options_or_error_code options = {} end @message = Configuration.localization_adapter.failure(message, current_action, options) @error_code = error_code @outcome = Outcomes::FAILURE end
Source
# File lib/light-service/context.rb, line 91 def fail_and_return!(*args) fail!(*args) throw(:jump_when_failed) end
Source
# File lib/light-service/context.rb, line 96 def fail_with_rollback!(message = nil, error_code = nil) fail!(message, error_code) raise FailWithRollbackError end
Source
# File lib/light-service/context.rb, line 146 def fetch(key, default = nil, &blk) self[key] ||= if block_given? super(key, &blk) else super end end
Calls superclass method
Source
# File lib/light-service/context.rb, line 154 def inspect "#{self.class}(#{self}, success: #{success?}, message: #{check_nil(message)}, error_code: " \ "#{check_nil(error_code)}, skip_remaining: #{@skip_remaining}, aliases: #{@aliases})" end
Source
# File lib/light-service/context.rb, line 59 def outcome warning_msg = '`Context#outcome` attribute reader is ' \ 'DEPRECATED and will be removed' LightService::Deprecation.warn(warning_msg) @outcome end
Source
# File lib/light-service/context.rb, line 54 def reset_skip_remaining! @message = nil @skip_remaining = false end
Source
# File lib/light-service/context.rb, line 101 def skip_all!(message = nil) warning_msg = "Using skip_all! has been deprecated, " \ "please use `skip_remaining!` instead." LightService::Deprecation.warn(warning_msg) skip_remaining!(message) end
Source
# File lib/light-service/context.rb, line 109 def skip_remaining!(message = nil) @message = message @skip_remaining = true end
Source
# File lib/light-service/context.rb, line 50 def skip_remaining? @skip_remaining end
Source
# File lib/light-service/context.rb, line 114 def stop_processing? failure? || skip_remaining? end
Source
# File lib/light-service/context.rb, line 66 def succeed!(message = nil, options = {}) @message = Configuration.localization_adapter.success(message, current_action, options) @outcome = Outcomes::SUCCESS end
Source
# File lib/light-service/context.rb, line 42 def success? @outcome == Outcomes::SUCCESS end
Private Instance Methods
Source
# File lib/light-service/context.rb, line 161 def check_nil(value) return 'nil' unless value "'#{value}'" end