mongomapper_ext 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
data/examples/tags.rb ADDED
@@ -0,0 +1,26 @@
1
+ require './helper'
2
+
3
+ MongoMapperExt.init
4
+
5
+ class TagsEx
6
+ include MongoMapper::Document
7
+ include MongoMapperExt::Tags
8
+
9
+ key :title
10
+ end
11
+
12
+ TagsEx.delete_all
13
+ TagsEx.create!(:title => "The title of the blogpost!!!", :tags => ["tag1", "tag2", "tag3"])
14
+ TagsEx.create!(:title => "testing tags", :tags => ["tag1", "tag4", "tag2"])
15
+ TagsEx.create!(:title => "more tags", :tags => ["tag1", "tag3", "tag5"])
16
+
17
+ puts ">> Tag Cloud"
18
+ puts TagsEx.tag_cloud.inspect
19
+
20
+ puts ">> with tag = tag5"
21
+ puts TagsEx.find_with_tags("tag5").inspect
22
+
23
+ puts ">> all tags that start with t"
24
+ puts TagsEx.find_tags(/^t/).inspect
25
+
26
+ TagsEx.delete_all
@@ -0,0 +1,24 @@
1
+ function findTags(collection, regex, query, limit) {
2
+ var tags = db.eval(
3
+ function(collection, regex, query){
4
+ var tags = [];
5
+ db[collection].find(query, {"tags":1}).limit(500).forEach(
6
+ function(p){
7
+ if ( p.tags ){
8
+ for ( var i=0; i<p.tags.length; i++ ){
9
+ var name = p.tags[i];
10
+ if(name.match(regex) != null && tags.indexOf(name) == -1)
11
+ tags.push(name);
12
+ }
13
+ }
14
+ }
15
+ );
16
+ return tags;
17
+ },
18
+ collection,
19
+ regex,
20
+ query
21
+ );
22
+
23
+ return tags.slice(0,limit||30);
24
+ }
@@ -0,0 +1,29 @@
1
+ // TODO: port it to map reduce
2
+ function tagCloud(collection, q, limit) {
3
+ var counts = db.eval(
4
+ function(collection, q){
5
+ var counts = {};
6
+ db[collection].find(q, {"tags":1}).limit(500).forEach(
7
+ function(p){
8
+ if ( p.tags ){
9
+ for ( var i=0; i<p.tags.length; i++ ){
10
+ var name = p.tags[i];
11
+ counts[name] = 1 + ( counts[name] || 0 );
12
+ }
13
+ }
14
+ }
15
+ );
16
+ return counts;
17
+ },
18
+ collection,
19
+ q
20
+ );
21
+
22
+ // maybe sort to by nice
23
+ var sorted = [];
24
+ for ( var tag in counts ){
25
+ sorted.push( { name : tag , count : counts[tag] } )
26
+ }
27
+
28
+ return sorted.slice(0,limit||30);
29
+ }
@@ -0,0 +1,27 @@
1
+ module MongoMapperExt
2
+ module Tags
3
+ def self.included(klass)
4
+ klass.class_eval do
5
+ extend ClassMethods
6
+
7
+ key :tags, Array, :index => true
8
+ end
9
+ end
10
+
11
+ module ClassMethods
12
+ def tag_cloud(conditions = {}, limit = 30)
13
+ self.database.eval("function(collection, q,l) { return tag_cloud(collection, q,l); }", self.collection_name, conditions, limit)
14
+ end
15
+
16
+ # Model.find_with_tags("budget", "big", :limit => 4)
17
+ def find_with_tags(*tags)
18
+ options = tags.extract_options!
19
+ self.all(options.merge(:tags => tags))
20
+ end
21
+
22
+ def find_tags(regex, conditions = {}, limit = 30)
23
+ self.database.eval("function(collection, a,b,c) { return find_tags(collection, a,b,c); }", self.collection_name, regex, conditions, limit)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -20,3 +20,22 @@ require 'mongomapper_ext/filter'
20
20
 
21
21
  # slug
22
22
  require 'mongomapper_ext/slugizer'
23
+
24
+ # tags
25
+ require 'mongomapper_ext/tags'
26
+
27
+ module MongoMapperExt
28
+ def self.init
29
+ load_jsfiles(::File.dirname(__FILE__)+"/mongomapper_ext/js")
30
+ end
31
+
32
+ def self.load_jsfiles(path)
33
+ Dir.glob(::File.join(path, "*.js")) do |js_path|
34
+ code = ::File.read(js_path)
35
+ name = ::File.basename(js_path, ".js")
36
+
37
+ # HACK: looks like ruby driver doesn't support this
38
+ MongoMapper.database.eval("db.system.js.save({_id: '#{name}', value: #{code}})")
39
+ end
40
+ end
41
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongomapper_ext}
8
- s.version = "0.1.2"
8
+ s.version = "0.1.3"
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-02-24}
12
+ s.date = %q{2010-02-28}
13
13
  s.description = %q{MongoMapper extensions}
14
14
  s.email = %q{krawek@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -27,13 +27,17 @@ Gem::Specification.new do |s|
27
27
  "examples/helper.rb",
28
28
  "examples/slugizer.rb",
29
29
  "examples/storage.rb",
30
+ "examples/tags.rb",
30
31
  "examples/types.rb",
31
32
  "examples/update.rb",
32
33
  "lib/mongomapper_ext.rb",
33
34
  "lib/mongomapper_ext/file.rb",
34
35
  "lib/mongomapper_ext/filter.rb",
36
+ "lib/mongomapper_ext/js/find_tags.js",
37
+ "lib/mongomapper_ext/js/tag_cloud.js",
35
38
  "lib/mongomapper_ext/slugizer.rb",
36
39
  "lib/mongomapper_ext/storage.rb",
40
+ "lib/mongomapper_ext/tags.rb",
37
41
  "lib/mongomapper_ext/types/open_struct.rb",
38
42
  "lib/mongomapper_ext/types/timestamp.rb",
39
43
  "lib/mongomapper_ext/update.rb",
@@ -44,6 +48,7 @@ Gem::Specification.new do |s|
44
48
  "test/test_filter.rb",
45
49
  "test/test_slugizer.rb",
46
50
  "test/test_storage.rb",
51
+ "test/test_tags.rb",
47
52
  "test/test_update.rb",
48
53
  "test/types/test_open_struct.rb",
49
54
  "test/types/test_set.rb",
@@ -57,6 +62,7 @@ Gem::Specification.new do |s|
57
62
  s.test_files = [
58
63
  "test/test_slugizer.rb",
59
64
  "test/test_filter.rb",
65
+ "test/test_tags.rb",
60
66
  "test/test_storage.rb",
61
67
  "test/test_update.rb",
62
68
  "test/support/custom_matchers.rb",
@@ -68,6 +74,7 @@ Gem::Specification.new do |s|
68
74
  "examples/types.rb",
69
75
  "examples/filter.rb",
70
76
  "examples/update.rb",
77
+ "examples/tags.rb",
71
78
  "examples/storage.rb",
72
79
  "examples/helper.rb",
73
80
  "examples/slugizer.rb"
data/test/helper.rb CHANGED
@@ -14,10 +14,12 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
14
  $LOAD_PATH.unshift(File.dirname(__FILE__))
15
15
 
16
16
  require 'mongomapper_ext'
17
+ MongoMapper.database = 'test'
18
+
17
19
  require 'models'
18
20
 
19
21
  class Test::Unit::TestCase
20
22
  include CustomMatchers
21
23
  end
22
24
 
23
- MongoMapper.database = 'test'
25
+ MongoMapperExt.init
data/test/models.rb CHANGED
@@ -36,6 +36,7 @@ class BlogPost # for Slug and Filter
36
36
  include MongoMapper::Document
37
37
  include MongoMapperExt::Filter
38
38
  include MongoMapperExt::Slugizer
39
+ include MongoMapperExt::Tags
39
40
 
40
41
  filterable_keys :title, :body, :tags, :date
41
42
  slug_key :title
data/test/test_tags.rb ADDED
@@ -0,0 +1,46 @@
1
+ require 'helper'
2
+
3
+ class TestTags < Test::Unit::TestCase
4
+ context "working with tags" do
5
+ setup do
6
+ BlogPost.delete_all
7
+ @blogpost = BlogPost.create(:title => "operation systems",
8
+ :body => "list of some operating systems",
9
+ :tags => %w[list windows freebsd osx linux])
10
+ @blogpost2 = BlogPost.create(:title => "nosql database",
11
+ :body => "list of some nosql databases",
12
+ :tags => %w[list mongodb redis couchdb])
13
+ end
14
+
15
+ should "generate the tagcloud" do
16
+ cloud = BlogPost.tag_cloud
17
+
18
+ [{"name"=>"list", "count"=>2.0},
19
+ {"name"=>"windows", "count"=>1.0},
20
+ {"name"=>"freebsd", "count"=>1.0},
21
+ {"name"=>"osx", "count"=>1.0},
22
+ {"name"=>"linux", "count"=>1.0},
23
+ {"name"=>"mongodb", "count"=>1.0},
24
+ {"name"=>"redis", "count"=>1.0},
25
+ {"name"=>"couchdb", "count"=>1.0}].each do |entry|
26
+ cloud.should include(entry)
27
+ end
28
+ end
29
+
30
+ should "find blogpost that include the given tags" do
31
+ BlogPost.find_with_tags("mongodb").should == [@blogpost2]
32
+ posts = BlogPost.find_with_tags("mongodb", "linux")
33
+ posts.should include(@blogpost)
34
+ posts.should include(@blogpost2)
35
+ posts.size.should == 2
36
+ end
37
+
38
+ should "find tags that start with li" do
39
+ tags = BlogPost.find_tags(/^li/)
40
+ tags.should include("linux")
41
+ tags.should include("list")
42
+ tags.size.should == 2
43
+ end
44
+
45
+ end
46
+ end
@@ -32,7 +32,7 @@ class TimestampTest < Test::Unit::TestCase
32
32
  @event2 = Event.create!(:start_date => start_time.utc, :end_datime => end_time.utc)
33
33
 
34
34
  Event.count.should == 2
35
- events = Event.find(:all, :$where => ("this.start_date >= %d && this.start_date <= %d" % [@event.start_date.yesterday.to_i, @event2.start_date.yesterday.to_i]))
35
+ events = Event.all(:$where => ("this.start_date >= %d && this.start_date <= %d" % [@event.start_date.yesterday.to_i, @event2.start_date.yesterday.to_i]))
36
36
 
37
37
  events.should == [@event]
38
38
  end
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.1.2
4
+ version: 0.1.3
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: 2010-02-24 00:00:00 -05:00
12
+ date: 2010-02-28 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -82,13 +82,17 @@ files:
82
82
  - examples/helper.rb
83
83
  - examples/slugizer.rb
84
84
  - examples/storage.rb
85
+ - examples/tags.rb
85
86
  - examples/types.rb
86
87
  - examples/update.rb
87
88
  - lib/mongomapper_ext.rb
88
89
  - lib/mongomapper_ext/file.rb
89
90
  - lib/mongomapper_ext/filter.rb
91
+ - lib/mongomapper_ext/js/find_tags.js
92
+ - lib/mongomapper_ext/js/tag_cloud.js
90
93
  - lib/mongomapper_ext/slugizer.rb
91
94
  - lib/mongomapper_ext/storage.rb
95
+ - lib/mongomapper_ext/tags.rb
92
96
  - lib/mongomapper_ext/types/open_struct.rb
93
97
  - lib/mongomapper_ext/types/timestamp.rb
94
98
  - lib/mongomapper_ext/update.rb
@@ -99,6 +103,7 @@ files:
99
103
  - test/test_filter.rb
100
104
  - test/test_slugizer.rb
101
105
  - test/test_storage.rb
106
+ - test/test_tags.rb
102
107
  - test/test_update.rb
103
108
  - test/types/test_open_struct.rb
104
109
  - test/types/test_set.rb
@@ -134,6 +139,7 @@ summary: MongoMapper extensions
134
139
  test_files:
135
140
  - test/test_slugizer.rb
136
141
  - test/test_filter.rb
142
+ - test/test_tags.rb
137
143
  - test/test_storage.rb
138
144
  - test/test_update.rb
139
145
  - test/support/custom_matchers.rb
@@ -145,6 +151,7 @@ test_files:
145
151
  - examples/types.rb
146
152
  - examples/filter.rb
147
153
  - examples/update.rb
154
+ - examples/tags.rb
148
155
  - examples/storage.rb
149
156
  - examples/helper.rb
150
157
  - examples/slugizer.rb