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

Methods inherited from Base

#accept?

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)


41
42
43
# File 'lib/gamefic/query/scoped.rb', line 41

def ambiguous?
  @ambiguous
end

#precisionObject



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

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

#query(subject, token) ⇒ Result

Returns:



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gamefic/query/scoped.rb', line 25

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

#select(subject) ⇒ Object



19
20
21
22
# File 'lib/gamefic/query/scoped.rb', line 19

def select(subject)
  @scope.matches(subject)
        .that_are(*@arguments)
end