Hi, I'm Bruce Williams.

KeywordSearch 1.3.1 (2007-10-09)

KeywordSearch is a small library I wrote earlier this year to add support for GMail-style keyword/value searches via a DSL. It’s a parser pet project that I tweak from time-to-time.

Here’s an example, albeit a little hackish:

<notextile>  
# Some variables to build up
  clauses = []
  arguments = []

  # Search a string, defining the supported keywords and building up
  # the variables in the associated closures

  KeywordSearch.search('account has:attachment since:2006-12-03') do |with|

    with.default_keyword :title # values without keywords get assigned to this

    with.keyword :title do |values| # keyword blocks are yielded an array of values
      clauses << "title like ?"
      arguments << "%#{values.join(' ')}%"
    end

    with.keyword :has do |values|
      clauses << 'has_attachment = true' if values.include?('attachment')
    end

    with.keyword :since do |values|
      date = Date.parse(values.first) # only support one
      clauses << 'created_on >= ?'
      arguments << date.to_s
    end

  end

  # Do our search with <tt>clauses</tt> and <tt>arguments</tt>
  conditions = [clauses.map{|c| "(#{c})"}.join(' AND ')), *arguments] # simplistic example
  results = Message.find(:all, :conditions => conditions)
</notextile>

KeywordSearch is available from RubyForge and can be installed via:

sudo gem install keyword_search

Comments, as always, are welcome.

Changes in this release: