dm-is-searchable 0.9.9 → 0.9.10
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/README.txt +119 -0
- data/lib/dm-is-searchable.rb +4 -6
- data/lib/dm-is-searchable/is/version.rb +1 -1
- data/tasks/spec.rb +1 -1
- metadata +3 -3
data/History.txt
CHANGED
data/README.txt
CHANGED
@@ -1 +1,120 @@
|
|
1
1
|
= dm-is-searchable
|
2
|
+
|
3
|
+
* http://datamapper.org
|
4
|
+
* git://github.com/sam/dm-more.git
|
5
|
+
|
6
|
+
== Description
|
7
|
+
|
8
|
+
A DataMapper search plugin api to search for resources from one repository and
|
9
|
+
load from another.
|
10
|
+
|
11
|
+
Typically a full text search adapter that can only produce partial resources is
|
12
|
+
searched and the resulting resource collection is then loaded from your default
|
13
|
+
repository.
|
14
|
+
|
15
|
+
== Synopsis
|
16
|
+
|
17
|
+
=== Resources
|
18
|
+
|
19
|
+
require "rubygems"
|
20
|
+
require "dm-core"
|
21
|
+
require "dm-is-searchable"
|
22
|
+
|
23
|
+
DataMapper.setup(:default, :adapter => 'in_memory')
|
24
|
+
|
25
|
+
# Your search adapter configuration. See your search adapters setup options.
|
26
|
+
DataMapper.setup(:search, :adapter => 'your_adapter')
|
27
|
+
|
28
|
+
class Cow
|
29
|
+
include DataMapper::Resource
|
30
|
+
property :name, String, :key => true
|
31
|
+
property :likes, String
|
32
|
+
property :cowpats, Integer
|
33
|
+
|
34
|
+
is :searchabe # This defaults to repository(:search), you could also do
|
35
|
+
# is :searchable, :repository => :some_searchable_repository
|
36
|
+
end
|
37
|
+
|
38
|
+
class Chicken
|
39
|
+
include DataMapper::Resource
|
40
|
+
property :name, String, :key => true
|
41
|
+
property :likes, String
|
42
|
+
property :eggs, Integer
|
43
|
+
|
44
|
+
is :searchable
|
45
|
+
repository(:search) do
|
46
|
+
# We only want to be able to search by name.
|
47
|
+
properties(:search).clear
|
48
|
+
property :name, String
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
=== Searching
|
53
|
+
|
54
|
+
Mixing in is-searchable by default defines a single class method in your
|
55
|
+
resource with the signature of:
|
56
|
+
|
57
|
+
# ==== Parameters
|
58
|
+
# search_options<Hash>::
|
59
|
+
# DM::Query conditions to pass to the searchable repository. Unless you
|
60
|
+
# explicitly defined a searchable repository this is repository(:search).
|
61
|
+
#
|
62
|
+
# options<Hash>::
|
63
|
+
# Optionsal DM::Query conditions to pass to the repository holding the full
|
64
|
+
# resource. Without a scoped search this is repository(:default).
|
65
|
+
#
|
66
|
+
# ==== Returns
|
67
|
+
# DM::Collection:: Zero or more DM::Resource objects.
|
68
|
+
MyModel#search(search_options = {}, options = {})
|
69
|
+
|
70
|
+
A basic full text search for cows called 'Pete', 'Peter', 'Pedro' (dependant on
|
71
|
+
the search adapter) would look like:
|
72
|
+
|
73
|
+
puts Cow.search(:name => 'pete').inspect
|
74
|
+
#=> [<Cow name="peter" cowpats="12" ...>, <Cow name='pete' cowpats="1024" ...>, ...]
|
75
|
+
|
76
|
+
Adding extra conditions to apply to the default repository allows you to do
|
77
|
+
interesting things to the search adapter results. For example conditions on
|
78
|
+
properties not available in your search adapter or unsupported operators:
|
79
|
+
|
80
|
+
# Unsupported #gt operator in search adapter.
|
81
|
+
puts Cow.search({:name => 'pete'}, {:cowpats.gt => 1000}).inspect
|
82
|
+
#=> [<Cow name="pete" cowpats="1024" ...>]
|
83
|
+
|
84
|
+
# Unknown property in search adapter.
|
85
|
+
puts Chicken.search({:name => 'steve'}, {:eggs => (100..200)}).inspect
|
86
|
+
#=> [<Chicken name="steve" eggs="120" ...>]
|
87
|
+
|
88
|
+
=== Adapter
|
89
|
+
|
90
|
+
Like all DM adapters a custom search adapter implements the
|
91
|
+
DM::AbstractAdapter interface.
|
92
|
+
|
93
|
+
The key differences from a typical adapter are:
|
94
|
+
|
95
|
+
==== DM::AbstractAdapter#read_many
|
96
|
+
|
97
|
+
An Array of Hashes in the form of <tt>[{:id => 12}, {:id => 53}]</tt> will
|
98
|
+
work just fine. In fact so long as the following snippet would run on the
|
99
|
+
returned value you are free to return whatever you like.
|
100
|
+
|
101
|
+
ids = read_many_result.collect{|doc| doc[:id]} #=> Array of ID's.
|
102
|
+
|
103
|
+
==== DM::AbstractAdapter#read_one
|
104
|
+
|
105
|
+
No need to DM::Model#load here. Just return your partial resource as a Hash
|
106
|
+
in the form of <tt>{:id => 12}</tt>. Like <tt>#read_many</tt> the returned
|
107
|
+
value really only needs to respond to <tt>#[:id]</tt>.
|
108
|
+
|
109
|
+
read_one_result[:id] #=> The resource ID to load.
|
110
|
+
|
111
|
+
|
112
|
+
== Compatible Search Adapters
|
113
|
+
|
114
|
+
=== Ferret
|
115
|
+
Gem:: dm-ferret-adapter
|
116
|
+
Git:: git://github.com/sam/dm-more.git # dm-more/adapters/dm-ferret-adapter
|
117
|
+
|
118
|
+
=== Sphinx
|
119
|
+
Gem:: dm-sphinx-adapter
|
120
|
+
Git:: git://github.com/shanna/dm-sphinx-adapter.git
|
data/lib/dm-is-searchable.rb
CHANGED
@@ -3,7 +3,7 @@ require 'rubygems'
|
|
3
3
|
require 'pathname'
|
4
4
|
|
5
5
|
# Add all external dependencies for the plugin here
|
6
|
-
gem 'dm-core', '~>0.9.
|
6
|
+
gem 'dm-core', '~>0.9.10'
|
7
7
|
require 'dm-core'
|
8
8
|
|
9
9
|
# Require plugin-files
|
@@ -12,9 +12,7 @@ require Pathname(__FILE__).dirname.expand_path / 'dm-is-searchable' / 'is' / 'se
|
|
12
12
|
|
13
13
|
# Include the plugin in Resource
|
14
14
|
module DataMapper
|
15
|
-
module
|
16
|
-
|
17
|
-
|
18
|
-
end # module ClassMethods
|
19
|
-
end # module Resource
|
15
|
+
module Model
|
16
|
+
include DataMapper::Is::Searchable
|
17
|
+
end # module Model
|
20
18
|
end # module DataMapper
|
data/tasks/spec.rb
CHANGED
@@ -8,7 +8,7 @@ begin
|
|
8
8
|
desc 'Run specifications'
|
9
9
|
Spec::Rake::SpecTask.new(:spec) do |t|
|
10
10
|
t.spec_opts << '--options' << 'spec/spec.opts' if File.exists?('spec/spec.opts')
|
11
|
-
t.spec_files = Pathname.glob((ROOT + 'spec/**/*_spec.rb').to_s)
|
11
|
+
t.spec_files = Pathname.glob((ROOT + 'spec/**/*_spec.rb').to_s).map { |f| f.to_s }
|
12
12
|
|
13
13
|
begin
|
14
14
|
gem 'rcov', '~>0.8'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dm-is-searchable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bernerd Schaefer
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-01-
|
12
|
+
date: 2009-01-19 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -20,7 +20,7 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - ~>
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version: 0.9.
|
23
|
+
version: 0.9.10
|
24
24
|
version:
|
25
25
|
description: A DataMapper plugin for searching
|
26
26
|
email:
|