lib/gamefic/world/playbook.rb in gamefic-2.3.0 vs lib/gamefic/world/playbook.rb in gamefic-2.4.0
- old
+ new
@@ -3,10 +3,12 @@
module Gamefic
module World
# A collection of rules for performing commands.
#
class Playbook
+ ActionHook = Struct.new(:verb, :block)
+
# An array of available syntaxes.
#
# @return [Array<Gamefic::Syntax>]
attr_reader :syntaxes
@@ -45,21 +47,27 @@
def verbs
@commands.keys
end
# Add a proc to be evaluated before a character executes an action.
+ # When a verb is specified, the proc will only be evaluated if the
+ # action's verb matches it.
#
+ # @param verb [Symbol, nil]
# @yieldparam [Gamefic::Action]
- def before_action &block
- @before_actions.push block
+ def before_action verb = nil, &block
+ @before_actions.push ActionHook.new(verb, block)
end
alias validate before_action
# Add a proc to be evaluated after a character executes an action.
+ # When a verb is specified, the proc will only be evaluated if the
+ # action's verb matches it.
#
+ # @param verb [Symbol, nil]
# @yieldparam [Gamefic::Action]
- def after_action &block
- @after_actions.push block
+ def after_action verb = nil, &block
+ @after_actions.push ActionHook.new(verb, block)
end
# Get an Array of all Actions associated with the specified verb.
#
# @param verb [Symbol] The Symbol for the verb (e.g., :go or :look)