Module: PublicCommandMethods

Defined in:
lib/runtime/command.rb

Overview

The PublicCommandMethods module

Constant Summary collapse

RecordNotFoundErrorPattern =
%r{Record not found}.freeze
AttributeFieldReferenceTemplate =
'@%<attribute>s'.freeze

Instance Method Summary collapse

Instance Method Details

#<<(event) ⇒ Object



59
60
61
62
# File 'lib/runtime/command.rb', line 59

def <<(event)
  @successors.add event
  event.antecedent = self
end

#antecedent_cancelled?Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
# File 'lib/runtime/command.rb', line 91

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

#callObject



140
141
142
# File 'lib/runtime/command.rb', line 140

def call
  process self
end

#call_activityObject



135
136
137
138
# File 'lib/runtime/command.rb', line 135

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

#cancelled?Boolean

Returns:

  • (Boolean)


86
87
88
89
# File 'lib/runtime/command.rb', line 86

def cancelled?
  return true if future.nil?
  future.cancelled?
end

#concluded?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/runtime/command.rb', line 99

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

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

Shortcut for using delays



116
117
118
# File 'lib/runtime/command.rb', line 116

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

#elemental?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/runtime/command.rb', line 74

def elemental?
  @type == :elemental
end

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



121
122
123
# File 'lib/runtime/command.rb', line 121

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

#get_value(from, attribute) ⇒ Object



179
180
181
182
# File 'lib/runtime/command.rb', line 179

def get_value(from, attribute)
  return from.send(attribute) if from.respond_to?(attribute)
  from.instance_variable_get(format(AttributeFieldReferenceTemplate, attribute: attribute).to_sym)
end

#immediately?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/runtime/command.rb', line 82

def immediately?
  @when == :immediately
end

#integral?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/runtime/command.rb', line 78

def integral?
  @type == :integral
end

#mandate_cause_record_exists!Object



167
168
169
170
171
172
173
174
175
# File 'lib/runtime/command.rb', line 167

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::CommandCauseRecordNotFoundError, message
  end
  log.warn "Unexpected error refreshing event cause object: #{e.message}"
end

#maybe_log_nilling_warningObject



194
195
196
197
# File 'lib/runtime/command.rb', line 194

def maybe_log_nilling_warning
  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



103
104
105
106
107
# File 'lib/runtime/command.rb', line 103

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

#on_cancelled_antecedentObject



156
157
158
159
# File 'lib/runtime/command.rb', line 156

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

#on_failure(_cause) ⇒ Object



148
149
150
# File 'lib/runtime/command.rb', line 148

def on_failure(_cause)
  cancelled
end

#on_success(_result) ⇒ Object



152
153
154
# File 'lib/runtime/command.rb', line 152

def on_success(_result)
  completed
end

#origin(x = self) ⇒ Object



64
65
66
67
# File 'lib/runtime/command.rb', line 64

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

#parse_activityObject



131
132
133
# File 'lib/runtime/command.rb', line 131

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

#runObject



144
145
146
# File 'lib/runtime/command.rb', line 144

def run
  call
end

#termination(x = self) ⇒ Object



69
70
71
72
# File 'lib/runtime/command.rb', line 69

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

#to_sObject



161
162
163
# File 'lib/runtime/command.rb', line 161

def to_s
  self.identity
end

#warn_when_nilling_noun(from, attribute, value) ⇒ Object



184
185
186
187
188
189
190
191
192
# File 'lib/runtime/command.rb', line 184

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