bloodhound 0.2 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +2 -97
- data/bloodhound.gemspec +2 -2
- data/lib/bloodhound.rb +3 -3
- metadata +2 -2
data/README.markdown
CHANGED
@@ -1,104 +1,9 @@
|
|
1
1
|
Bloodhound
|
2
2
|
==========
|
3
3
|
|
4
|
-
Simple
|
4
|
+
Simple scoping of ActiveRecord models based on "key:value" strings.
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
hound = Bloodhound.new
|
9
|
-
hound.add_search_field(:name, :string)
|
10
|
-
hound.add_search_field(:age, :integer)
|
11
|
-
hound.add_search_field(:active, :boolean)
|
12
|
-
|
13
|
-
attributes = hound.attributes_from('name:"John Doe" age:22 active:yes')
|
14
|
-
attributes #=> { "name" => "John Doe", "age" => 22, "active" => true }
|
15
|
-
|
16
|
-
The available types are `:string` (the default), `:integer`, `:float`, `:date`,
|
17
|
-
`:time`, and `:boolean`. Any other type is treated as a string.
|
18
|
-
|
19
|
-
It matches several values as boolean values. 'yes', 'y', and 'true' are all
|
20
|
-
mapped to `true`, while 'no', 'n', and 'false' are mapped to `false`.
|
21
|
-
|
22
|
-
You can customize the hash key returned:
|
23
|
-
|
24
|
-
hound.add_search_field(:user, :string, "users.name")
|
25
|
-
|
26
|
-
attributes = hound.attributes_from('user:"John Doe"')
|
27
|
-
attributes #=> { "users.name" => "John Doe" }
|
28
|
-
|
29
|
-
It can match dates and times, using [Chronic](http://github.com/mojombo/chronic),
|
30
|
-
so this is valid:
|
31
|
-
|
32
|
-
hound.add_search_field(:added, :date, "added_on")
|
33
|
-
|
34
|
-
attributes = hound.attributes_from("added:today")
|
35
|
-
attributes #=> { "added_on" => Date.today }
|
36
|
-
|
37
|
-
Finally, you can also provide processing rules for the parsed value, by passing
|
38
|
-
a block:
|
39
|
-
|
40
|
-
hound.add_search_field(:inactive, :boolean, "active") {|value| not value }
|
41
|
-
|
42
|
-
attributes = hound.attributes_from("inactive:true")
|
43
|
-
attributes #=> { "active" => false }
|
44
|
-
|
45
|
-
ActiveRecord integration
|
46
|
-
------------------------
|
47
|
-
|
48
|
-
require "bloodhound/active_record"
|
49
|
-
|
50
|
-
class Video < ActiveRecord::Base
|
51
|
-
extend Bloodhound::Searchable
|
52
|
-
named_scope :search, lambda {|query| bloodhound.attributes_from(query) }
|
53
|
-
end
|
54
|
-
|
55
|
-
The ActiveRecord implementation will automatically define search fields for all
|
56
|
-
non-id, non-timestamp columns (ie, all except for `id` and `foo_id`).
|
57
|
-
|
58
|
-
You can, of course, add those in manually if you need them.
|
59
|
-
|
60
|
-
The syntax for defining attributes is a bit more rails-esque:
|
61
|
-
|
62
|
-
class Video < ActiveRecord::Base
|
63
|
-
extend Bloodhound::Searchable
|
64
|
-
search_field :user, :type => :string, :attribute => "users.login"
|
65
|
-
end
|
66
|
-
|
67
|
-
The return value of `ActiveRecord::Bloodhound#attributes_from` changes a bit,
|
68
|
-
and returns a hash directly compatible with ActiveRecord:
|
69
|
-
|
70
|
-
attributes = Video.bloodhound.attributes_from("user:foca")
|
71
|
-
attributes #=> { :conditions => { "users.login" => "foca" } }
|
72
|
-
|
73
|
-
Any extra options you pass to `search_field` are added into the finder options:
|
74
|
-
|
75
|
-
class Video < ActiveRecord::Base
|
76
|
-
extend Bloodhound::Searchable
|
77
|
-
search_field :user, :attribute => "users.login", :joins => :user
|
78
|
-
|
79
|
-
belongs_to :user
|
80
|
-
end
|
81
|
-
|
82
|
-
attributes = Video.bloodhound.attributes_from("user:foca")
|
83
|
-
attributes #=> { :joins => :user,
|
84
|
-
:conditions => { "users.login" => "foca" } }
|
85
|
-
|
86
|
-
Install it
|
87
|
-
----------
|
88
|
-
|
89
|
-
gem install bloodhound
|
90
|
-
|
91
|
-
For the active record interface:
|
92
|
-
|
93
|
-
config.gem "bloodhound", :lib => "bloodhound/active_record"
|
94
|
-
|
95
|
-
Known problems
|
96
|
-
--------------
|
97
|
-
|
98
|
-
* Chronic is a bit… weird matching some stuff, specially regarding time zones.
|
99
|
-
* The ActiveRecord 'extra options' won't merge. So if you define two search
|
100
|
-
fields with ':joins => :an_association', only the latter will remain. This
|
101
|
-
will be fixed in a future release.
|
6
|
+
More documentation later.
|
102
7
|
|
103
8
|
License
|
104
9
|
-------
|
data/bloodhound.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "bloodhound"
|
3
|
-
s.version = "0.2"
|
4
|
-
s.date = "2010-02-
|
3
|
+
s.version = "0.2.1"
|
4
|
+
s.date = "2010-02-04"
|
5
5
|
|
6
6
|
s.description = "Convert strings like 'user:foca age:23' to { 'user' => 'foca' => 'age' => 23 }"
|
7
7
|
s.summary = "Convert strings like 'user:foca age:23' to { 'user' => 'foca' => 'age' => 23 }"
|
data/lib/bloodhound.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require "chronic"
|
2
2
|
|
3
3
|
class Bloodhound
|
4
|
-
VERSION = "0.2"
|
4
|
+
VERSION = "0.2.1"
|
5
5
|
ATTRIBUTE_RE = /\s*(?:(?:(\S+):)?(?:"([^"]*)"|'([^']*)'|(\S+)))\s*/.freeze
|
6
6
|
|
7
7
|
def initialize
|
@@ -74,7 +74,7 @@ class Bloodhound
|
|
74
74
|
private :attribute_search_for
|
75
75
|
|
76
76
|
def keyword_search_for(model, keyword, value)
|
77
|
-
model.scoped(@keywords[keyword].call(value))
|
77
|
+
model.scoped(@keywords[keyword.to_sym].call(value))
|
78
78
|
end
|
79
79
|
private :keyword_search_for
|
80
80
|
|
@@ -124,7 +124,7 @@ class Bloodhound
|
|
124
124
|
private :tokenize
|
125
125
|
|
126
126
|
def has_keyword?(name)
|
127
|
-
@keywords.has_key?(name)
|
127
|
+
@keywords.has_key?(name.to_sym)
|
128
128
|
end
|
129
129
|
|
130
130
|
module Searchable
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bloodhound
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Nicol\xC3\xA1s Sanguinetti"
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-02-
|
12
|
+
date: 2010-02-04 00:00:00 -02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|