popstar 0.0.2

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/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
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.md ADDED
@@ -0,0 +1,42 @@
1
+ # Popstar
2
+
3
+ Popularity system for your Rails models.
4
+
5
+ [![Build Status](https://secure.travis-ci.org/matteodepalo/popstar.png?branch=master)](http://travis-ci.org/matteodepalo/popstar)
6
+
7
+ ## Installation
8
+
9
+ Put this in your gemfile:
10
+
11
+ `gem 'popstar', :git => 'git://github.com/matteodepalo/popstar.git'`
12
+
13
+ ## Usage
14
+
15
+ Define your badge rules in a PopularityRules class
16
+
17
+ ```
18
+ class PopularityRules
19
+ include Popstar::Popularity
20
+
21
+ def initialize
22
+ popularity_for :voteable do
23
+ on :create, Vote, :rate => proc { |vote| vote.rate*2 }
24
+ on :update, Vote, :rate => proc { |vote| (vote.rate - vote.rate_was)*2 }
25
+ end
26
+
27
+ popularity_for :question do
28
+ on :create, Answer, :rate => 2
29
+ end
30
+
31
+ popularity_for :viewable do
32
+ on :create, ViewsCount
33
+ end
34
+
35
+ popularity_for :widget do
36
+ on :create, Comment
37
+ on :create, Question, :rate => 2
38
+ on :create, Answer, :rate => 3
39
+ end
40
+ end
41
+ end
42
+ ```
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Popstar'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.md')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,6 @@
1
+ class PopularityRules
2
+ include Popstar::Popularity
3
+
4
+ def initialize
5
+ end
6
+ end
@@ -0,0 +1 @@
1
+ PopularityRules.new
@@ -0,0 +1,5 @@
1
+ module Popstar
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Popstar
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ module Popstar
2
+ module Popular
3
+ def self.included(base)
4
+ base.field :popularity, :type => Integer, :default => 0
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Popstar
2
+ module Popularity
3
+ def popularity_for(target, &block)
4
+ RuleGroup.new(target, &block)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,36 @@
1
+ module Popstar
2
+ class RuleGroup
3
+ def initialize(target, &block)
4
+ @target = target
5
+ instance_eval(&block) if block_given?
6
+ end
7
+
8
+ def on(action, model, *args)
9
+ options = args.extract_options!
10
+ options.reverse_merge!(
11
+ :rate => 1
12
+ )
13
+
14
+ rate = begin
15
+ if options[:rate].respond_to?(:call)
16
+ options[:rate]
17
+ else
18
+ proc { options[:rate] }
19
+ end
20
+ end
21
+
22
+ target = @target
23
+
24
+ increase_popularity = proc do |model|
25
+ model.send(target).inc(:popularity, rate.call(model))
26
+ end
27
+
28
+ decrease_popularity = proc do |model|
29
+ model.try(:send, target).try(:inc, :popularity, -rate.call(model))
30
+ end
31
+
32
+ model.send("after_#{action}", increase_popularity)
33
+ model.send("after_destroy", decrease_popularity) if action == :create
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module Popstar
2
+ VERSION = "0.0.2"
3
+ end
data/lib/popstar.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'popstar/engine'
2
+ require 'popstar/popular'
3
+ require 'popstar/popularity'
4
+ require 'popstar/rule_group'
5
+
6
+ module Popstar
7
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :popstar do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: popstar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matteo Depalo
9
+ - Eugenio Depalo
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-07-06 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 3.2.6
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: 3.2.6
31
+ - !ruby/object:Gem::Dependency
32
+ name: mongoid
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: 2.4.0
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 2.4.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: bson_ext
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: rspec-rails
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ - !ruby/object:Gem::Dependency
80
+ name: mongoid-rspec
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ description: Popstar lets you add rules to determine your rails models popularity.
96
+ email:
97
+ - matteodepalo@gmail.com
98
+ executables: []
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - app/models/popularity_rules.rb
103
+ - config/initializers/boot.rb
104
+ - lib/popstar/engine.rb
105
+ - lib/popstar/popular.rb
106
+ - lib/popstar/popularity.rb
107
+ - lib/popstar/rule_group.rb
108
+ - lib/popstar/version.rb
109
+ - lib/popstar.rb
110
+ - lib/tasks/popstar_tasks.rake
111
+ - MIT-LICENSE
112
+ - Rakefile
113
+ - README.md
114
+ homepage: https://github.com/matteodepalo/popstar
115
+ licenses: []
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ segments:
127
+ - 0
128
+ hash: 161942556043864234
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ segments:
136
+ - 0
137
+ hash: 161942556043864234
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 1.8.24
141
+ signing_key:
142
+ specification_version: 3
143
+ summary: Popularity system for your Rails models.
144
+ test_files: []