Sha256: 9e6f84c9a4db9a6d844ee1ac707161091a872dec85df761c8a2a7cf9783f46d3

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module Gamefic
  module Query
    # 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.
    #
    class Scoped < Base
      attr_reader :scope

      # @param scope [Class<Gamefic::Scope::Base>]
      def initialize scope, *arguments, ambiguous: false
        super(*arguments, ambiguous: ambiguous)
        @scope = scope
      end

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

      # @return [Result]
      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

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

      def ambiguous?
        @ambiguous
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gamefic-3.4.0 lib/gamefic/query/scoped.rb
gamefic-3.3.0 lib/gamefic/query/scoped.rb