mongoid-rating 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'mongoid', '>= 2.0.2'
4
+ gem 'rdoc', ">= 2.5.11"
5
+
6
+ group :development do
7
+ gem "bson_ext", ">= 1.3.1"
8
+ gem "rspec-core", '>= 2.6.3'
9
+ gem 'rspec', '>= 2.6.0'
10
+ gem 'database_cleaner', '>= 0.6.7'
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.6.0"
13
+ gem "rcov", ">= 0.9.10"
14
+ end
@@ -0,0 +1,51 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.0.9)
5
+ activesupport (= 3.0.9)
6
+ builder (~> 2.1.2)
7
+ i18n (~> 0.5.0)
8
+ activesupport (3.0.9)
9
+ bson (1.3.1)
10
+ bson_ext (1.3.1)
11
+ builder (2.1.2)
12
+ database_cleaner (0.6.7)
13
+ diff-lcs (1.1.2)
14
+ git (1.2.5)
15
+ i18n (0.5.0)
16
+ jeweler (1.6.4)
17
+ bundler (~> 1.0)
18
+ git (>= 1.2.5)
19
+ rake
20
+ mongo (1.3.1)
21
+ bson (>= 1.3.1)
22
+ mongoid (2.1.7)
23
+ activemodel (~> 3.0)
24
+ mongo (~> 1.3)
25
+ tzinfo (~> 0.3.22)
26
+ rake (0.9.2)
27
+ rcov (0.9.10)
28
+ rdoc (3.9.1)
29
+ rspec (2.6.0)
30
+ rspec-core (~> 2.6.0)
31
+ rspec-expectations (~> 2.6.0)
32
+ rspec-mocks (~> 2.6.0)
33
+ rspec-core (2.6.4)
34
+ rspec-expectations (2.6.0)
35
+ diff-lcs (~> 1.1.2)
36
+ rspec-mocks (2.6.0)
37
+ tzinfo (0.3.29)
38
+
39
+ PLATFORMS
40
+ ruby
41
+
42
+ DEPENDENCIES
43
+ bson_ext (>= 1.3.1)
44
+ bundler (~> 1.0.0)
45
+ database_cleaner (>= 0.6.7)
46
+ jeweler (~> 1.6.0)
47
+ mongoid (>= 2.0.2)
48
+ rcov (>= 0.9.10)
49
+ rdoc (>= 2.5.11)
50
+ rspec (>= 2.6.0)
51
+ rspec-core (>= 2.6.3)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Łukasz Śliwa
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
File without changes
@@ -0,0 +1,35 @@
1
+ = mongoid-rating
2
+
3
+ Rating solution for mongoid.
4
+
5
+ = Usage
6
+
7
+ class User
8
+ include Mongoid::Document
9
+ end
10
+
11
+ class Photo
12
+ include Mongoid::Document
13
+ include Mongoid::Rating
14
+ end
15
+
16
+ Do you want rate a photo as a user? Just type:
17
+
18
+ photo.rate(:by => user, :value => 10).save
19
+
20
+ or
21
+
22
+ photo.rate!(:by => user, :value => 10) # same as above
23
+
24
+ mongoid-rating store information about user and score in :rates field. Some useful methods:
25
+
26
+ photo.rated?(:by => user) # should return true if user rate a photo
27
+ photo.rates_count
28
+ photo.rates_average
29
+ photo.rates_sum
30
+
31
+ == Copyright
32
+
33
+ Copyright (c) 2011 Łukasz Śliwa. See LICENSE.txt for
34
+ further details.
35
+
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "mongoid-rating"
18
+ gem.homepage = "http://github.com/mrplum/mongoid-rating"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Rating solution for mongoid}
21
+ gem.description = %Q{Simple rating solution for mongoid}
22
+ gem.email = "lukasz.sliwa@gmail.com"
23
+ gem.authors = ["Łukasz Śliwa"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'spec'
31
+ test.pattern = 'spec/**/*_spec.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'spec'
38
+ test.pattern = 'spec/**/*_spec.rb'
39
+ test.verbose = true
40
+ test.rcov_opts << '--exclude "gems/*"'
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rdoc/task'
46
+ RDoc::Task.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "mongoid-rating #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.2
@@ -0,0 +1,44 @@
1
+ module Mongoid
2
+ module Rating
3
+
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ field :rates, :type => Hash, :default => {}
8
+ end
9
+
10
+ module InstanceMethods
11
+ def rate(params = {})
12
+ by = params[:by]
13
+ value = params[:value]
14
+ if by.respond_to?(:id)
15
+ rates[by.id.to_s] = value
16
+ end
17
+ self
18
+ end
19
+
20
+ def rate!(params = {})
21
+ rate(params).save
22
+ end
23
+
24
+ def rated?(params = {})
25
+ by = params[:by]
26
+ if by.respond_to?(:id)
27
+ rates[by.id.to_s]
28
+ end
29
+ end
30
+
31
+ def rates_sum
32
+ rates.values.sum
33
+ end
34
+
35
+ def rates_count
36
+ rates.size
37
+ end
38
+
39
+ def rates_average
40
+ rates_sum / rates_count.to_f
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,4 @@
1
+ class Photo
2
+ include Mongoid::Document
3
+ include Mongoid::Rating
4
+ end
@@ -0,0 +1,3 @@
1
+ class User
2
+ include Mongoid::Document
3
+ end
@@ -0,0 +1,48 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Mongoid::Rating do
4
+
5
+ ['Lucas', 'Adam', 'Tom'].each { |name|
6
+ let name.to_s.downcase do
7
+ User.create(:name => name)
8
+ end
9
+ }
10
+
11
+ let :photo do
12
+ Photo.create
13
+ end
14
+
15
+ it "should include :rates field" do
16
+ Photo.fields['rates'].should_not be_nil
17
+ photo.rates.empty?.should be_true
18
+ end
19
+
20
+ it "should rate" do
21
+ photo.rates[lucas.id.to_s].should be_nil
22
+ photo.rate(:by => lucas, :value => 10)
23
+ photo.rates[lucas.id.to_s].should == 10
24
+ photo.changes.should include('rates')
25
+ end
26
+
27
+ it "should rate!" do
28
+ photo.rates[lucas.id.to_s].should be_nil
29
+ photo.rate!(:by => lucas, :value => 5).should be_true
30
+ photo.rates[lucas.id.to_s].should == 5
31
+ end
32
+
33
+ it "should check rated?" do
34
+ photo.rated?(:by => adam).should be_false
35
+ photo.rate!(:by => adam, :value => 1).should be_true
36
+ photo.rated?(:by => adam).should be_true
37
+ end
38
+
39
+ it "should check rates_* methods" do
40
+ { adam => 1, lucas => 5, tom => 7}.map { |by, value|
41
+ photo.rate(:by => by, :value => value)
42
+ }
43
+ photo.rates_count.should == 3
44
+ photo.rates_sum.should == 13
45
+ photo.rates_average.should == (1+5+7)/3.0
46
+ end
47
+
48
+ end
@@ -0,0 +1,31 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'rubygems'
5
+ require "mongoid"
6
+ require "mongoid-rating"
7
+ require "rspec"
8
+ require "database_cleaner"
9
+
10
+ MODELS = File.join(File.dirname(__FILE__), "models")
11
+
12
+ Dir["#{File.dirname(__FILE__)}/models/*.rb"].each { |f| require f }
13
+
14
+ Mongoid.config.master = Mongo::Connection.new.db("mongoid-rating-spec")
15
+ Mongoid.logger = Logger.new($stdout)
16
+
17
+ DatabaseCleaner.orm = "mongoid"
18
+
19
+ RSpec.configure do |config|
20
+ config.before(:all) do
21
+ DatabaseCleaner.strategy = :truncation
22
+ end
23
+
24
+ config.before(:each) do
25
+ DatabaseCleaner.start
26
+ end
27
+
28
+ config.after(:each) do
29
+ DatabaseCleaner.clean
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,213 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-rating
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 2
9
+ version: 0.1.2
10
+ platform: ruby
11
+ authors:
12
+ - "\xC5\x81ukasz \xC5\x9Aliwa"
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-08-09 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: mongoid
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 0
30
+ - 2
31
+ version: 2.0.2
32
+ type: :runtime
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rdoc
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 2
44
+ - 5
45
+ - 11
46
+ version: 2.5.11
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: bson_ext
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 1
59
+ - 3
60
+ - 1
61
+ version: 1.3.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: rspec-core
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 2
74
+ - 6
75
+ - 3
76
+ version: 2.6.3
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *id004
80
+ - !ruby/object:Gem::Dependency
81
+ name: rspec
82
+ requirement: &id005 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ segments:
88
+ - 2
89
+ - 6
90
+ - 0
91
+ version: 2.6.0
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: *id005
95
+ - !ruby/object:Gem::Dependency
96
+ name: database_cleaner
97
+ requirement: &id006 !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ segments:
103
+ - 0
104
+ - 6
105
+ - 7
106
+ version: 0.6.7
107
+ type: :development
108
+ prerelease: false
109
+ version_requirements: *id006
110
+ - !ruby/object:Gem::Dependency
111
+ name: bundler
112
+ requirement: &id007 !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ segments:
118
+ - 1
119
+ - 0
120
+ - 0
121
+ version: 1.0.0
122
+ type: :development
123
+ prerelease: false
124
+ version_requirements: *id007
125
+ - !ruby/object:Gem::Dependency
126
+ name: jeweler
127
+ requirement: &id008 !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ~>
131
+ - !ruby/object:Gem::Version
132
+ segments:
133
+ - 1
134
+ - 6
135
+ - 0
136
+ version: 1.6.0
137
+ type: :development
138
+ prerelease: false
139
+ version_requirements: *id008
140
+ - !ruby/object:Gem::Dependency
141
+ name: rcov
142
+ requirement: &id009 !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ segments:
148
+ - 0
149
+ - 9
150
+ - 10
151
+ version: 0.9.10
152
+ type: :development
153
+ prerelease: false
154
+ version_requirements: *id009
155
+ description: Simple rating solution for mongoid
156
+ email: lukasz.sliwa@gmail.com
157
+ executables: []
158
+
159
+ extensions: []
160
+
161
+ extra_rdoc_files:
162
+ - LICENSE.txt
163
+ - README
164
+ - README.rdoc
165
+ files:
166
+ - .document
167
+ - Gemfile
168
+ - Gemfile.lock
169
+ - LICENSE.txt
170
+ - README
171
+ - README.rdoc
172
+ - Rakefile
173
+ - VERSION
174
+ - lib/mongoid-rating.rb
175
+ - spec/models/photo.rb
176
+ - spec/models/user.rb
177
+ - spec/mongoid-rating_spec.rb
178
+ - spec/spec_helper.rb
179
+ has_rdoc: true
180
+ homepage: http://github.com/mrplum/mongoid-rating
181
+ licenses:
182
+ - MIT
183
+ post_install_message:
184
+ rdoc_options: []
185
+
186
+ require_paths:
187
+ - lib
188
+ required_ruby_version: !ruby/object:Gem::Requirement
189
+ none: false
190
+ requirements:
191
+ - - ">="
192
+ - !ruby/object:Gem::Version
193
+ hash: 1835390545794837523
194
+ segments:
195
+ - 0
196
+ version: "0"
197
+ required_rubygems_version: !ruby/object:Gem::Requirement
198
+ none: false
199
+ requirements:
200
+ - - ">="
201
+ - !ruby/object:Gem::Version
202
+ segments:
203
+ - 0
204
+ version: "0"
205
+ requirements: []
206
+
207
+ rubyforge_project:
208
+ rubygems_version: 1.3.7
209
+ signing_key:
210
+ specification_version: 3
211
+ summary: Rating solution for mongoid
212
+ test_files: []
213
+