Class: LightService::Context

Inherits:
Hash
  • Object
show all
Defined in:
lib/light-service/context.rb,
lib/light-service/context/key_verifier.rb

Overview

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Classes: ExpectedKeyVerifier, KeyVerifier, PromisedKeyVerifier, ReservedKeysVerifier, ReservedKeysViaOrganizerVerifier

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context = {}, outcome = Outcomes::SUCCESS, message = '', error_code = nil) ⇒ Context

rubocop:disable Metrics/ParameterLists, Lint/MissingSuper



13
14
15
16
17
18
19
20
21
22
23
24
# 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
  @skip_all_remaining = false

  context.to_hash.each { |k, v| self[k] = v }
end

Instance Attribute Details

#around_actionsObject

Returns the value of attribute around_actions.



9
10
11
# File 'lib/light-service/context.rb', line 9

def around_actions
  @around_actions
end

#current_actionObject

Returns the value of attribute current_action.



9
10
11
# File 'lib/light-service/context.rb', line 9

def current_action
  @current_action
end

#error_codeObject

Returns the value of attribute error_code.



9
10
11
# File 'lib/light-service/context.rb', line 9

def error_code
  @error_code
end

#messageObject

Returns the value of attribute message.



9
10
11
# File 'lib/light-service/context.rb', line 9

def message
  @message
end

#organized_byObject

Returns the value of attribute organized_by.



9
10
11
# File 'lib/light-service/context.rb', line 9

def organized_by
  @organized_by
end

Class Method Details

.make(context = {}) ⇒ Object

rubocop:enable Metrics/ParameterLists, Lint/MissingSuper



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/light-service/context.rb', line 27

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



151
152
153
154
# File 'lib/light-service/context.rb', line 151

def [](key)
  key = aliases.key(key) || key
  return super(key)
end

#add_to_context(values) ⇒ Object



39
40
41
# File 'lib/light-service/context.rb', line 39

def add_to_context(values)
  merge! values
end

#aliasesObject



147
148
149
# File 'lib/light-service/context.rb', line 147

def aliases
  @aliases ||= {}
end

#assign_aliases(aliases) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/light-service/context.rb', line 139

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



128
129
130
131
132
133
134
135
136
137
# File 'lib/light-service/context.rb', line 128

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

#fail!(message = nil, options_or_error_code = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/light-service/context.rb', line 78

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



96
97
98
99
# File 'lib/light-service/context.rb', line 96

def fail_and_return!(*args)
  fail!(*args)
  throw(:jump_when_failed)
end

#fail_with_rollback!(message = nil, error_code = nil) ⇒ Object



101
102
103
104
# File 'lib/light-service/context.rb', line 101

def fail_with_rollback!(message = nil, error_code = nil)
  fail!(message, error_code)
  raise FailWithRollbackError
end

#failure?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/light-service/context.rb', line 47

def failure?
  success? == false
end

#fetch(key, default = nil, &blk) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/light-service/context.rb', line 156

def fetch(key, default = nil, &blk)
  self[key] ||= if block_given?
                  super(key, &blk)
                else
                  super
                end
end

#inspectObject



164
165
166
167
168
# File 'lib/light-service/context.rb', line 164

def inspect
  "#{self.class}(#{self}, success: #{success?}, message: #{check_nil(message)}, error_code: " \
    "#{check_nil(error_code)}, skip_remaining: #{@skip_remaining}, " \
    "skip_all_remaining: #{@skip_all_remaining}, aliases: #{@aliases})"
end

#outcomeObject



64
65
66
67
68
69
# File 'lib/light-service/context.rb', line 64

def outcome
  warning_msg = '`Context#outcome` attribute reader is ' \
                'DEPRECATED and will be removed'
  LightService::Deprecation.warn(warning_msg)
  @outcome
end

#reset_skip_remaining!Object



59
60
61
62
# File 'lib/light-service/context.rb', line 59

def reset_skip_remaining!
  @message = nil
  @skip_remaining = false
end

#skip_all!(message = nil) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/light-service/context.rb', line 106

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

#skip_all_remaining!(message = nil) ⇒ Object



119
120
121
122
# File 'lib/light-service/context.rb', line 119

def skip_all_remaining!(message = nil)
  @message = message
  @skip_all_remaining = true
end

#skip_all_remaining?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/light-service/context.rb', line 55

def skip_all_remaining?
  @skip_all_remaining
end

#skip_remaining!(message = nil) ⇒ Object



114
115
116
117
# File 'lib/light-service/context.rb', line 114

def skip_remaining!(message = nil)
  @message = message
  @skip_remaining = true
end

#skip_remaining?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/light-service/context.rb', line 51

def skip_remaining?
  @skip_remaining
end

#stop_processing?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/light-service/context.rb', line 124

def stop_processing?
  failure? || skip_remaining? || skip_all_remaining?
end

#succeed!(message = nil, options = {}) ⇒ Object



71
72
73
74
75
76
# File 'lib/light-service/context.rb', line 71

def succeed!(message = nil, options = {})
  @message = Configuration.localization_adapter.success(message,
                                                        current_action,
                                                        options)
  @outcome = Outcomes::SUCCESS
end

#success?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/light-service/context.rb', line 43

def success?
  @outcome == Outcomes::SUCCESS
end