lunr 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,11 @@
1
+ === 2.0.1 / 2010-09-02
2
+
3
+ * Clean up model and search.
4
+
5
+ === 2.0.0 / 2010-09-01
6
+
7
+ * Reboot.
8
+
1
9
  === 1.0.1 / 2010-08-25
2
10
 
3
11
  * Handle missing :p value correctly.
data/Manifest.txt CHANGED
@@ -7,6 +7,7 @@ Rakefile
7
7
  lib/lunr.rb
8
8
  lib/lunr/errors.rb
9
9
  lib/lunr/model.rb
10
+ lib/lunr/model/klass.rb
10
11
  lib/lunr/search.rb
11
12
  lib/lunr/sunspot.rb
12
13
  test/test_lunr.rb
data/lib/lunr.rb CHANGED
@@ -5,7 +5,8 @@ require "lunr/search"
5
5
  module Lunr
6
6
 
7
7
  # Duh.
8
- VERSION = "2.0.0"
8
+
9
+ VERSION = "2.0.1"
9
10
 
10
11
  end
11
12
 
data/lib/lunr/model.rb CHANGED
@@ -1,45 +1,15 @@
1
- require "lunr/search"
2
- require "lunr/sunspot"
1
+ require "lunr/model/klass"
3
2
 
4
3
  module Lunr
5
4
  module Model
6
- def self.included klass
7
- klass.extend Klass
8
- end
9
-
10
5
  attr_reader :id
11
6
 
12
- module Klass
13
- def properties
14
- @properties ||= []
15
- end
16
-
17
- def scopes
18
- @scopes ||= {}
19
- end
20
-
21
- def scope sym = :all, &block
22
- scopes[sym] = block
23
-
24
- unless sym == :all
25
- class_eval <<-END, __FILE__, __LINE__ + 1
26
- def self.#{sym}; search.#{sym} end
27
- END
28
- end
29
- end
30
-
31
- def search &block
32
- Lunr::Search.new self, &block
33
- end
34
-
35
- alias_method :all, :search
36
-
37
- def searches classname, &block
38
- Sunspot::TypeField.alias self, classname
39
- Sunspot.setup self, &block
7
+ def to_h
8
+ @to_h ||= {}.tap do |h|
9
+ h[:id] = id
40
10
 
41
- properties.uniq.each do |prop|
42
- attr_reader prop
11
+ self.class.properties.each do |name, type|
12
+ h[name] = send name
43
13
  end
44
14
  end
45
15
  end
@@ -0,0 +1,45 @@
1
+ require "lunr/search"
2
+ require "lunr/sunspot"
3
+
4
+ module Lunr
5
+ module Model
6
+ def self.included klass
7
+ klass.extend Klass
8
+ end
9
+
10
+ module Klass
11
+ def properties
12
+ @properties ||= {}
13
+ end
14
+
15
+ def scopes
16
+ @scopes ||= {}
17
+ end
18
+
19
+ def scope sym = :all, &block
20
+ scopes[sym] = block
21
+
22
+ unless sym == :all
23
+ class_eval <<-END, __FILE__, __LINE__ + 1
24
+ def self.#{sym}; search.#{sym} end
25
+ END
26
+ end
27
+ end
28
+
29
+ def search &block
30
+ Lunr::Search.new self, &block
31
+ end
32
+
33
+ alias_method :all, :search
34
+
35
+ def searches classname, &block
36
+ Sunspot::TypeField.alias self, classname
37
+ Sunspot.setup self, &block
38
+
39
+ properties.each do |name, type|
40
+ attr_reader name
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
data/lib/lunr/search.rb CHANGED
@@ -6,39 +6,6 @@ module Lunr
6
6
  class Search
7
7
  include Enumerable
8
8
 
9
- def each &block
10
- execute && @results.each(&block)
11
- end
12
-
13
- def total
14
- execute && @search.total
15
- end
16
-
17
- alias_method :size, :total
18
-
19
- def empty?
20
- 0 == total
21
- end
22
-
23
- # Acting like WillPaginate::Collection
24
-
25
- def current_page
26
- execute && @search.query.page
27
- end
28
-
29
- def per_page
30
- execute && @search.query.per_page
31
- end
32
-
33
- def total_entries
34
- total
35
- end
36
-
37
- def total_pages
38
- total_entries / per_page +
39
- (total_entries % per_page > 0 ? 1 : 0)
40
- end
41
-
42
9
  attr_reader :klass
43
10
 
44
11
  def initialize klass, &block
@@ -46,16 +13,20 @@ module Lunr
46
13
 
47
14
  @executed = false
48
15
  @klass = klass
49
- @search = Sunspot.new_search klass, &block
16
+ @search = Sunspot.new_search klass
50
17
 
51
- if all = @klass.scopes[:all]
52
- scope &all
53
- end
18
+ all = @klass.scopes[:all]
19
+
20
+ scope &all if all
21
+ scope &block if block_given?
54
22
  end
55
23
 
56
- def scope &block
57
- executable!
58
- @search.build &block
24
+ def each &block
25
+ execute && @results.each(&block)
26
+ end
27
+
28
+ def empty?
29
+ 0 == total
59
30
  end
60
31
 
61
32
  def executable!
@@ -66,14 +37,8 @@ module Lunr
66
37
  @executed
67
38
  end
68
39
 
69
- # :nodoc:
70
-
71
- def params
72
- @search.query.to_params
73
- end
74
-
75
40
  def method_missing sym, *args
76
- super unless scope = klass.scopes[sym]
41
+ return super unless scope = klass.scopes[sym]
77
42
 
78
43
  executable!
79
44
 
@@ -88,10 +53,45 @@ module Lunr
88
53
  self
89
54
  end
90
55
 
56
+ def page
57
+ @page ||= execute && @search.query.page
58
+ end
59
+
60
+ def pages
61
+ total / per +
62
+ (total_entries % per_page > 0 ? 1 : 0)
63
+ end
64
+
65
+ def params
66
+ @search.query.to_params
67
+ end
68
+
69
+ def per
70
+ @per ||= execute && @search.query.per_page
71
+ end
72
+
91
73
  def respond_to sym, include_private = false
92
74
  klass.scopes.key?(sym) || super
93
75
  end
94
76
 
77
+ def scope &block
78
+ executable!
79
+ @search.build &block
80
+ end
81
+
82
+ def total
83
+ @total ||= execute && @search.total
84
+ end
85
+
86
+ alias_method :size, :total
87
+
88
+ # Quack like WillPaginate::Collection
89
+
90
+ alias_method :current_page, :page
91
+ alias_method :per_page, :per
92
+ alias_method :total_entries, :total
93
+ alias_method :total_pages, :pages
94
+
95
95
  private
96
96
 
97
97
  def execute
@@ -100,11 +100,20 @@ module Lunr
100
100
  @search.execute
101
101
 
102
102
  @results = @search.hits.map do |hit|
103
+ # FIX: this belongs in the model.
103
104
  klass.new.tap do |model|
104
105
  model.instance_variable_set :"@id", hit.primary_key
105
106
 
106
- klass.properties.each do |prop|
107
- model.instance_variable_set :"@#{prop}", hit.stored(prop)
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
108
117
  end
109
118
  end
110
119
  end
data/lib/lunr/sunspot.rb CHANGED
@@ -4,7 +4,7 @@ module Sunspot
4
4
  module DSL
5
5
  class Fields
6
6
  def property name, type, options = {}
7
- @setup.clazz.properties << name
7
+ @setup.clazz.properties[name] = type
8
8
  send type, name, options.merge(:stored => true)
9
9
  end
10
10
  end
@@ -30,12 +30,6 @@ module Sunspot
30
30
  end
31
31
  end
32
32
 
33
- class Setup
34
- def lunr_properties
35
- @lunr_properties ||= []
36
- end
37
- end
38
-
39
33
  class TypeField
40
34
  class << self
41
35
  def alias(dest_class, source_class_name)
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: 15
4
+ hash: 13
5
5
  prerelease: false
6
6
  segments:
7
7
  - 2
8
8
  - 0
9
- - 0
10
- version: 2.0.0
9
+ - 1
10
+ version: 2.0.1
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-01 00:00:00 -07:00
18
+ date: 2010-09-02 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -192,6 +192,7 @@ files:
192
192
  - lib/lunr.rb
193
193
  - lib/lunr/errors.rb
194
194
  - lib/lunr/model.rb
195
+ - lib/lunr/model/klass.rb
195
196
  - lib/lunr/search.rb
196
197
  - lib/lunr/sunspot.rb
197
198
  - test/test_lunr.rb