Class: Gamefic::Response
Overview
A proc to be executed in response to a command that matches its verb and queries.
Instance Attribute Summary collapse
Instance Method Summary
collapse
#available, #children, #descendants, #extended, #global, #integer, #myself, #parent, #plaintext, #siblings
Constructor Details
#initialize(verb, *queries, meta: false, &block) ⇒ Response
Returns a new instance of Response.
24
25
26
27
28
29
|
# File 'lib/gamefic/response.rb', line 24
def initialize verb, *queries, meta: false, &block
@verb = verb&.to_sym
@meta = meta
@block = block
@queries = map_queries(queries)
end
|
Instance Attribute Details
#block ⇒ Proc
19
20
21
|
# File 'lib/gamefic/response.rb', line 19
def block
@block
end
|
16
17
18
|
# File 'lib/gamefic/response.rb', line 16
def queries
@queries
end
|
#verb ⇒ Symbol
13
14
15
|
# File 'lib/gamefic/response.rb', line 13
def verb
@verb
end
|
Instance Method Details
#accept?(actor, command) ⇒ Boolean
True if the Response can be executed for the given actor and command.
48
49
50
51
52
|
# File 'lib/gamefic/response.rb', line 48
def accept?(actor, command)
command.verb == verb &&
command.arguments.length == queries.length &&
queries.zip(command.arguments).all? { |query, argument| query.accept?(actor, argument) }
end
|
#bind(narrative) ⇒ Object
76
77
78
|
# File 'lib/gamefic/response.rb', line 76
def bind(narrative)
clone.inject_binding narrative
end
|
#bound? ⇒ Boolean
72
73
74
|
# File 'lib/gamefic/response.rb', line 72
def bound?
!!gamefic_binding.narrative
end
|
#execute(*args) ⇒ Object
54
55
56
57
|
# File 'lib/gamefic/response.rb', line 54
def execute *args
Gamefic.logger.warn "Executing unbound response #{inspect}" unless bound?
gamefic_binding.call(*args)
end
|
#inspect ⇒ Object
68
69
70
|
# File 'lib/gamefic/response.rb', line 68
def inspect
"#<#{self.class} #{([verb] + queries).map(&:inspect).join(', ')}>"
end
|
The ‘meta?` flag is just a way for authors to identify responses that serve a purpose other than performing in-game actions. Out-of-game responses can include features like displaying help documentation or listing credits.
36
37
38
|
# File 'lib/gamefic/response.rb', line 36
def meta?
@meta
end
|
#precision ⇒ Integer
Note:
Precision is decreased if the response has a nil verb.
The total precision of all the response’s queries.
64
65
66
|
# File 'lib/gamefic/response.rb', line 64
def precision
@precision ||= calculate_precision
end
|
#syntax ⇒ Object
40
41
42
|
# File 'lib/gamefic/response.rb', line 40
def syntax
@syntax ||= generate_default_syntax
end
|