Sha256: f8c55bb385abf67f0eaaba120590dae3f5b9ceaf95aa4024be69e47c4f5e5dfe

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

# encoding: utf-8

module Dusen
  class Parser

    TEXT_QUERY = /(?:(\-)?"([^"]+)"|(\-)?([\S]+))/
    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 = $2
        value = "#{$4}#{$6}"
        exclude = "#{$1}" == "-"
        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.6.1 lib/dusen/parser.rb
dusen-0.6.0 lib/dusen/parser.rb
dusen-0.5.3 lib/dusen/parser.rb