Class: Gamefic::Query::Scoped

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

Overview

A Scoped query uses a Scope to select entities to filter based on their relationship to the entity performing the query. For example, Scope::Children would filter from an array of the entity’s descendants.

Returns:

Instance Attribute Summary collapse

Attributes inherited from Base

#ambiguous, #arguments

Instance Method Summary collapse

Constructor Details

#initialize(scope, *arguments, ambiguous: false) ⇒ Scoped

Returns a new instance of Scoped.

Parameters:



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

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

Instance Attribute Details

#scopeObject (readonly)

Returns the value of attribute scope.



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

def scope
  @scope
end

Instance Method Details

#ambiguous?Boolean

Returns:

  • (Boolean)


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

def ambiguous?
  @ambiguous
end

#precisionObject



32
33
34
# File 'lib/gamefic/query/scoped.rb', line 32

def precision
  @precision ||= @scope.precision + calculate_precision
end

#query(subject, token) ⇒ Result

Returns:



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gamefic/query/scoped.rb', line 20

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

  scan = Scanner.scan(available, token)

  return ambiguous_result(scan) if ambiguous?

  unambiguous_result(scan)
end