Sha256: 1326488d8535bba90c6d4c5a2d9b3a85884d15ed7c21a044bcf573edb53ce743

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module Gamefic
  module Query
    # 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.
    #
    class General < Base
      # @param entities [Array, Proc]
      # @param arguments [Array<Object>]
      # @param ambiguous [Boolean]
      def initialize entities, *arguments, ambiguous: false
        super(*arguments, ambiguous: ambiguous)
        @entities = entities
      end

      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

      private

      def available_entities(subject)
        if @entities.is_a?(Proc)
          if @entities.arity.zero?
            @entities.call
          else
            @entities.call(subject)
          end
        else
          @entities
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gamefic-3.0.0 lib/gamefic/query/general.rb