Sha256: 67c4a9863c44a04d2cd235de19da82a3bc07d6e6a0a78901446e8c389062a7fb

Contents?: true

Size: 1.29 KB

Versions: 9

Compression:

Stored size: 1.29 KB

Contents

module Gamefic
  module Query
    class Text < Base
      def initialize *arguments
        arguments.each do |a|
          if (a.kind_of?(Symbol) || a.kind_of?(String)) && !a.to_s.end_with?('?')
            raise ArgumentError.new("Text query arguments can only be boolean method names (:method?) or regular expressions")
          end
        end
        super
      end

      def resolve _subject, token, continued: false
        return Matches.new([], '', token) unless accept?(token)
        parts = token.split(Keywords::SPLIT_REGEXP)
        cursor = []
        matches = []
        i = 0
        parts.each { |w|
          cursor.push w
          matches = cursor if accept?(cursor.join(' '))
          i += 1
        }
        if continued
          Matches.new([matches.join(' ')], matches.join(' '), parts[i..-1].join(' '))
        elsif matches.length == parts.length
          Matches.new([matches.join(' ')], matches.join(' '), '')
        else
          Matches.new([], '', parts.join(' '))
        end
      end

      def include? _subject, token
        accept?(token)
      end

      def accept? entity
        return false unless entity.kind_of?(String) and !entity.empty?
        super
      end

      def precision
        0
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
gamefic-2.4.0 lib/gamefic/query/text.rb
gamefic-2.3.0 lib/gamefic/query/text.rb
gamefic-2.2.3 lib/gamefic/query/text.rb
gamefic-2.2.2 lib/gamefic/query/text.rb
gamefic-2.2.1 lib/gamefic/query/text.rb
gamefic-2.2.0 lib/gamefic/query/text.rb
gamefic-2.1.1 lib/gamefic/query/text.rb
gamefic-2.1.0 lib/gamefic/query/text.rb
gamefic-2.0.3 lib/gamefic/query/text.rb