We got nominated! Help us out and vote for GitHub as Best Bootstrapped Startup of 2008. (You can vote once a day.) [ hide ]

public
Description: Generic support for extracting GMail-style search keywords/values from strings
Homepage: http://codefluency.rubyforge.org/keyword_search
Clone URL: git://github.com/bruce/keyword_search.git
wbruce (author)
Tue Oct 09 00:49:35 -0700 2007
commit  f0cad1153e1f62e7b031b34140f1249fd2e8a55d
tree    0d50188f6d4568908ba553bce81c0e01da5d06a6
parent  8c68fbc89ba31c5d2a5dabdeaf63c6c1ece19b50
keyword_search / README.txt
100644 93 lines (64 sloc) 2.969 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
keyword_search
    http://codefluency.rubyforge.org/keyword_search
    by Bruce Williams
 
== DESCRIPTION:
  
Generic library to parse GMail-style search strings for keyword/value pairs; supports definition of valid keywords and handling of quoted values.
 
== FEATURES/PROBLEMS:
  
The library features a very simple, easy-to-use API.
* Define handlers for supported keywords with blocks
* Define the default keyword (values not part of a keyword/value pair)
 
Various notes:
* Quoted values are supported.
* Input is automatically downcased (both keywords and values should be assumed to be in lowercase)
 
Development Roadmap:
2.0:: Add negation and grouping (will break API backwards compatibility)
 
== SYNOPSIS:
 
Here's an example of usage from Rails (though the library is generic, and could presumably be used for any Ruby project)
 
  # 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
  
    with.keyword :title do |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)
 
== REQUIREMENTS:
 
* hoe
 
== INSTALL:
 
sudo gem install keyword_search
 
== LICENSE:
 
(The MIT License)
 
Copyright (c) 2007 Bruce Williams
 
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
 
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
== LEGAL NOTES
 
GMail is copyright Google, Inc.