Class: Ni::Context
- Inherits:
-
Object
show all
- Defined in:
- lib/ni/context.rb
Defined Under Namespace
Classes: Errors
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(interactor, action, system_uid = nil) ⇒ Context
Returns a new instance of Context.
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/ni/context.rb', line 41
def initialize(interactor, action, system_uid = nil)
self.external_system_uid = system_uid
self.system_uid = system_uid || SecureRandom.hex(15)
self.current_interactor = interactor
self.current_action = action
self.errors = Errors.new
self.as_result = false
@data = {}
@success = true
@terminated = false
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
209
210
211
212
213
214
215
|
# File 'lib/ni/context.rb', line 209
def method_missing(name, *args, &block)
if name.to_s.ends_with?('=')
self[name[0..-2].to_sym] = args.first
else
self[name]
end
end
|
Instance Attribute Details
#as_result ⇒ Object
Returns the value of attribute as_result.
36
37
38
|
# File 'lib/ni/context.rb', line 36
def as_result
@as_result
end
|
#continue_from ⇒ Object
Returns the value of attribute continue_from.
36
37
38
|
# File 'lib/ni/context.rb', line 36
def continue_from
@continue_from
end
|
#current_action ⇒ Object
Returns the value of attribute current_action.
35
36
37
|
# File 'lib/ni/context.rb', line 35
def current_action
@current_action
end
|
#current_interactor ⇒ Object
Returns the value of attribute current_interactor.
35
36
37
|
# File 'lib/ni/context.rb', line 35
def current_interactor
@current_interactor
end
|
#errors ⇒ Object
Returns the value of attribute errors.
35
36
37
|
# File 'lib/ni/context.rb', line 35
def errors
@errors
end
|
#external_system_uid ⇒ Object
Returns the value of attribute external_system_uid.
36
37
38
|
# File 'lib/ni/context.rb', line 36
def external_system_uid
@external_system_uid
end
|
#halt_execution ⇒ Object
Returns the value of attribute halt_execution.
37
38
39
|
# File 'lib/ni/context.rb', line 37
def halt_execution
@halt_execution
end
|
#skip_the_rest_chain ⇒ Object
Returns the value of attribute skip_the_rest_chain.
37
38
39
|
# File 'lib/ni/context.rb', line 37
def skip_the_rest_chain
@skip_the_rest_chain
end
|
#system_uid ⇒ Object
Returns the value of attribute system_uid.
36
37
38
|
# File 'lib/ni/context.rb', line 36
def system_uid
@system_uid
end
|
Instance Method Details
#[](name) ⇒ Object
155
156
157
158
159
160
161
|
# File 'lib/ni/context.rb', line 155
def [](name)
unless allow_to_read?(name)
raise "The `#{name}` is not allowed to read"
end
raw_get(name)
end
|
#[]=(name, value) ⇒ Object
163
164
165
166
167
168
169
|
# File 'lib/ni/context.rb', line 163
def []=(name, value)
unless allow_to_write?(name)
raise "The `#{name}` is not allowed to write"
end
raw_set(name, value)
end
|
#allow_to_read?(name) ⇒ Boolean
201
202
203
|
# File 'lib/ni/context.rb', line 201
def allow_to_read?(name)
as_result || self.current_interactor.allow_to_read?(self.current_action, name)
end
|
#allow_to_write?(name) ⇒ Boolean
205
206
207
|
# File 'lib/ni/context.rb', line 205
def allow_to_write?(name)
as_result || self.current_interactor.allow_to_write?(self.current_action, name)
end
|
#assign_current_interactor!(interactor) ⇒ Object
94
95
96
|
# File 'lib/ni/context.rb', line 94
def assign_current_interactor!(interactor)
self.current_interactor = interactor
end
|
#assign_data!(params) ⇒ Object
90
91
92
|
# File 'lib/ni/context.rb', line 90
def assign_data!(params)
@data.merge!(params)
end
|
77
78
79
|
# File 'lib/ni/context.rb', line 77
def can_perform_next_step?
success? && !terminated?
end
|
#cancel! ⇒ Object
73
74
75
|
# File 'lib/ni/context.rb', line 73
def cancel!
@success = false
end
|
#canceled? ⇒ Boolean
61
62
63
|
# File 'lib/ni/context.rb', line 61
def canceled?
!@success && valid?
end
|
#chain_skipped? ⇒ Boolean
115
116
117
118
119
120
|
# File 'lib/ni/context.rb', line 115
def chain_skipped?
skip_the_rest_chain.present?
ensure
self.skip_the_rest_chain = false
end
|
#continue_from!(name_or_interactor) ⇒ Object
98
99
100
101
|
# File 'lib/ni/context.rb', line 98
def continue_from!(name_or_interactor)
name = name_or_interactor.is_a?(Class) ? name_or_interactor.interactor_id! : name_or_interactor
self.continue_from = name
end
|
#execution_halted? ⇒ Boolean
107
108
109
|
# File 'lib/ni/context.rb', line 107
def execution_halted?
halt_execution.present?
end
|
#failed? ⇒ Boolean
57
58
59
|
# File 'lib/ni/context.rb', line 57
def failed?
!@success || !valid?
end
|
#failure! ⇒ Object
69
70
71
|
# File 'lib/ni/context.rb', line 69
def failure!
@success = false
end
|
#fetch(*args) ⇒ Object
197
198
199
|
# File 'lib/ni/context.rb', line 197
def fetch(*args)
args.map { |name| self[name] }
end
|
#halt_execution! ⇒ Object
103
104
105
|
# File 'lib/ni/context.rb', line 103
def halt_execution!
self.halt_execution = true
end
|
#has_key?(name) ⇒ Boolean
185
186
187
|
# File 'lib/ni/context.rb', line 185
def has_key?(name)
@data.has_key?(name.to_sym)
end
|
#invalid? ⇒ Boolean
193
194
195
|
# File 'lib/ni/context.rb', line 193
def invalid?
!valid?
end
|
#raw_get(name) ⇒ Object
raw_get was defined only for internal purposes only. Please do not use it in your code
172
173
174
|
# File 'lib/ni/context.rb', line 172
def raw_get(name)
@data[name.to_sym]
end
|
#raw_set(name, value) ⇒ Object
raw_set was defined only for internal purposes only. Please do not use it in your code
177
178
179
|
# File 'lib/ni/context.rb', line 177
def raw_set(name, value)
@data[name.to_sym] = value
end
|
#respond_to?(name, include_private = false) ⇒ Boolean
181
182
183
|
# File 'lib/ni/context.rb', line 181
def respond_to?(name, include_private=false)
super || has_key?(name)
end
|
#resultify! ⇒ Object
134
135
136
137
138
|
# File 'lib/ni/context.rb', line 134
def resultify!
self.as_result = true
self
end
|
#should_be_restored? ⇒ Boolean
130
131
132
|
# File 'lib/ni/context.rb', line 130
def should_be_restored?
wait_for_execution? || self.external_system_uid.present?
end
|
#skip_the_rest_chain! ⇒ Object
111
112
113
|
# File 'lib/ni/context.rb', line 111
def skip_the_rest_chain!
self.skip_the_rest_chain = true
end
|
#success! ⇒ Object
81
82
83
|
# File 'lib/ni/context.rb', line 81
def success!
@terminated = true
end
|
#success? ⇒ Boolean
53
54
55
|
# File 'lib/ni/context.rb', line 53
def success?
@success && valid?
end
|
#terminate! ⇒ Object
85
86
87
88
|
# File 'lib/ni/context.rb', line 85
def terminate!
@success = false
@terminated = true
end
|
#terminated? ⇒ Boolean
65
66
67
|
# File 'lib/ni/context.rb', line 65
def terminated?
@terminated
end
|
#valid? ⇒ Boolean
189
190
191
|
# File 'lib/ni/context.rb', line 189
def valid?
errors.empty?
end
|
#wait_completed! ⇒ Object
122
123
124
|
# File 'lib/ni/context.rb', line 122
def wait_completed!
self.continue_from = nil
end
|
#wait_for_execution? ⇒ Boolean
126
127
128
|
# File 'lib/ni/context.rb', line 126
def wait_for_execution?
continue_from.present?
end
|
#within_interactor(interactor, action) ⇒ Object
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/ni/context.rb', line 140
def within_interactor(interactor, action)
stored_interactor = self.current_interactor
stored_action = self.current_action
self.current_interactor = interactor
self.current_action = action
result = yield
self.current_interactor = stored_interactor
self.current_action = stored_action
result
end
|