Sha256: af4b99dd4eb4d965df6b0eb8c0280fd1c36280c3cd409b1aee95b386167c56a1

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module Gamefic
  module Query
    # A special query that handles text instead of entities.
    #
    class Text
      # @param argument [String, Regexp]
      def initialize argument = /.*/
        @argument = argument
        validate
      end

      # @return [String, Regexp]
      def select(_subject)
        @argument
      end

      def query _subject, token
        if match? token
          Result.new(token, '')
        else
          Result.new(nil, token)
        end
      end

      def precision
        0
      end

      def accept? _subject, argument
        match? argument
      end

      def ambiguous?
        true
      end

      private

      def match? token
        case @argument
        when Regexp
          token =~ @argument
        else
          token == @argument
        end
      end

      def validate
        return if @argument.is_a?(String) || @argument.is_a?(Regexp)

        raise ArgumentError, 'Invalid text query argument'
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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