Class: Gamefic::Query::General

Inherits:
Base
  • Object
show all
Defined in:
lib/gamefic/query/general.rb

Overview

A General query accepts an array of entities to filter. Unlike Scoped queries, the resulting entities will not necessarily be in the actor’s immediate vicinity.

General queries can also be passed a Proc that returns an array of entities. If the Proc accepts an argument, it will be given the subject of the query.

Instance Attribute Summary

Attributes inherited from Base

#ambiguous, #arguments

Instance Method Summary collapse

Methods inherited from Base

#ambiguous?, #precision

Constructor Details

#initialize(entities, *arguments, ambiguous: false) ⇒ General

Returns a new instance of General.

Parameters:

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


17
18
19
20
# File 'lib/gamefic/query/general.rb', line 17

def initialize entities, *arguments, ambiguous: false
  super(*arguments, ambiguous: ambiguous)
  @entities = entities
end

Instance Method Details

#query(subject, token) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/gamefic/query/general.rb', line 22

def query subject, token
  filtered = available_entities(subject).that_are(*@arguments)
  return Result.new(token, nil) if filtered.include?(token)

  scan = Scanner.scan(filtered, token)

  ambiguous? ? ambiguous_result(scan) : unambiguous_result(scan)
end