Module: Gamefic::Scriptable::Responses

Includes:
Queries, Syntaxes
Included in:
Gamefic::Scriptable
Defined in:
lib/gamefic/scriptable/responses.rb

Instance Method Summary collapse

Methods included from Syntaxes

#interpret, #syntaxes

Methods included from Queries

#available, #children, #descendants, #extended, #global, #integer, #myself, #parent, #plaintext, #siblings

Instance Method Details

#meta(verb, *args) {|| ... } ⇒ Response

Create a meta response to a command.

Parameters:

  • verb (Symbol, String, nil)

    An imperative verb for the command

  • args (Array<Object>)

    Filters for the command’s tokens

Yield Parameters:

Returns:



46
47
48
49
50
51
# File 'lib/gamefic/scriptable/responses.rb', line 46

def meta verb, *args, &proc
  response = Response.new(verb&.to_sym, *args, meta: true, &proc)
  responses.push response
  syntaxes.push response.syntax
  response
end

#respond(verb, *args) {|| ... } ⇒ Response

Create a response to a command. A Response uses the verb argument to identify the imperative verb that triggers the action. It can also accept queries to tokenize the remainder of the input and filter for particular entities or properties. The block` argument is the proc to execute when the input matches all of the Response’s criteria (i.e., verb and queries).

Examples:

A simple Response.

respond :wave do |actor|
  actor.tell "Hello!"
end
# The command "wave" will respond "Hello!"

A Response that accepts a Character

respond :salute, available(Character) do |actor, character|
  actor.tell "#{The character} returns your salute."
end

Parameters:

  • verb (Symbol, String, nil)

    An imperative verb for the command

  • args (Array<Object>)

    Filters for the command’s tokens

Yield Parameters:

Returns:



32
33
34
35
36
37
# File 'lib/gamefic/scriptable/responses.rb', line 32

def respond verb, *args, &proc
  response = Response.new(verb&.to_sym, *args, &proc)
  responses.push response
  syntaxes.push response.syntax
  response
end

#responsesArray<Response>

Returns:



54
55
56
# File 'lib/gamefic/scriptable/responses.rb', line 54

def responses
  @responses ||= []
end

#responses_for(*verbs) ⇒ Array<Response>

Returns:



59
60
61
62
# File 'lib/gamefic/scriptable/responses.rb', line 59

def responses_for(*verbs)
  symbols = verbs.map { |verb| verb&.to_sym }
  responses.select { |response| symbols.include? response.verb }
end

#verbsArray<Symbol>

Returns:



65
66
67
# File 'lib/gamefic/scriptable/responses.rb', line 65

def verbs
  responses.select(&:verb).uniq(&:verb)
end