Class: Gamefic::Action

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/gamefic/action.rb

Overview

The handler for executing responses for a provided actor and array of arguments. It’s also responsible for executing before_action and after_action hooks if necessary.

Defined Under Namespace

Classes: Hook

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

logger

Constructor Details

#initialize(actor, arguments, response) ⇒ Action

Returns a new instance of Action.

Parameters:



44
45
46
47
48
# File 'lib/gamefic/action.rb', line 44

def initialize actor, arguments, response
  @actor = actor
  @arguments = arguments
  @response = response
end

Instance Attribute Details

#actorActive (readonly)

Returns:



33
34
35
# File 'lib/gamefic/action.rb', line 33

def actor
  @actor
end

#argumentsArray (readonly)

Returns:



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

def arguments
  @arguments
end

#responseResponse (readonly)

Returns:



39
40
41
# File 'lib/gamefic/action.rb', line 39

def response
  @response
end

Instance Method Details

#cancelObject

Cancel an action. This method can be called in an action hook to prevent subsequent hooks and/or the action itself from being executed.



71
72
73
# File 'lib/gamefic/action.rb', line 71

def cancel
  @cancelled = true
end

#cancelled?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/gamefic/action.rb', line 75

def cancelled?
  @cancelled ||= false
end

#executeself

Returns:

  • (self)


51
52
53
54
55
56
57
58
# File 'lib/gamefic/action.rb', line 51

def execute
  return self if cancelled? || executed?

  Gamefic.logger.info "Executing #{([verb] + [arguments]).flatten.map(&:inspect).join(', ')}"
  @executed = true
  response.execute actor, *arguments
  self
end

#executed?Boolean

True if the response has been executed. False typically means that the #execute method has not been called or the action was cancelled in a before_action hook.

Returns:

  • (Boolean)


64
65
66
# File 'lib/gamefic/action.rb', line 64

def executed?
  @executed ||= false
end

#meta?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/gamefic/action.rb', line 87

def meta?
  response.meta?
end

#narrativeObject



83
84
85
# File 'lib/gamefic/action.rb', line 83

def narrative
  response.narrative
end

#verbObject



79
80
81
# File 'lib/gamefic/action.rb', line 79

def verb
  response.verb
end