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
# 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

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



26
27
28
29
30
31
32
33
34
35
36
# 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

Instance Method Details

#[](key) ⇒ Object



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

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

#add_to_context(values) ⇒ Object



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

def add_to_context(values)
  merge! values
end

#aliasesObject



137
138
139
# File 'lib/light-service/context.rb', line 137

def aliases
  @aliases ||= {}
end

#assign_aliases(aliases) ⇒ Object



129
130
131
132
133
134
135
# 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

#define_accessor_methods_for_keys(keys) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# 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

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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

#fail_and_return!(*args) ⇒ Object



91
92
93
94
# File 'lib/light-service/context.rb', line 91

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

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



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

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

#failure?Boolean



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

def failure?
  success? == false
end

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



146
147
148
149
150
151
152
# 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

#inspectObject



154
155
156
157
# 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

#outcomeObject



59
60
61
62
63
64
# 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

#reset_skip_remaining!Object



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

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

#skip_all!(message = nil) ⇒ Object



101
102
103
104
105
106
107
# 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

#skip_remaining!(message = nil) ⇒ Object



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

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

#skip_remaining?Boolean



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

def skip_remaining?
  @skip_remaining
end

#stop_processing?Boolean



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

def stop_processing?
  failure? || skip_remaining?
end

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



66
67
68
69
70
71
# 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

#success?Boolean



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

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