Module: PublicEventMethods

Includes:
EventConstants
Included in:
Inform::Event
Defined in:
lib/story_teller/events.rb

Overview

The PublicEventMethods module

Constant Summary collapse

RecordNotFoundErrorPattern =
%r{Record not found}.freeze

Constants included from EventConstants

EventConstants::CallerPattern, EventConstants::EventActivityTemplate, EventConstants::EventAntecedantTemplate, EventConstants::EventCauseIdentityTemplate, EventConstants::EventNameTemplate, EventConstants::EventSourceFullTemplate, EventConstants::EventSourceTemplate

Instance Method Summary collapse

Instance Method Details

#<<(event) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/story_teller/events.rb', line 115

def <<(event)
  if @successors.respond_to?(:add)
    @successors.add(event)
  else
    @successors << event
  end
  event.antecedent = self
end

#antecedent_cancelled?Boolean

Returns:

  • (Boolean)


153
154
155
156
157
158
159
# File 'lib/story_teller/events.rb', line 153

def antecedent_cancelled?
  return false if self.antecedent.nil?
  return false unless self.antecedent.cancelled?
  return false if self.terminus.nil?
  log.warn "Event antecedent #{self.antecedent} was cancelled"
  true
end

#callObject

java_signature ‘V call()’



231
232
233
# File 'lib/story_teller/events.rb', line 231

def call
  process(self)
end

#call_activityObject



225
226
227
228
# File 'lib/story_teller/events.rb', line 225

def call_activity
  return self.activity.call(self) if self.activity.arity == 1
  self.activity.call(*self.args)
end

#cancelled?Boolean

Returns:

  • (Boolean)


146
147
148
149
150
151
# File 'lib/story_teller/events.rb', line 146

def cancelled?
  return true if future.nil?
  return false unless future.respond_to?(:cancelled?)

  future.cancelled?
end

#concluded?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/story_teller/events.rb', line 161

def concluded?
  semaphore.synchronize { concluded == true }
end

#contextualize(from) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/story_teller/events.rb', line 90

def contextualize(from)
  # log.info "Initializing context for event #{self} from #{from} (#{from.class})"
  Inform::Context.each_attribute(from) do |attribute, value|
    warn_when_nilling_noun(from, self, attribute, value)
    send(Inform::Context.setter_method(attribute), value)
  end
end

#delay(delay, &block) ⇒ Object Also known as: after_delay

Shortcut for using delays



206
207
208
# File 'lib/story_teller/events.rb', line 206

def delay(delay, &block)
  Inform::Event.new({ cause: cause, antecedent: self, delay: delay }, &block)
end

#elemental?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/story_teller/events.rb', line 134

def elemental?
  @type == :elemental
end

#finally(&block) ⇒ Object Also known as: on_cancel, when_completed, when_done, when_finished, when_over, ultimately



211
212
213
# File 'lib/story_teller/events.rb', line 211

def finally(&block)
  Inform::Event.new({ cause: cause, antecedent: self, terminus: true }, &block)
end

#getObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/story_teller/events.rb', line 165

def get
  if future.respond_to?(:get)
    future.get
  elsif future.respond_to?(:call)
    future.call
  end
  future
rescue SystemExit => e
  raise e
# rescue EventCancelled => e
#   log.warn "Cancelled #{event}"
#   log.debug "Reason for event cancellation: #{e}"
rescue StandardError => e
  log.error "Event execution exception: #{e.message}"
end

#immediately?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/story_teller/events.rb', line 142

def immediately?
  @when == :immediately
end

#inspectObject



257
258
259
# File 'lib/story_teller/events.rb', line 257

def inspect
  format(EventCauseIdentityTemplate, identity: @cause.identity, name: @cause.name)
end

#integral?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/story_teller/events.rb', line 138

def integral?
  @type == :integral
end

#java_getObject



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/story_teller/events.rb', line 181

def java_get
  self.future.get
  self.future
rescue SystemExit => e
  raise e
rescue Java::JavaUtilConcurrent::CancellationException => e
  log.warn "Cancelled #{event}"
  log.debug "Reason for event cancellation: #{e}"
rescue Java::JavaUtilConcurrent::ExecutionException => e
  log.error "Event execution exception: #{e.message}"
end

#mandate_cause_record_exists!Object



263
264
265
266
267
268
269
270
271
# File 'lib/story_teller/events.rb', line 263

def mandate_cause_record_exists!
  self.cause.refresh
rescue StandardError => e
  if RecordNotFoundErrorPattern.match?(e.message)
    message = e.message + ' for cause of event ' + self.to_s
    raise Inform::EventCauseRecordNotFoundError, message
  end
  log.warn "Unexpected error refreshing event cause object: #{e.message}"
end

#maybe_log_nilling_warning(callstack) ⇒ Object

TODO: Remove



110
111
112
113
# File 'lib/story_teller/events.rb', line 110

def maybe_log_nilling_warning(callstack)
  return if callstack.grep('channel_read')
  ['===========', (caller[-4...] + callstack), '==========='].each { |t| log.warn t unless t.nil? }
end

#now(params = {}, &block) ⇒ Object Also known as: add_event, event, and_then, chain, eventually, later



193
194
195
196
197
# File 'lib/story_teller/events.rb', line 193

def now(params = {}, &block)
  # TODO: Determine if :cause should be overwritten
  # by any :cause params given by the params Hash
  Inform::Event.new({ cause: cause, antecedent: self }.merge(params), &block)
end

#on_cancelled_antecedentObject



248
249
250
251
# File 'lib/story_teller/events.rb', line 248

def on_cancelled_antecedent
  schedule self unless terminus.nil?
  successors.each(&:on_cancelled_antecedent)
end

#on_failure(_cause) ⇒ Object



240
241
242
# File 'lib/story_teller/events.rb', line 240

def on_failure(_cause)
  cancelled
end

#on_success(_result) ⇒ Object



244
245
246
# File 'lib/story_teller/events.rb', line 244

def on_success(_result)
  completed
end

#origin(x = self) ⇒ Object



124
125
126
127
# File 'lib/story_teller/events.rb', line 124

def origin(x = self)
  x = origin x.antecedent if x.antecedent
  x.object? ? x : x.cause
end

#parse_activityObject



221
222
223
# File 'lib/story_teller/events.rb', line 221

def parse_activity
  self.cause.parse(self.activity)
end

#runObject

java_signature ‘void run()’



236
237
238
# File 'lib/story_teller/events.rb', line 236

def run
  call
end

#termination(x = self) ⇒ Object



129
130
131
132
# File 'lib/story_teller/events.rb', line 129

def termination(x = self)
  event = x.successors.first
  event || x.terminus
end

#to_sObject



253
254
255
# File 'lib/story_teller/events.rb', line 253

def to_s
  self.identity
end

#warn_when_nilling_noun(from, to, attribute, value) ⇒ Object

TODO: Remove



99
100
101
102
103
104
105
106
107
# File 'lib/story_teller/events.rb', line 99

def warn_when_nilling_noun(from, to, attribute, value)
  return unless attribute == :noun
  return unless value.nil?
  return if (current_value = to.send(attribute)).nil?
  return if attribute == :parameters && current_value <= 1
  log.debug "Overwriting #{to}.#{attribute} (#{current_value}) with #{from}.#{attribute} (nil)!"
  log.debug " #{to}.context: #{to.context.inspect}"
  maybe_log_nilling_warning(to.callstack)
end