Module: PublicCommandMethods
- Defined in:
- lib/story_teller/command.rb
Overview
The PublicCommandMethods module
Constant Summary collapse
- RecordNotFoundErrorPattern =
%r{Record not found}.freeze
- AttributeFieldReferenceTemplate =
'@%<attribute>s'.freeze
Instance Method Summary collapse
- #<<(event) ⇒ Object
- #antecedent_cancelled? ⇒ Boolean
- #call ⇒ Object
- #call_activity ⇒ Object
- #cancelled? ⇒ Boolean
- #concluded? ⇒ Boolean
-
#delay(delay, &block) ⇒ Object
(also: #after_delay)
Shortcut for using delays.
- #elemental? ⇒ Boolean
- #finally(&block) ⇒ Object (also: #on_cancel, #when_completed, #when_done, #when_finished, #when_over, #ultimately)
- #get_value(from, attribute) ⇒ Object
- #immediately? ⇒ Boolean
- #integral? ⇒ Boolean
- #mandate_cause_record_exists! ⇒ Object
- #maybe_log_nilling_warning ⇒ Object
- #now(params = {}, &block) ⇒ Object (also: #add_event, #event, #and_then, #chain, #eventually, #later)
- #on_cancelled_antecedent ⇒ Object
- #on_failure(_cause) ⇒ Object
- #on_success(_result) ⇒ Object
- #origin(x = self) ⇒ Object
- #parse_activity ⇒ Object
- #run ⇒ Object
- #termination(x = self) ⇒ Object
- #to_s ⇒ Object
- #warn_when_nilling_noun(from, attribute, value) ⇒ Object
Instance Method Details
#<<(event) ⇒ Object
59 60 61 62 |
# File 'lib/story_teller/command.rb', line 59 def <<(event) @successors.add event event.antecedent = self end |
#antecedent_cancelled? ⇒ Boolean
93 94 95 96 97 98 99 |
# File 'lib/story_teller/command.rb', line 93 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 |
#call ⇒ Object
142 143 144 |
# File 'lib/story_teller/command.rb', line 142 def call process self end |
#call_activity ⇒ Object
137 138 139 140 |
# File 'lib/story_teller/command.rb', line 137 def call_activity return self.activity.call(self) if self.activity.arity == 1 self.activity.call(*self.args) end |
#cancelled? ⇒ Boolean
86 87 88 89 90 91 |
# File 'lib/story_teller/command.rb', line 86 def cancelled? return true if future.nil? return false unless future.respond_to?(:cancelled?) future.cancelled? end |
#concluded? ⇒ Boolean
101 102 103 |
# File 'lib/story_teller/command.rb', line 101 def concluded? semaphore.synchronize { concluded == true } end |
#delay(delay, &block) ⇒ Object Also known as: after_delay
Shortcut for using delays
118 119 120 |
# File 'lib/story_teller/command.rb', line 118 def delay(delay, &block) StoryTeller::Command.new({ cause: cause, antecedent: self, delay: delay }, &block) end |
#elemental? ⇒ Boolean
74 75 76 |
# File 'lib/story_teller/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
123 124 125 |
# File 'lib/story_teller/command.rb', line 123 def finally(&block) StoryTeller::Command.new({ cause: cause, antecedent: self, terminus: true }, &block) end |
#get_value(from, attribute) ⇒ Object
181 182 183 184 |
# File 'lib/story_teller/command.rb', line 181 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
82 83 84 |
# File 'lib/story_teller/command.rb', line 82 def immediately? @when == :immediately end |
#integral? ⇒ Boolean
78 79 80 |
# File 'lib/story_teller/command.rb', line 78 def integral? @type == :integral end |
#mandate_cause_record_exists! ⇒ Object
169 170 171 172 173 174 175 176 177 |
# File 'lib/story_teller/command.rb', line 169 def mandate_cause_record_exists! self.cause.refresh rescue StandardError => e if RecordNotFoundErrorPattern.match?(e.) = e. + ' for cause of event ' + self.to_s raise StoryTeller::CommandCauseRecordNotFoundError, end log.warn "Unexpected error refreshing event cause object: #{e.}" end |
#maybe_log_nilling_warning ⇒ Object
196 197 198 199 |
# File 'lib/story_teller/command.rb', line 196 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
105 106 107 108 109 |
# File 'lib/story_teller/command.rb', line 105 def now(params = {}, &block) # TODO: Determine if :cause should be overwritten # by any :cause params given by the params Hash StoryTeller::Command.new({ cause: cause, antecedent: self }.merge(params), &block) end |
#on_cancelled_antecedent ⇒ Object
158 159 160 161 |
# File 'lib/story_teller/command.rb', line 158 def on_cancelled_antecedent schedule self unless terminus.nil? successors.each(&:on_cancelled_antecedent) end |
#on_failure(_cause) ⇒ Object
150 151 152 |
# File 'lib/story_teller/command.rb', line 150 def on_failure(_cause) cancelled end |
#on_success(_result) ⇒ Object
154 155 156 |
# File 'lib/story_teller/command.rb', line 154 def on_success(_result) completed end |
#origin(x = self) ⇒ Object
64 65 66 67 |
# File 'lib/story_teller/command.rb', line 64 def origin(x = self) x = origin x.antecedent if x.antecedent x.object? ? x : x.cause end |
#parse_activity ⇒ Object
133 134 135 |
# File 'lib/story_teller/command.rb', line 133 def parse_activity self.cause.parse(self.activity) end |
#run ⇒ Object
146 147 148 |
# File 'lib/story_teller/command.rb', line 146 def run call end |
#termination(x = self) ⇒ Object
69 70 71 72 |
# File 'lib/story_teller/command.rb', line 69 def termination(x = self) event = x.successors.first event || x.terminus end |
#to_s ⇒ Object
163 164 165 |
# File 'lib/story_teller/command.rb', line 163 def to_s self.identity end |
#warn_when_nilling_noun(from, attribute, value) ⇒ Object
186 187 188 189 190 191 192 193 194 |
# File 'lib/story_teller/command.rb', line 186 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 |