lunr 2.0.2 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,10 @@
1
+ === 2.0.3 / 2010-09-07
2
+
3
+ * Cleanups, squash warnings.
4
+ * Improve the README a bit.
5
+ * Improve search and scope examples.
6
+ * Remove unnecessary monkeypatch.
7
+
1
8
  === 2.0.2 / 2010-09-02
2
9
 
3
10
  * Fix Rakefile typo.
data/README.rdoc CHANGED
@@ -6,13 +6,11 @@
6
6
 
7
7
  A simple read-only interface to Solr, built on Sunspot.
8
8
 
9
- Lunr makes it easy to query and create objects from a Solr index
10
- without requiring all the knowledge, code, and data that was used to
11
- build the index in the first place.
12
-
13
- If you have complex indexes with a stored fields and need to search /
14
- access those fields without access to the original data store, Lunr
15
- might be what you're looking for.
9
+ Lunr makes it easy to query and create objects from a Sunspot-managed
10
+ Solr index without requiring all the knowledge, code, and data used to
11
+ build the index in the first place. If you have complex indexes with a
12
+ stored fields and need to search / access those fields without access
13
+ to the original data store, Lunr might be what you're looking for.
16
14
 
17
15
  == A Lunr Model
18
16
 
@@ -31,11 +29,11 @@ might be what you're looking for.
31
29
  string :state
32
30
  end
33
31
 
34
- scope do |q|
35
- q.order_by :hot, :desc
36
- q.order_by :accepted_at, :desc
32
+ scope do
33
+ order_by :hot, :desc
34
+ order_by :accepted_at, :desc
37
35
 
38
- q.with :state, "accepted"
36
+ with :state, "accepted"
39
37
  end
40
38
 
41
39
  scope :hot do
@@ -83,11 +81,11 @@ Sunspot's DSL that involves indexing.
83
81
  Call <code>scope</code> with no name to set default filtering,
84
82
  ordering, and pagination properties.
85
83
 
86
- scope do |q|
87
- q.order_by :hot, :desc
88
- q.order_by :accepted_at, :desc
84
+ scope do
85
+ order_by :hot, :desc
86
+ order_by :accepted_at, :desc
89
87
 
90
- q.with :state, "accepted"
88
+ with :state, "accepted"
91
89
  end
92
90
 
93
91
  === Named Scopes
@@ -111,9 +109,14 @@ first param must be the Sunspot query object.
111
109
 
112
110
  == Searching
113
111
 
112
+ Use Sunspot's excellent search DSL for ad hock searches.
113
+
114
114
  My::SimpleTrack.search
115
115
  My::SimpleTrack.all # same thing
116
116
 
117
+ My::SimpleTrack.search { with :title, "Hello" }
118
+ My::SimpleTrack.first { with :title, "First!" }
119
+
117
120
  All searches return a <code>Lunr::Search</code> object, and searches
118
121
  can be narrowed using the Sunspot query DSL or named scopes. Searches
119
122
  aren't run until an <code>Enumerable</code>, arraylike, or pagination
@@ -134,8 +137,8 @@ method is called on the search object. For convenience,
134
137
 
135
138
  == Development
136
139
 
137
- Version number notwithstanding, Lunr is under pretty heavy
138
- development. It's missing a lot of tests, and will be in flux a
140
+ Lunr is under pretty heavy development. It was extracted from a Real
141
+ Application, and barely has any tests of its own. It will be in flux a
139
142
  bit. Contributions and suggestions are very welcome.
140
143
 
141
144
  Install the <code>isolate</code> gem and run <code>rake</code>. All
data/lib/lunr.rb CHANGED
@@ -6,7 +6,7 @@ module Lunr
6
6
 
7
7
  # Duh.
8
8
 
9
- VERSION = "2.0.2"
9
+ VERSION = "2.0.3"
10
10
 
11
11
  end
12
12
 
data/lib/lunr/model.rb CHANGED
@@ -2,7 +2,7 @@ require "lunr/model/klass"
2
2
 
3
3
  module Lunr
4
4
  module Model
5
- attr_reader :id
5
+ attr_accessor :id
6
6
 
7
7
  def to_h
8
8
  @to_h ||= {}.tap do |h|
@@ -8,6 +8,26 @@ module Lunr
8
8
  end
9
9
 
10
10
  module Klass
11
+ def create hit
12
+ new.tap do |instance|
13
+ instance.id = hit.primary_key
14
+
15
+ properties.each do |name, type|
16
+ value = hit.stored name
17
+
18
+ # For text fields, which always appear to be multiple.
19
+
20
+ if Array === value && value.length == 1 && type == :text
21
+ value = value.first
22
+ end
23
+
24
+ instance.send "#{name}=", value
25
+ end
26
+
27
+ instance.freeze
28
+ end
29
+ end
30
+
11
31
  def first &block
12
32
  search(&block).first
13
33
  end
@@ -20,12 +40,12 @@ module Lunr
20
40
  @scopes ||= {}
21
41
  end
22
42
 
23
- def scope sym = :all, &block
24
- scopes[sym] = block
43
+ def scope name = :all, &block
44
+ scopes[name] = block
25
45
 
26
- unless sym == :all
46
+ unless name == :all
27
47
  class_eval <<-END, __FILE__, __LINE__ + 1
28
- def self.#{sym}; search.#{sym} end
48
+ def self.#{name}; search.#{name} end
29
49
  END
30
50
  end
31
51
  end
@@ -36,12 +56,12 @@ module Lunr
36
56
 
37
57
  alias_method :all, :search
38
58
 
39
- def searches classname, &block
40
- Sunspot::TypeField.alias self, classname
59
+ def searches classname = nil, &block
60
+ Sunspot::TypeField.alias self, classname if classname
41
61
  Sunspot.setup self, &block
42
62
 
43
63
  properties.each do |name, type|
44
- attr_reader name
64
+ attr_accessor name
45
65
  end
46
66
  end
47
67
  end
data/lib/lunr/search.rb CHANGED
@@ -17,8 +17,8 @@ module Lunr
17
17
 
18
18
  all = @klass.scopes[:all]
19
19
 
20
- scope &all if all
21
- scope &block if block_given?
20
+ scope(&all) if all
21
+ scope(&block) if block_given?
22
22
  end
23
23
 
24
24
  def each &block
@@ -37,15 +37,15 @@ module Lunr
37
37
  @executed
38
38
  end
39
39
 
40
- def method_missing sym, *args
41
- return super unless scope = klass.scopes[sym]
40
+ def method_missing name, *args
41
+ return super unless scope = klass.scopes[name]
42
42
 
43
43
  executable!
44
44
 
45
45
  dsl = @search.send :dsl
46
46
 
47
47
  if args.empty?
48
- dsl.instance_eval &scope
48
+ dsl.instance_eval(&scope)
49
49
  else
50
50
  scope.call dsl, args
51
51
  end
@@ -58,8 +58,8 @@ module Lunr
58
58
  end
59
59
 
60
60
  def pages
61
- total / per +
62
- (total_entries % per_page > 0 ? 1 : 0)
61
+ @pages ||= total / per +
62
+ ((total_entries % per_page) > 0 ? 1 : 0)
63
63
  end
64
64
 
65
65
  def params
@@ -70,13 +70,15 @@ module Lunr
70
70
  @per ||= execute && @search.query.per_page
71
71
  end
72
72
 
73
- def respond_to sym, include_private = false
74
- klass.scopes.key?(sym) || super
73
+ def respond_to name, include_private = false
74
+ klass.scopes.key?(name) || super
75
75
  end
76
76
 
77
77
  def scope &block
78
78
  executable!
79
- @search.build &block
79
+ @search.build(&block)
80
+
81
+ self
80
82
  end
81
83
 
82
84
  def total
@@ -95,27 +97,12 @@ module Lunr
95
97
  private
96
98
 
97
99
  def execute
98
- unless @executed
100
+ unless executed?
99
101
  @executed = true
100
102
  @search.execute
101
103
 
102
104
  @results = @search.hits.map do |hit|
103
- # FIX: this belongs in the model.
104
- klass.new.tap do |model|
105
- model.instance_variable_set :"@id", hit.primary_key
106
-
107
- klass.properties.each do |name, type|
108
- value = hit.stored name
109
-
110
- # For text fields, which always appear to be multiple.
111
-
112
- if Array === value && value.length == 1 && type == :text
113
- value = value.first
114
- end
115
-
116
- model.instance_variable_set :"@#{name}", value
117
- end
118
- end
105
+ klass.create hit
119
106
  end
120
107
  end
121
108
 
data/lib/lunr/sunspot.rb CHANGED
@@ -10,18 +10,12 @@ module Sunspot
10
10
  end
11
11
  end
12
12
 
13
- class Field
14
- def stored?
15
- @stored
16
- end
17
- end
18
-
19
13
  module Search
20
14
  class Hit
21
15
  alias_method :original_initialize, :initialize
22
16
 
23
17
  def initialize *args
24
- original_initialize *args
18
+ original_initialize(*args)
25
19
 
26
20
  if clazz = Sunspot::TypeField.aliases_inverted[@class_name]
27
21
  @class_name = clazz.name
@@ -46,6 +40,8 @@ module Sunspot
46
40
  end
47
41
  end
48
42
 
43
+ alias_method :old_to_indexed, :to_indexed
44
+
49
45
  def to_indexed clazz
50
46
  self.class.aliases[clazz] || clazz.name
51
47
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lunr
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 9
5
5
  prerelease: false
6
6
  segments:
7
7
  - 2
8
8
  - 0
9
- - 2
10
- version: 2.0.2
9
+ - 3
10
+ version: 2.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - John Barnette
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-02 00:00:00 -07:00
18
+ date: 2010-09-07 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -165,13 +165,11 @@ dependencies:
165
165
  description: |-
166
166
  A simple read-only interface to Solr, built on Sunspot.
167
167
 
168
- Lunr makes it easy to query and create objects from a Solr index
169
- without requiring all the knowledge, code, and data that was used to
170
- build the index in the first place.
171
-
172
- If you have complex indexes with a stored fields and need to search /
173
- access those fields without access to the original data store, Lunr
174
- might be what you're looking for.
168
+ Lunr makes it easy to query and create objects from a Sunspot-managed
169
+ Solr index without requiring all the knowledge, code, and data used to
170
+ build the index in the first place. If you have complex indexes with a
171
+ stored fields and need to search / access those fields without access
172
+ to the original data store, Lunr might be what you're looking for.
175
173
  email:
176
174
  - code@jbarnette.com
177
175
  executables: []