Sha256: 8d636152199d088b906676bb2c772007eb0f4f4843a0ee93cfee3278a86f1137

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

module ScopedSearch::QueryStringParser
  
  def to_search_query
    items = lex_for_query_string_parsing
    search_conditions = []
    
    negate = false
    items.each do |item|
      case item
      when :not
        negate = true
      else
        search_conditions << (negate ? [item, :not] : [item])
        negate = false
      end
    end
    return search_conditions
  end
  
  def lex_for_query_string_parsing
    terms = []
    current_term = ""
    quoted_string_openend = false
    
    self.each_char do |char|
      
      case char
      when /\s/
        if quoted_string_openend
          current_term << char
        elsif current_term.length > 0
          terms << current_term
          current_term = ""
        end
      
      when '-'
        if quoted_string_openend || current_term.length > 0
          current_term << char
        else
          terms << :not
        end
        
      when '"'
        if quoted_string_openend
          if current_term.length > 0
            if current_term[-1,1] == "\\"
              current_term[-1] = char
            else
              terms << current_term
              current_term = ""
              quoted_string_openend = false
            end
          else 
            quoted_string_openend = false
          end

        else
          quoted_string_openend = true
        end
        
      else
        current_term << char
      end
      
    end
    terms << current_term if current_term.length > 0
    return terms
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wvanbergen-scoped_search-0.1.2 lib/scoped_search/query_string_parser.rb