Sha256: 78de9f7825e4f85f691e44f3d4a707a14c102ab04005b38f9a052709ac32fefe

Contents?: true

Size: 934 Bytes

Versions: 1

Compression:

Stored size: 934 Bytes

Contents

# frozen_string_literal: true

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

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

      def precision
        0
      end

      private

      def match? token
        return true if @argument.nil?

        case @argument
        when Regexp
          token =~ @argument
        else
          token == @argument
        end
      end

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

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