Class: Gamefic::Query::Base
- Inherits:
-
Object
- Object
- Gamefic::Query::Base
- Defined in:
- lib/gamefic/query/base.rb
Overview
A base class for entity-based queries that can be applied to responses. Each query represents an attempt to match an argument in a command to a game entity.
Instance Attribute Summary collapse
- #ambiguous ⇒ Boolean readonly
- #arguments ⇒ Array<Object> readonly
Instance Method Summary collapse
- #accept?(subject, object) ⇒ Boolean
- #ambiguous? ⇒ Boolean
-
#initialize(*arguments, ambiguous: false) ⇒ Base
constructor
A new instance of Base.
- #precision ⇒ Integer
-
#query(subject, token) ⇒ Result
deprecated
Deprecated.
Queries should only be used to select entities that are eligible to be response arguments. After a text command is tokenized into an array of expressions, the composer builds the command that the dispatcher uses to execute actions. The #accept? method verifies that the command’s arguments match the response’s queries.
-
#select(subject) ⇒ Array<Entity>
Get an array of entities that match the query from the context of the subject.
Constructor Details
#initialize(*arguments, ambiguous: false) ⇒ Base
Returns a new instance of Base.
20 21 22 23 24 25 |
# File 'lib/gamefic/query/base.rb', line 20 def initialize *arguments, ambiguous: false raise ArgumentError, "nil argument in query" if arguments.any?(&:nil?) @arguments = arguments @ambiguous = ambiguous end |
Instance Attribute Details
#ambiguous ⇒ Boolean (readonly)
14 15 16 |
# File 'lib/gamefic/query/base.rb', line 14 def ambiguous @ambiguous end |
#arguments ⇒ Array<Object> (readonly)
11 12 13 |
# File 'lib/gamefic/query/base.rb', line 11 def arguments @arguments end |
Instance Method Details
#accept?(subject, object) ⇒ Boolean
49 50 51 52 53 54 55 56 |
# File 'lib/gamefic/query/base.rb', line 49 def accept?(subject, object) available = select(subject) if ambiguous? object & available == object else available.include?(object) end end |
#ambiguous? ⇒ Boolean
63 64 65 |
# File 'lib/gamefic/query/base.rb', line 63 def ambiguous? @ambiguous end |
#precision ⇒ Integer
59 60 61 |
# File 'lib/gamefic/query/base.rb', line 59 def precision @precision ||= calculate_precision end |
#query(subject, token) ⇒ Result
Queries should only be used to select entities that are eligible to be response arguments. After a text command is tokenized into an array of expressions, the composer builds the command that the dispatcher uses to execute actions. The #accept? method verifies that the command’s arguments match the response’s queries.
36 37 38 |
# File 'lib/gamefic/query/base.rb', line 36 def query(subject, token) raise "#query not implemented for #{self.class}" end |