Class: Gamefic::Response
- Inherits:
-
Object
- Object
- Gamefic::Response
- Defined in:
- lib/gamefic/response.rb
Overview
A proc to be executed in response to a command that matches its verb and queries.
Instance Attribute Summary collapse
- #queries ⇒ Array<Query::Base> readonly
- #verb ⇒ Symbol readonly
Instance Method Summary collapse
-
#attempt(actor, command) ⇒ Action?
Return an Action if the Response can accept the actor’s command.
- #execute(*args) ⇒ Object
- #hidden? ⇒ Boolean
-
#initialize(verb, stage, *queries, meta: false, &block) ⇒ Response
constructor
A new instance of Response.
-
#meta? ⇒ Boolean
The ‘meta?` flag is just a way for authors to identify responses that serve a purpose other than performing in-game actions.
- #precision ⇒ Object
- #syntax ⇒ Object
Constructor Details
#initialize(verb, stage, *queries, meta: false, &block) ⇒ Response
Returns a new instance of Response.
22 23 24 25 26 27 28 |
# File 'lib/gamefic/response.rb', line 22 def initialize verb, stage, *queries, meta: false, &block @verb = verb @stage = stage @queries = map_queryable_objects(queries) @meta = @block = block end |
Instance Attribute Details
#queries ⇒ Array<Query::Base> (readonly)
12 13 14 |
# File 'lib/gamefic/response.rb', line 12 def queries @queries end |
#verb ⇒ Symbol (readonly)
9 10 11 |
# File 'lib/gamefic/response.rb', line 9 def verb @verb end |
Instance Method Details
#attempt(actor, command) ⇒ Action?
Return an Action if the Response can accept the actor’s command.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/gamefic/response.rb', line 53 def attempt actor, command return nil if command.verb != verb tokens = command.arguments.clone result = [] remainder = '' queries.each do |qd| token = tokens.shift txt = "#{remainder} #{token}".strip return nil if txt.empty? response = qd.query(actor, txt) return nil if response.match.nil? result.push response.match remainder = response.remainder end return nil unless tokens.empty? && remainder.empty? Action.new(actor, result, self) end |
#execute(*args) ⇒ Object
78 79 80 |
# File 'lib/gamefic/response.rb', line 78 def execute *args Stage.run(@stage, *args, &@block) end |
#hidden? ⇒ Boolean
39 40 41 |
# File 'lib/gamefic/response.rb', line 39 def hidden? @hidden ||= verb.to_s.start_with?('_') end |
#meta? ⇒ Boolean
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.
35 36 37 |
# File 'lib/gamefic/response.rb', line 35 def @meta end |
#precision ⇒ Object
82 83 84 |
# File 'lib/gamefic/response.rb', line 82 def precision @precision ||= calculate_precision end |
#syntax ⇒ Object
43 44 45 |
# File 'lib/gamefic/response.rb', line 43 def syntax @syntax ||= generate_default_syntax end |