Sha256: 67bb8a7f984254db8ad6ef35abcc9ade8e5d186459733d85a752aba8a9cf4093

Contents?: true

Size: 1.14 KB

Versions: 3

Compression:

Stored size: 1.14 KB

Contents

# encoding: utf-8

module Dusen
  class Parser

    WESTERNISH_WORD_CHARACTER =  '\\w\\-\\.;@_ÄÖÜäöüß' # this is wrong on so many levels
    TEXT_QUERY = /(?:(\-)?"([^"]+)"|(\-)?([#{WESTERNISH_WORD_CHARACTER}]+))/
    FIELD_QUERY = /(\w+)\:#{TEXT_QUERY}/

    def self.parse(query_string)
      query_string = query_string.dup # we are going to delete substrings in-place
      query = Query.new
      extract_field_query_tokens(query_string, query)
      extract_text_query_tokens(query_string, query)
      query
    end

    def self.extract_text_query_tokens(query_string, query)
      while query_string.sub!(TEXT_QUERY, '')
        value = "#{$2}#{$4}"
        exclude = "#{$1}#{$3}" == "-"
        options = { :field => 'text', :value => value, :exclude => exclude }
        query << Token.new(options)
      end
    end

    def self.extract_field_query_tokens(query_string, query)
      while query_string.sub!(FIELD_QUERY, '')
        field = $1
        value = "#{$3}#{$5}"
        exclude = "#{$2}#{$4}" == "-"
        options = { :field => field, :value => value, :exclude => exclude }
        query << Token.new(options)
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dusen-0.5.1 lib/dusen/parser.rb
dusen-0.5 lib/dusen/parser.rb
dusen-0.4.11 lib/dusen/parser.rb