Sha256: f59cb9e1aa5d0fde72eb5d34ef08c781d061884265484ae168a99d33466da309

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 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.
    #
    # @return [Class<Gamefic::Scope::Base>]
    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

3 entries across 3 versions & 1 rubygems

Version Path
gamefic-3.2.1 lib/gamefic/query/scoped.rb
gamefic-3.2.0 lib/gamefic/query/scoped.rb
gamefic-3.1.0 lib/gamefic/query/scoped.rb