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
-
#narrative ⇒ Object
Returns the value of attribute narrative.
Instance Method Summary collapse
-
#accept?(subject, object) ⇒ Boolean
True if the object is selectable by the subject.
- #ambiguous? ⇒ Boolean
-
#initialize(*arguments, ambiguous: false, name: self.class.to_s) ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ Object
- #name ⇒ Object
- #precision ⇒ Integer
-
#query(subject, token) ⇒ Result
(also: #filter)
Get a query result for a given subject and token.
-
#select(subject) ⇒ Array<Entity>
Get an array of entities that match the arguments from the context of the subject.
-
#span(_subject) ⇒ Array<Entity>
Get an array of entities that are candidates for selection from the context of the subject.
Constructor Details
#initialize(*arguments, ambiguous: false, name: self.class.to_s) ⇒ Base
Returns a new instance of Base.
23 24 25 26 27 28 29 |
# File 'lib/gamefic/query/base.rb', line 23 def initialize *arguments, ambiguous: false, name: self.class.to_s raise ArgumentError, "nil argument in query" if arguments.any?(&:nil?) @arguments = arguments @ambiguous = ambiguous @name = name 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 |
#narrative ⇒ Object
Returns the value of attribute narrative.
16 17 18 |
# File 'lib/gamefic/query/base.rb', line 16 def narrative @narrative end |
Instance Method Details
#accept?(subject, object) ⇒ Boolean
True if the object is selectable by the subject.
80 81 82 83 84 85 86 87 |
# File 'lib/gamefic/query/base.rb', line 80 def accept?(subject, object) available = select(subject) if ambiguous? object & available == object else available.include?(object) end end |
#ambiguous? ⇒ Boolean
94 95 96 |
# File 'lib/gamefic/query/base.rb', line 94 def ambiguous? @ambiguous end |
#inspect ⇒ Object
102 103 104 |
# File 'lib/gamefic/query/base.rb', line 102 def inspect "##{ambiguous? ? '*' : ''}#{name}(#{normalized_arguments.map(&:inspect).join(', ')})" end |
#name ⇒ Object
98 99 100 |
# File 'lib/gamefic/query/base.rb', line 98 def name @name || self.class.to_s end |
#precision ⇒ Integer
90 91 92 |
# File 'lib/gamefic/query/base.rb', line 90 def precision @precision ||= calculate_precision end |
#query(subject, token) ⇒ Result Also known as: filter
Get a query result for a given subject and token.
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/gamefic/query/base.rb', line 42 def query(subject, token) first_pass = Scanner.scan(span(subject), token) if ambiguous? ambiguous_result(first_pass.filter(*normalized_arguments)) elsif first_pass.match.one? unambiguous_result(first_pass.filter(*normalized_arguments)) else unambiguous_result(first_pass) end end |
#select(subject) ⇒ Array<Entity>
Get an array of entities that match the arguments from the context of the subject.
59 60 61 |
# File 'lib/gamefic/query/base.rb', line 59 def select subject span(subject).that_are(*normalized_arguments) end |
#span(_subject) ⇒ Array<Entity>
Get an array of entities that are candidates for selection from the context of the subject. These are the entities that #select will filter through query’s arguments.
Subclasses should override this method.
71 72 73 |
# File 'lib/gamefic/query/base.rb', line 71 def span _subject [] end |