Class: LightService::Context
- Inherits:
-
Hash
- Object
- Hash
- LightService::Context
show all
- Defined in:
- lib/light-service/context.rb,
lib/light-service/context/key_verifier.rb
Overview
rubocop:disable ClassLength
Defined Under Namespace
Classes: ExpectedKeyVerifier, KeyVerifier, PromisedKeyVerifier, ReservedKeysVerifier
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(context = {}, outcome = Outcomes::SUCCESS, message = '', error_code = nil) ⇒ Context
Returns a new instance of Context.
13
14
15
16
17
18
19
20
21
22
23
|
# 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 }
self
end
|
Instance Attribute Details
#current_action ⇒ Object
Returns the value of attribute current_action.
11
12
13
|
# File 'lib/light-service/context.rb', line 11
def current_action
@current_action
end
|
#error_code ⇒ Object
Returns the value of attribute error_code.
11
12
13
|
# File 'lib/light-service/context.rb', line 11
def error_code
@error_code
end
|
#message ⇒ Object
Returns the value of attribute message.
11
12
13
|
# File 'lib/light-service/context.rb', line 11
def message
@message
end
|
Class Method Details
.make(context = {}) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/light-service/context.rb', line 25
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
|
Instance Method Details
#[](key) ⇒ Object
138
139
140
141
|
# File 'lib/light-service/context.rb', line 138
def [](key)
key = aliases.key(key) || key
return super(key)
end
|
#add_to_context(values) ⇒ Object
37
38
39
|
# File 'lib/light-service/context.rb', line 37
def add_to_context(values)
merge! values
end
|
#aliases ⇒ Object
134
135
136
|
# File 'lib/light-service/context.rb', line 134
def aliases
@aliases ||= {}
end
|
#assign_aliases(aliases) ⇒ Object
126
127
128
129
130
131
132
|
# File 'lib/light-service/context.rb', line 126
def assign_aliases(aliases)
@aliases = aliases
aliases.each_pair do |key, key_alias|
self[key_alias] = self[key]
end
end
|
#define_accessor_methods_for_keys(keys) ⇒ Object
117
118
119
120
121
122
123
124
|
# File 'lib/light-service/context.rb', line 117
def define_accessor_methods_for_keys(keys)
return if keys.nil?
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
|
#fail!(message = nil, options_or_error_code = {}) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/light-service/context.rb', line 72
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
|
#fail_and_return!(*args) ⇒ Object
90
91
92
93
|
# File 'lib/light-service/context.rb', line 90
def fail_and_return!(*args)
fail!(*args)
throw(:jump_when_failed, *args)
end
|
#fail_with_rollback!(message = nil, error_code = nil) ⇒ Object
95
96
97
98
|
# File 'lib/light-service/context.rb', line 95
def fail_with_rollback!(message = nil, error_code = nil)
fail!(message, error_code)
raise FailWithRollbackError
end
|
#failure? ⇒ Boolean
45
46
47
|
# File 'lib/light-service/context.rb', line 45
def failure?
success? == false
end
|
#fetch(key, default_or_block = nil) ⇒ Object
143
144
145
|
# File 'lib/light-service/context.rb', line 143
def fetch(key, default_or_block = nil)
self[key] ||= super(key, default_or_block)
end
|
#inspect ⇒ Object
147
148
149
150
151
152
153
154
155
|
# File 'lib/light-service/context.rb', line 147
def inspect
"#{self.class}(#{self}, " \
+ "success: #{success?}, " \
+ "message: #{check_nil(message)}, " \
+ "error_code: #{check_nil(error_code)}, " \
+ "skip_remaining: #{@skip_remaining}, " \
+ "aliases: #{@aliases}" \
+ ")"
end
|
#outcome ⇒ Object
58
59
60
61
62
63
|
# File 'lib/light-service/context.rb', line 58
def outcome
msg = '`Context#outcome` attribute reader is ' \
'DEPRECATED and will be removed'
ActiveSupport::Deprecation.warn(msg)
@outcome
end
|
#reset_skip_remaining! ⇒ Object
53
54
55
56
|
# File 'lib/light-service/context.rb', line 53
def reset_skip_remaining!
@message = nil
@skip_remaining = false
end
|
#skip_all!(message = nil) ⇒ Object
100
101
102
103
104
105
106
|
# File 'lib/light-service/context.rb', line 100
def skip_all!(message = nil)
warning_msg = "Using skip_all! has been deprecated, " \
"please use `skip_remaining!` instead."
ActiveSupport::Deprecation.warn(warning_msg)
skip_remaining!(message)
end
|
#skip_remaining!(message = nil) ⇒ Object
108
109
110
111
|
# File 'lib/light-service/context.rb', line 108
def skip_remaining!(message = nil)
@message = message
@skip_remaining = true
end
|
#skip_remaining? ⇒ Boolean
49
50
51
|
# File 'lib/light-service/context.rb', line 49
def skip_remaining?
@skip_remaining
end
|
#stop_processing? ⇒ Boolean
113
114
115
|
# File 'lib/light-service/context.rb', line 113
def stop_processing?
failure? || skip_remaining?
end
|
#succeed!(message = nil, options = {}) ⇒ Object
65
66
67
68
69
70
|
# File 'lib/light-service/context.rb', line 65
def succeed!(message = nil, options = {})
@message = Configuration.localization_adapter.success(message,
current_action,
options)
@outcome = Outcomes::SUCCESS
end
|
#success? ⇒ Boolean
41
42
43
|
# File 'lib/light-service/context.rb', line 41
def success?
@outcome == Outcomes::SUCCESS
end
|