Class: Gamefic::Query::Base

Inherits:
Object
  • Object
show all
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.

Direct Known Subclasses

General, Scoped

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*arguments, ambiguous: false) ⇒ Base

Returns a new instance of Base.

Parameters:

  • arguments (Array<Object>)
  • ambiguous (Boolean) (defaults to: false)

Raises:

  • (ArgumentError)

    if any of the arguments are nil



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

#ambiguousBoolean (readonly)

Returns:

  • (Boolean)


14
15
16
# File 'lib/gamefic/query/base.rb', line 14

def ambiguous
  @ambiguous
end

#argumentsArray<Object> (readonly)

Returns:



11
12
13
# File 'lib/gamefic/query/base.rb', line 11

def arguments
  @arguments
end

Instance Method Details

#accept?(subject, object) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


63
64
65
# File 'lib/gamefic/query/base.rb', line 63

def ambiguous?
  @ambiguous
end

#precisionInteger

Returns:

  • (Integer)


59
60
61
# File 'lib/gamefic/query/base.rb', line 59

def precision
  @precision ||= calculate_precision
end

#query(subject, token) ⇒ Result

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.

Parameters:

Returns:



36
37
38
# File 'lib/gamefic/query/base.rb', line 36

def query(subject, token)
  raise "#query not implemented for #{self.class}"
end

#select(subject) ⇒ Array<Entity>

Get an array of entities that match the query from the context of the subject.

Parameters:

Returns:



45
46
47
# File 'lib/gamefic/query/base.rb', line 45

def select subject
  raise "#select not implemented for #{self.class}"
end