mongomapper_ext 0.0.4 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +1 -2
- data/VERSION +1 -1
- data/examples/slugizer.rb +2 -1
- data/lib/mongomapper_ext.rb +0 -1
- data/lib/mongomapper_ext/file.rb +9 -2
- data/lib/mongomapper_ext/filter.rb +18 -7
- data/lib/mongomapper_ext/slugizer.rb +19 -11
- data/lib/mongomapper_ext/storage.rb +6 -1
- data/lib/mongomapper_ext/update.rb +4 -0
- data/mongomapper_ext.gemspec +101 -0
- metadata +4 -14
- data/lib/mongomapper_ext/types/set.rb +0 -9
data/Rakefile
CHANGED
@@ -11,8 +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.
|
15
|
-
gem.add_dependency('ruby-stemmer', '>= 0.5.3')
|
14
|
+
gem.add_dependency('mongo_mapper', '>= 0.6.10')
|
16
15
|
gem.add_dependency('uuidtools', '>= 2.0.0')
|
17
16
|
|
18
17
|
gem.add_development_dependency("shoulda", ">= 2.10.2")
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/examples/slugizer.rb
CHANGED
@@ -4,7 +4,7 @@ class SlugizerEx
|
|
4
4
|
include MongoMapper::Document
|
5
5
|
include MongoMapperExt::Slugizer
|
6
6
|
|
7
|
-
slug_key :title
|
7
|
+
slug_key :title, :unique => true
|
8
8
|
|
9
9
|
key :title
|
10
10
|
end
|
@@ -13,3 +13,4 @@ u = SlugizerEx.create(:title => "The title of the blogpost!!!")
|
|
13
13
|
|
14
14
|
puts u.slug
|
15
15
|
puts SlugizerEx.by_slug(u.slug).inspect
|
16
|
+
|
data/lib/mongomapper_ext.rb
CHANGED
data/lib/mongomapper_ext/file.rb
CHANGED
@@ -23,9 +23,16 @@ module MongoMapperExt
|
|
23
23
|
|
24
24
|
def self.fetch(owner, filename)
|
25
25
|
db = owner.class.database
|
26
|
-
|
26
|
+
finder = nil
|
27
|
+
if defined?(MongoMapper::FinderOptions)
|
28
|
+
finder = MongoMapper::FinderOptions
|
29
|
+
else
|
30
|
+
finder = MongoMapper::Query
|
31
|
+
end
|
32
|
+
|
33
|
+
criteria, options = finder.new(owner.class, :filename => filename, :metadata => {:_id => owner.id}, :limit => 1).to_a
|
27
34
|
|
28
|
-
obj = db.collection("#{owner.collection.name}.files").find(criteria, options).
|
35
|
+
obj = db.collection("#{owner.collection.name}.files").find(criteria, options).next_document
|
29
36
|
|
30
37
|
if obj
|
31
38
|
self.new(owner, obj)
|
@@ -1,7 +1,11 @@
|
|
1
1
|
module MongoMapperExt
|
2
2
|
module Filter
|
3
3
|
def self.included(klass)
|
4
|
-
|
4
|
+
begin
|
5
|
+
require 'lingua/stemmer'
|
6
|
+
rescue LoadError
|
7
|
+
$stderr.puts "install ruby-stemmer `gem install ruby-stemmer` to activate the full text search support"
|
8
|
+
end
|
5
9
|
|
6
10
|
klass.class_eval do
|
7
11
|
extend ClassMethods
|
@@ -32,7 +36,7 @@ module MongoMapperExt
|
|
32
36
|
if opts[:per_page]
|
33
37
|
self.paginate(opts.deep_merge(:conditions => {:_keywords => /^(#{q}).*/ }))
|
34
38
|
else
|
35
|
-
self.
|
39
|
+
self.all(opts.deep_merge(:conditions => {:_keywords => /^(#{q}).*/ }))
|
36
40
|
end
|
37
41
|
end
|
38
42
|
end
|
@@ -46,19 +50,26 @@ module MongoMapperExt
|
|
46
50
|
lang = lang.call(self)
|
47
51
|
end
|
48
52
|
|
49
|
-
|
53
|
+
stemmer = nil
|
54
|
+
if defined?(Lingua)
|
55
|
+
stemmer = Lingua::Stemmer.new(:language => lang)
|
56
|
+
end
|
50
57
|
|
51
58
|
self._keywords = []
|
52
59
|
self.class.filterable_keys.each do |key|
|
53
|
-
self._keywords += keywords_for_value(
|
60
|
+
self._keywords += keywords_for_value(read_attribute(key), stemmer)
|
54
61
|
end
|
55
62
|
end
|
56
63
|
|
57
64
|
private
|
58
|
-
def keywords_for_value(
|
65
|
+
def keywords_for_value(val, stemmer=nil)
|
59
66
|
if val.kind_of?(String)
|
60
67
|
val.downcase.split.map do |word|
|
61
|
-
stem =
|
68
|
+
stem = word
|
69
|
+
if stemmer
|
70
|
+
stem = stemmer.stem(word)
|
71
|
+
end
|
72
|
+
|
62
73
|
if stem != word
|
63
74
|
[stem, word]
|
64
75
|
else
|
@@ -66,7 +77,7 @@ module MongoMapperExt
|
|
66
77
|
end
|
67
78
|
end.flatten
|
68
79
|
elsif val.kind_of?(Array)
|
69
|
-
val.map { |e| keywords_for_value(
|
80
|
+
val.map { |e| keywords_for_value(e, stemmer) }.flatten
|
70
81
|
else
|
71
82
|
[val]
|
72
83
|
end
|
@@ -5,33 +5,37 @@ module MongoMapperExt
|
|
5
5
|
extend ClassMethods
|
6
6
|
extend Finder
|
7
7
|
|
8
|
-
key :slug, String
|
9
|
-
ensure_index :slug
|
8
|
+
key :slug, String, :index => true
|
10
9
|
|
11
10
|
before_validation_on_create :generate_slug
|
12
11
|
end
|
13
12
|
end
|
14
13
|
|
15
14
|
def to_param
|
16
|
-
|
17
|
-
self.id
|
18
|
-
else
|
19
|
-
self.slug
|
20
|
-
end
|
15
|
+
self.slug.blank? ? self.id : self.slug
|
21
16
|
end
|
22
17
|
|
23
18
|
protected
|
24
19
|
def generate_slug
|
25
20
|
if self.slug.blank?
|
26
|
-
|
27
|
-
|
21
|
+
slug = self[self.class.slug_key].gsub(/[^A-Za-z0-9\s\-]/, "")[0,20].strip.gsub(/\s+/, "-").downcase
|
22
|
+
if !self.class.slug_options[:unique]
|
23
|
+
key = UUIDTools::UUID.random_create.hexdigest[0,4] #optimize
|
24
|
+
self.slug = key+"-"+slug
|
25
|
+
else
|
26
|
+
self.slug = slug
|
27
|
+
end
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
31
|
module ClassMethods
|
32
|
-
def slug_key(key = :name)
|
32
|
+
def slug_key(key = :name, options = {})
|
33
|
+
@slug_options ||= options
|
33
34
|
@slug_key ||= key
|
34
35
|
end
|
36
|
+
class_eval do
|
37
|
+
attr_reader :slug_options
|
38
|
+
end
|
35
39
|
end
|
36
40
|
|
37
41
|
module Finder
|
@@ -43,4 +47,8 @@ module MongoMapperExt
|
|
43
47
|
end
|
44
48
|
end
|
45
49
|
|
46
|
-
MongoMapper::Associations
|
50
|
+
if defined?(MongoMapper::Associations)
|
51
|
+
MongoMapper::Associations::Proxy.send(:include, MongoMapperExt::Slugizer::Finder)
|
52
|
+
else
|
53
|
+
MongoMapper::Plugins::Associations::Proxy.send(:include, MongoMapperExt::Slugizer::Finder)
|
54
|
+
end
|
@@ -50,6 +50,7 @@ module MongoMapperExt
|
|
50
50
|
|
51
51
|
module ClassMethods
|
52
52
|
def file_key(name)
|
53
|
+
key "_#{name}", String
|
53
54
|
define_method("#{name}=") do |file|
|
54
55
|
file_id = UUIDTools::UUID.random_create.hexdigest
|
55
56
|
filename = name
|
@@ -65,7 +66,11 @@ module MongoMapperExt
|
|
65
66
|
end
|
66
67
|
|
67
68
|
define_method(name) do
|
68
|
-
fetch_file(self["_#{name}"])
|
69
|
+
fetch_file(self["_#{name}"]) if metaclass.keys.has_key?("_#{name}")
|
70
|
+
end
|
71
|
+
|
72
|
+
define_method("has_#{name}?") do
|
73
|
+
!self["_#{name}"].blank?
|
69
74
|
end
|
70
75
|
end
|
71
76
|
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{mongomapper_ext}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["David A. Cuadrado"]
|
12
|
+
s.date = %q{2010-02-11}
|
13
|
+
s.description = %q{MongoMapper extensions}
|
14
|
+
s.email = %q{krawek@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"examples/filter.rb",
|
27
|
+
"examples/helper.rb",
|
28
|
+
"examples/slugizer.rb",
|
29
|
+
"examples/storage.rb",
|
30
|
+
"examples/types.rb",
|
31
|
+
"examples/update.rb",
|
32
|
+
"lib/mongomapper_ext.rb",
|
33
|
+
"lib/mongomapper_ext/file.rb",
|
34
|
+
"lib/mongomapper_ext/filter.rb",
|
35
|
+
"lib/mongomapper_ext/slugizer.rb",
|
36
|
+
"lib/mongomapper_ext/storage.rb",
|
37
|
+
"lib/mongomapper_ext/types/open_struct.rb",
|
38
|
+
"lib/mongomapper_ext/types/timestamp.rb",
|
39
|
+
"lib/mongomapper_ext/update.rb",
|
40
|
+
"mongomapper_ext.gemspec",
|
41
|
+
"test/helper.rb",
|
42
|
+
"test/models.rb",
|
43
|
+
"test/support/custom_matchers.rb",
|
44
|
+
"test/test_filter.rb",
|
45
|
+
"test/test_slugizer.rb",
|
46
|
+
"test/test_storage.rb",
|
47
|
+
"test/test_update.rb",
|
48
|
+
"test/types/test_open_struct.rb",
|
49
|
+
"test/types/test_set.rb",
|
50
|
+
"test/types/test_timestamp.rb"
|
51
|
+
]
|
52
|
+
s.homepage = %q{http://github.com/dcu/mongomapper_ext}
|
53
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
54
|
+
s.require_paths = ["lib"]
|
55
|
+
s.rubygems_version = %q{1.3.5}
|
56
|
+
s.summary = %q{MongoMapper extensions}
|
57
|
+
s.test_files = [
|
58
|
+
"test/test_slugizer.rb",
|
59
|
+
"test/test_filter.rb",
|
60
|
+
"test/test_storage.rb",
|
61
|
+
"test/test_update.rb",
|
62
|
+
"test/support/custom_matchers.rb",
|
63
|
+
"test/models.rb",
|
64
|
+
"test/helper.rb",
|
65
|
+
"test/types/test_open_struct.rb",
|
66
|
+
"test/types/test_timestamp.rb",
|
67
|
+
"test/types/test_set.rb",
|
68
|
+
"examples/types.rb",
|
69
|
+
"examples/filter.rb",
|
70
|
+
"examples/update.rb",
|
71
|
+
"examples/storage.rb",
|
72
|
+
"examples/helper.rb",
|
73
|
+
"examples/slugizer.rb"
|
74
|
+
]
|
75
|
+
|
76
|
+
if s.respond_to? :specification_version then
|
77
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
78
|
+
s.specification_version = 3
|
79
|
+
|
80
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
81
|
+
s.add_runtime_dependency(%q<mongo_mapper>, [">= 0.6.10"])
|
82
|
+
s.add_runtime_dependency(%q<uuidtools>, [">= 2.0.0"])
|
83
|
+
s.add_development_dependency(%q<shoulda>, [">= 2.10.2"])
|
84
|
+
s.add_development_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
|
85
|
+
s.add_development_dependency(%q<mocha>, [">= 0.9.4"])
|
86
|
+
else
|
87
|
+
s.add_dependency(%q<mongo_mapper>, [">= 0.6.10"])
|
88
|
+
s.add_dependency(%q<uuidtools>, [">= 2.0.0"])
|
89
|
+
s.add_dependency(%q<shoulda>, [">= 2.10.2"])
|
90
|
+
s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
|
91
|
+
s.add_dependency(%q<mocha>, [">= 0.9.4"])
|
92
|
+
end
|
93
|
+
else
|
94
|
+
s.add_dependency(%q<mongo_mapper>, [">= 0.6.10"])
|
95
|
+
s.add_dependency(%q<uuidtools>, [">= 2.0.0"])
|
96
|
+
s.add_dependency(%q<shoulda>, [">= 2.10.2"])
|
97
|
+
s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
|
98
|
+
s.add_dependency(%q<mocha>, [">= 0.9.4"])
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongomapper_ext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David A. Cuadrado
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2010-02-11 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -20,17 +20,7 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - ">="
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version: 0.
|
24
|
-
version:
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
|
-
name: ruby-stemmer
|
27
|
-
type: :runtime
|
28
|
-
version_requirement:
|
29
|
-
version_requirements: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: 0.5.3
|
23
|
+
version: 0.6.10
|
34
24
|
version:
|
35
25
|
- !ruby/object:Gem::Dependency
|
36
26
|
name: uuidtools
|
@@ -100,9 +90,9 @@ files:
|
|
100
90
|
- lib/mongomapper_ext/slugizer.rb
|
101
91
|
- lib/mongomapper_ext/storage.rb
|
102
92
|
- lib/mongomapper_ext/types/open_struct.rb
|
103
|
-
- lib/mongomapper_ext/types/set.rb
|
104
93
|
- lib/mongomapper_ext/types/timestamp.rb
|
105
94
|
- lib/mongomapper_ext/update.rb
|
95
|
+
- mongomapper_ext.gemspec
|
106
96
|
- test/helper.rb
|
107
97
|
- test/models.rb
|
108
98
|
- test/support/custom_matchers.rb
|