Sha256: 6741de0a1e821286dd3e427f3dd7209e878abbbd53f19f0608c58f8775efbc34

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

class Query
  class << self
    # Returns a Ferret::Query from a raw String query.
    def extract_from(raw_query)
      parser.parse(convert_to_english(raw_query))
    end
    
    private
    
    # Converts query keywords to english so they can be parsed by Ferret.    
    def convert_to_english(raw_query)
      to_en={
       /\b#{:AND.l}\b/=>'AND',
       /\b#{:OR.l}\b/=>'OR',
       /\b#{:NOT.l}\b/=>'NOT',
       /(#{:filename.l}):/=>'filename:',
       /(#{:filetype.l}):/=>'filetype:',
       /#{:content.l}:/ => 'content:',
       /(#{:modified.l}):/ => 'modified:',
       /(#{:language.l}):/ => 'language:',
       /\b#{:LIKE.l}\s+(\S+)/=>'\1~'
      }
      to_en.inject(raw_query){|mem,non_english_to_english_keyword|
        mem.gsub(*non_english_to_english_keyword)
      }      
    end
    
    # Instantiates a QueryParser once, and keeps it in cache.
    def parser
      @@parser ||= Ferret::QueryParser.new(:fields => [:content, :filename, :basename, :filetype, :modified], :or_default => false, :analyzer=>Picolena::Analyzer)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
picolena-0.1.5 lib/picolena/templates/app/models/query.rb