lib/gamefic/action.rb in gamefic-2.3.0 vs lib/gamefic/action.rb in gamefic-2.4.0
- old
+ new
@@ -30,22 +30,29 @@
run_after_actions
end
# Cancel an action. This method can be called in a before_action hook to
# prevent subsequent hooks and the action itself from being executed.
+ # Cancelling an action in an after_action hook has no effect.
#
def cancel
+ # @todo Emit a warning for attempts to cancel an action after it's been
+ # executed
@cancelled = true
end
# True if the #execute method has been called for this action.
#
# @return [Boolean]
def executed?
@executed
end
+ def cancelled?
+ !@executed && @cancelled
+ end
+
# The verb associated with this action.
#
# @return [Symbol] The symbol representing the verb
def verb
self.class.verb
@@ -89,20 +96,24 @@
def run_before_actions
return unless @with_callbacks
@actor.playbooks
.flat_map(&:before_actions)
- .each do |block|
- block.call(self)
+ .each do |hook|
+ next unless hook.verb.nil? || hook.verb == verb
+ hook.block.call(self)
break if @cancelled
end
end
def run_after_actions
return unless @with_callbacks
@actor.playbooks
.flat_map(&:after_actions)
- .each { |block| block.call(self) }
+ .each do |hook|
+ next unless hook.verb.nil? || hook.verb == verb
+ hook.block.call(self)
+ end
end
class << self
attr_reader :verb