class Gamefic::Rulebook::Hooks

A collection of hooks that can be executed before and after an action.

Attributes

after_actions[R]
before_actions[R]

Public Class Methods

new() click to toggle source
# File lib/gamefic/rulebook/hooks.rb, line 12
def initialize
  @before_actions = []
  @after_actions = []
end

Public Instance Methods

after_action(narrative, *verbs, &block) click to toggle source
# File lib/gamefic/rulebook/hooks.rb, line 28
def after_action narrative, *verbs, &block
  after_actions.push Action::Hook.new(verbs, Callback.new(narrative, block))
end
before_action(narrative, *verbs, &block) click to toggle source
# File lib/gamefic/rulebook/hooks.rb, line 24
def before_action narrative, *verbs, &block
  before_actions.push Action::Hook.new(verbs, Callback.new(narrative, block))
end
empty?() click to toggle source
# File lib/gamefic/rulebook/hooks.rb, line 32
def empty?
  before_actions.empty? && after_actions.empty?
end
freeze() click to toggle source
Calls superclass method
# File lib/gamefic/rulebook/hooks.rb, line 17
def freeze
  super
  @before_actions.freeze
  @after_actions.freeze
  self
end
run_after(action) click to toggle source
# File lib/gamefic/rulebook/hooks.rb, line 40
def run_after action
  run_action_hooks action, after_actions
end
run_before(action) click to toggle source
# File lib/gamefic/rulebook/hooks.rb, line 36
def run_before action
  run_action_hooks action, before_actions
end

Private Instance Methods

run_action_hooks(action, hooks) click to toggle source
# File lib/gamefic/rulebook/hooks.rb, line 46
def run_action_hooks action, hooks
  hooks.each do |hook|
    break if action.cancelled?

    next unless hook.match?(action.verb)

    hook.callback.run action
  end
end