mongomapper_ext 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -11,7 +11,7 @@ begin
11
11
  gem.homepage = "http://github.com/dcu/mongomapper_ext"
12
12
  gem.authors = ["David A. Cuadrado"]
13
13
 
14
- gem.add_dependency('mongo_mapper', '~> 0.7')
14
+ gem.add_dependency('mongo_mapper', '~> 0.8')
15
15
  gem.add_dependency('uuidtools', '>= 2.0.0')
16
16
 
17
17
  gem.add_development_dependency("shoulda", ">= 2.10.2")
data/TODO ADDED
@@ -0,0 +1,2 @@
1
+ - arreglar si el stemmer no soporta el lenguaje
2
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -14,6 +14,8 @@ rescue LoadError
14
14
  $stderr.puts "disabling `magic` support. use 'gem install magic' to enable it"
15
15
  end
16
16
 
17
+ require 'mongomapper_ext/paginator'
18
+
17
19
  # types
18
20
  require 'mongomapper_ext/types/open_struct'
19
21
  require 'mongomapper_ext/types/timestamp'
@@ -24,7 +24,7 @@ module MongoMapperExt
24
24
  io = StringIO.new(io)
25
25
  end
26
26
 
27
- if defined?(Magic)
27
+ if defined?(Magic) && Magic.respond_to?(:guess_string_mime_type)
28
28
  data = io.read(256) # be nice with memory usage
29
29
  self.content_type = options[:content_type] = Magic.guess_string_mime_type(data.to_s)
30
30
  self.extension ||= options[:content_type].to_s.split("/").last.split("-").last
@@ -38,7 +38,7 @@ module MongoMapperExt
38
38
 
39
39
  stemmed = []
40
40
  if defined?(Lingua)
41
- stemmer = Lingua::Stemmer.new(:language => language)
41
+ stemmer = MongoMapperExt::Filter.build_stemmer(language)
42
42
  original_words.each do |word|
43
43
  stemmed_word = stemmer.stem(word)
44
44
  stemmed << stemmed_word unless original_words.include?(stemmed_word)
@@ -54,11 +54,11 @@ module MongoMapperExt
54
54
  page = opts.delete(:page) || 1
55
55
  select = opts.delete(:select) || self.keys.keys
56
56
 
57
- criteria, options = to_query(opts)
57
+ query = Plucky::Query.new(self.collection, opts)
58
58
 
59
- results = self.database.eval("function(collection, q, config) { return filter(collection, q, config); }", self.collection_name, criteria.merge({"_keywords" => {:$in => regex}}), {:words => original_words.to_a, :stemmed => stemmed.to_a, :limit => limit, :min_score => min_score, :select => select })
59
+ results = self.database.eval("function(collection, q, config) { return filter(collection, q, config); }", self.collection_name, query.criteria.source.merge({"_keywords" => {:$in => regex}}), {:words => original_words.to_a, :stemmed => stemmed.to_a, :limit => limit, :min_score => min_score, :select => select })
60
60
 
61
- pagination = MongoMapper::Plugins::Pagination::Proxy.new(results["total_entries"], page, limit)
61
+ pagination = Paginator.new(results["total_entries"], page, limit)
62
62
 
63
63
  pagination.subject = results['results'].map do |result|
64
64
  item = self.new(result['doc'])
@@ -82,7 +82,7 @@ module MongoMapperExt
82
82
 
83
83
  stemmer = nil
84
84
  if defined?(Lingua)
85
- stemmer = Lingua::Stemmer.new(:language => lang)
85
+ stemmer = MongoMapperExt::Filter.build_stemmer(lang)
86
86
  end
87
87
 
88
88
  stop_words = []
@@ -142,6 +142,14 @@ module MongoMapperExt
142
142
  end
143
143
  end
144
144
 
145
+ def self.build_stemmer(language)
146
+ begin
147
+ return Lingua::Stemmer.new(:language => language)
148
+ rescue Lingua::StemmerError
149
+ return nil
150
+ end
151
+ end
152
+
145
153
  def en_stop_words
146
154
  ["a", "about", "above", "after", "again", "against", "all", "am", "an",
147
155
  "and", "any", "are", "aren't", "as", "at", "be", "because", "been",
@@ -0,0 +1,69 @@
1
+ # encoding: UTF-8
2
+ module MongoMapperExt
3
+ class Paginator
4
+ instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|respond_to\?|proxy_|^object_id$)/ }
5
+
6
+ attr_accessor :subject
7
+ attr_reader :total_entries, :per_page, :current_page
8
+ alias limit per_page
9
+
10
+ def initialize(total_entries, current_page, per_page=nil)
11
+ @total_entries = total_entries.to_i
12
+ self.per_page = per_page
13
+ self.current_page = current_page
14
+ end
15
+
16
+ def total_pages
17
+ (total_entries / per_page.to_f).ceil
18
+ end
19
+
20
+ def out_of_bounds?
21
+ current_page > total_pages
22
+ end
23
+
24
+ def previous_page
25
+ current_page > 1 ? (current_page - 1) : nil
26
+ end
27
+
28
+ def next_page
29
+ current_page < total_pages ? (current_page + 1) : nil
30
+ end
31
+
32
+ def skip
33
+ (current_page - 1) * per_page
34
+ end
35
+ alias offset skip # for will paginate support
36
+
37
+ def send(method, *args, &block)
38
+ if respond_to?(method)
39
+ super
40
+ else
41
+ subject.send(method, *args, &block)
42
+ end
43
+ end
44
+
45
+ def ===(other)
46
+ other === subject
47
+ end
48
+
49
+ def method_missing(name, *args, &block)
50
+ @subject.send(name, *args, &block)
51
+ end
52
+
53
+ def respond_to?(name, *args, &block)
54
+ super || @subject.respond_to?(name, *args, &block)
55
+ end
56
+
57
+ private
58
+ def per_page=(value)
59
+ value = 25 if value.blank?
60
+ @per_page = value.to_i
61
+ end
62
+
63
+ def current_page=(value)
64
+ value = value.to_i
65
+ value = 1 if value < 1
66
+ @current_page = value
67
+ end
68
+ end
69
+ end
@@ -5,16 +5,17 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongomapper_ext}
8
- s.version = "0.3.0"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["David A. Cuadrado"]
12
- s.date = %q{2010-05-08}
12
+ s.date = %q{2010-06-20}
13
13
  s.description = %q{MongoMapper extensions}
14
14
  s.email = %q{krawek@gmail.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
- "README.rdoc"
17
+ "README.rdoc",
18
+ "TODO"
18
19
  ]
19
20
  s.files = [
20
21
  ".document",
@@ -38,6 +39,7 @@ Gem::Specification.new do |s|
38
39
  "lib/mongomapper_ext/js/filter.js",
39
40
  "lib/mongomapper_ext/js/find_tags.js",
40
41
  "lib/mongomapper_ext/js/tag_cloud.js",
42
+ "lib/mongomapper_ext/paginator.rb",
41
43
  "lib/mongomapper_ext/slugizer.rb",
42
44
  "lib/mongomapper_ext/storage.rb",
43
45
  "lib/mongomapper_ext/tags.rb",
@@ -90,20 +92,20 @@ Gem::Specification.new do |s|
90
92
  s.specification_version = 3
91
93
 
92
94
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
93
- s.add_runtime_dependency(%q<mongo_mapper>, ["~> 0.7"])
95
+ s.add_runtime_dependency(%q<mongo_mapper>, ["~> 0.8"])
94
96
  s.add_runtime_dependency(%q<uuidtools>, [">= 2.0.0"])
95
97
  s.add_development_dependency(%q<shoulda>, [">= 2.10.2"])
96
98
  s.add_development_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
97
99
  s.add_development_dependency(%q<mocha>, [">= 0.9.4"])
98
100
  else
99
- s.add_dependency(%q<mongo_mapper>, ["~> 0.7"])
101
+ s.add_dependency(%q<mongo_mapper>, ["~> 0.8"])
100
102
  s.add_dependency(%q<uuidtools>, [">= 2.0.0"])
101
103
  s.add_dependency(%q<shoulda>, [">= 2.10.2"])
102
104
  s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
103
105
  s.add_dependency(%q<mocha>, [">= 0.9.4"])
104
106
  end
105
107
  else
106
- s.add_dependency(%q<mongo_mapper>, ["~> 0.7"])
108
+ s.add_dependency(%q<mongo_mapper>, ["~> 0.8"])
107
109
  s.add_dependency(%q<uuidtools>, [">= 2.0.0"])
108
110
  s.add_dependency(%q<shoulda>, [">= 2.10.2"])
109
111
  s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
7
+ - 4
8
8
  - 0
9
- version: 0.3.0
9
+ version: 0.4.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - David A. Cuadrado
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-08 00:00:00 -05:00
17
+ date: 2010-06-20 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -26,8 +26,8 @@ dependencies:
26
26
  - !ruby/object:Gem::Version
27
27
  segments:
28
28
  - 0
29
- - 7
30
- version: "0.7"
29
+ - 8
30
+ version: "0.8"
31
31
  type: :runtime
32
32
  version_requirements: *id001
33
33
  - !ruby/object:Gem::Dependency
@@ -95,6 +95,7 @@ extensions: []
95
95
  extra_rdoc_files:
96
96
  - LICENSE
97
97
  - README.rdoc
98
+ - TODO
98
99
  files:
99
100
  - .document
100
101
  - .gitignore
@@ -117,6 +118,7 @@ files:
117
118
  - lib/mongomapper_ext/js/filter.js
118
119
  - lib/mongomapper_ext/js/find_tags.js
119
120
  - lib/mongomapper_ext/js/tag_cloud.js
121
+ - lib/mongomapper_ext/paginator.rb
120
122
  - lib/mongomapper_ext/slugizer.rb
121
123
  - lib/mongomapper_ext/storage.rb
122
124
  - lib/mongomapper_ext/tags.rb
@@ -136,6 +138,7 @@ files:
136
138
  - test/types/test_open_struct.rb
137
139
  - test/types/test_set.rb
138
140
  - test/types/test_timestamp.rb
141
+ - TODO
139
142
  has_rdoc: true
140
143
  homepage: http://github.com/dcu/mongomapper_ext
141
144
  licenses: []