Class: Gamefic::Rulebook::Hooks

Inherits:
Object
  • Object
show all
Defined in:
lib/gamefic/rulebook/hooks.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHooks

Returns a new instance of Hooks.



12
13
14
15
# File 'lib/gamefic/rulebook/hooks.rb', line 12

def initialize
  @before_actions = []
  @after_actions = []
end

Instance Attribute Details

#after_actionsObject (readonly)

Returns the value of attribute after_actions.



10
11
12
# File 'lib/gamefic/rulebook/hooks.rb', line 10

def after_actions
  @after_actions
end

#before_actionsObject (readonly)

Returns the value of attribute before_actions.



8
9
10
# File 'lib/gamefic/rulebook/hooks.rb', line 8

def before_actions
  @before_actions
end

Instance Method Details

#after_action(narrative, *verbs, &block) ⇒ Object



28
29
30
# 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) ⇒ Object



24
25
26
# 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?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/gamefic/rulebook/hooks.rb', line 32

def empty?
  before_actions.empty? && after_actions.empty?
end

#freezeObject



17
18
19
20
21
22
# File 'lib/gamefic/rulebook/hooks.rb', line 17

def freeze
  super
  @before_actions.freeze
  @after_actions.freeze
  self
end

#run_after(action) ⇒ Object



40
41
42
# File 'lib/gamefic/rulebook/hooks.rb', line 40

def run_after action
  run_action_hooks action, after_actions
end

#run_before(action) ⇒ Object



36
37
38
# File 'lib/gamefic/rulebook/hooks.rb', line 36

def run_before action
  run_action_hooks action, before_actions
end