cache_counters 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/lib/cache_counters.rb +2 -0
- data/lib/cache_counters/acts_as_cache.rb +17 -0
- data/lib/cache_counters/class_methods.rb +33 -0
- data/rails_generators/cache_counters/cache_counters_generator.rb +35 -0
- data/rails_generators/cache_counters/templates/counter.rb +3 -0
- data/rails_generators/cache_counters/templates/migration.rb +14 -0
- data/spec/cache_counters_spec.rb +43 -0
- data/spec/debug.log +81 -0
- data/spec/schema.rb +8 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +32 -0
- metadata +81 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Brian Bommarito
|
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.rdoc
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= cache_counters
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 Brian Bommarito. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "cache_counters"
|
8
|
+
gem.summary = %Q{Counter caches for general counters}
|
9
|
+
gem.description = %Q{Creates a generic counter cache for those counter caches that do not fit anywere else}
|
10
|
+
gem.email = "bommarito.brian.matthew@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/bommaritobrianmatthew/cache_counters"
|
12
|
+
gem.authors = ["Brian Bommarito"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
gem.files = FileList["[A-Z]*", "{bin,rails_generators,lib,spec}/**/*", 'lib/jeweler/templates/.gitignore']
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
end
|
33
|
+
|
34
|
+
task :spec => :check_dependencies
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
require 'rake/rdoctask'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
41
|
+
|
42
|
+
rdoc.rdoc_dir = 'rdoc'
|
43
|
+
rdoc.title = "cache_counters #{version}"
|
44
|
+
rdoc.rdoc_files.include('README*')
|
45
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
46
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Acts
|
3
|
+
module CounterCaches
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def acts_as_counter
|
10
|
+
class_eval do
|
11
|
+
extend CacheCountersClassMethods
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Acts
|
3
|
+
module CounterCaches
|
4
|
+
module ClassMethods
|
5
|
+
module CacheCountersClassMethods
|
6
|
+
def [](key)
|
7
|
+
find_or_create_by_key(key.to_s).value
|
8
|
+
end
|
9
|
+
|
10
|
+
protected
|
11
|
+
|
12
|
+
def method_missing(method, *args, &block)
|
13
|
+
if method.to_s =~ /^increment_(.*)$/
|
14
|
+
record = self.find_or_create_by_key($1.to_s)
|
15
|
+
record.increment!(:value)
|
16
|
+
elsif method.to_s =~ /^decrement_(.*)$/
|
17
|
+
record = self.find_or_create_by_key($1.to_s)
|
18
|
+
record.decrement!(:value)
|
19
|
+
elsif method.to_s =~ /^reset_(.*)$/
|
20
|
+
record = self.find_or_create_by_key($1.to_s)
|
21
|
+
record.update_attribute(:value, 0)
|
22
|
+
elsif method.to_s =~ /^destroy_(.*)$/
|
23
|
+
record = self.find_or_create_by_key($1.to_s)
|
24
|
+
record.destroy
|
25
|
+
else
|
26
|
+
super
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class CacheCountersGenerator < Rails::Generator::Base
|
2
|
+
attr_accessor :model_name
|
3
|
+
|
4
|
+
def initialize(runtime_args, runtime_options = {})
|
5
|
+
super
|
6
|
+
|
7
|
+
@model_name = @args[0] || 'counter'
|
8
|
+
end
|
9
|
+
|
10
|
+
def manifest
|
11
|
+
record do |m|
|
12
|
+
m.directory "app/models"
|
13
|
+
|
14
|
+
m.template "counter.rb", "app/models/#{model_singular_name}.rb"
|
15
|
+
|
16
|
+
m.migration_template "migration.rb", "db/migrate", :migration_file_name => "create_#{model_plural_name}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def model_singular_name
|
21
|
+
model_name.underscore
|
22
|
+
end
|
23
|
+
|
24
|
+
def model_plural_name
|
25
|
+
model_singular_name.pluralize
|
26
|
+
end
|
27
|
+
|
28
|
+
def model_class_name
|
29
|
+
model_name.camelize
|
30
|
+
end
|
31
|
+
|
32
|
+
def model_plural_class_name
|
33
|
+
model_plural_name.camelize
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class Create<%= model_plural_class_name %> < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :<%= model_plural_name %> do |t|
|
4
|
+
t.string :key, :null => false
|
5
|
+
t.integer :value, :null => false, :default => true
|
6
|
+
end
|
7
|
+
add_index(:<%= model_plural_name %>, :key)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.down
|
11
|
+
remove_index(:<%= model_plural_name %>, :key)
|
12
|
+
drop_table :<%= model_plural_name %>
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "CacheCounters" do
|
4
|
+
before(:each) do
|
5
|
+
Counter.delete_all
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should find an existing counter" do
|
9
|
+
Counter.create(:key => "count")
|
10
|
+
Counter[:count].should == 0
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should create a non-existent key" do
|
14
|
+
Counter[:missing].should == 0
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should increment a counter by 1" do
|
18
|
+
Counter[:count].should == 0
|
19
|
+
Counter.increment_count
|
20
|
+
Counter[:count].should == 1
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should decrement a counter by 1" do
|
24
|
+
Counter.increment_count
|
25
|
+
Counter[:count].should == 1
|
26
|
+
Counter.decrement_count
|
27
|
+
Counter[:count].should == 0
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should reset the counter to 0" do
|
31
|
+
Counter.increment_count
|
32
|
+
Counter[:count].should == 1
|
33
|
+
Counter.reset_count
|
34
|
+
Counter[:count].should == 0
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should delete the counter" do
|
38
|
+
Counter[:count].should == 0
|
39
|
+
Counter.destroy_count
|
40
|
+
Counter.find_by_key("count").should be_nil
|
41
|
+
Counter.increment_count
|
42
|
+
end
|
43
|
+
end
|
data/spec/debug.log
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# Logfile created on Sat Jan 23 10:59:45 -0500 2010 by logger.rb
|
2
|
+
** acts_as_counter: initialized properly.
|
3
|
+
** acts_as_counter: initialized properly.
|
4
|
+
** acts_as_counter: initialized properly.
|
5
|
+
** acts_as_counter: initialized properly.
|
6
|
+
** acts_as_counter: initialized properly.
|
7
|
+
** acts_as_counter: initialized properly.
|
8
|
+
** acts_as_counter: initialized properly.
|
9
|
+
** acts_as_counter: initialized properly.
|
10
|
+
** acts_as_counter: initialized properly.
|
11
|
+
** acts_as_counter: initialized properly.
|
12
|
+
** acts_as_counter: initialized properly.
|
13
|
+
** acts_as_counter: initialized properly.
|
14
|
+
** acts_as_counter: initialized properly.
|
15
|
+
** acts_as_counter: initialized properly.
|
16
|
+
** acts_as_counter: initialized properly.
|
17
|
+
** acts_as_counter: initialized properly.
|
18
|
+
** acts_as_counter: initialized properly.
|
19
|
+
** acts_as_counter: initialized properly.
|
20
|
+
** acts_as_counter: initialized properly.
|
21
|
+
** acts_as_counter: initialized properly.
|
22
|
+
** acts_as_counter: initialized properly.
|
23
|
+
** acts_as_counter: initialized properly.
|
24
|
+
** acts_as_counter: initialized properly.
|
25
|
+
** acts_as_counter: initialized properly.
|
26
|
+
** acts_as_counter: initialized properly.
|
27
|
+
** acts_as_counter: initialized properly.
|
28
|
+
** acts_as_counter: initialized properly.
|
29
|
+
** acts_as_counter: initialized properly.
|
30
|
+
** acts_as_counter: initialized properly.
|
31
|
+
** acts_as_counter: initialized properly.
|
32
|
+
** acts_as_counter: initialized properly.
|
33
|
+
** acts_as_counter: initialized properly.
|
34
|
+
** acts_as_counter: initialized properly.
|
35
|
+
** acts_as_counter: initialized properly.
|
36
|
+
** acts_as_counter: initialized properly.
|
37
|
+
** acts_as_counter: initialized properly.
|
38
|
+
** acts_as_counter: initialized properly.
|
39
|
+
** acts_as_counter: initialized properly.
|
40
|
+
** acts_as_counter: initialized properly.
|
41
|
+
** acts_as_counter: initialized properly.
|
42
|
+
** acts_as_counter: initialized properly.
|
43
|
+
** acts_as_counter: initialized properly.
|
44
|
+
** acts_as_counter: initialized properly.
|
45
|
+
** acts_as_counter: initialized properly.
|
46
|
+
** acts_as_counter: initialized properly.
|
47
|
+
** acts_as_counter: initialized properly.
|
48
|
+
** acts_as_counter: initialized properly.
|
49
|
+
** acts_as_counter: initialized properly.
|
50
|
+
** acts_as_counter: initialized properly.
|
51
|
+
** acts_as_counter: initialized properly.
|
52
|
+
** acts_as_counter: initialized properly.
|
53
|
+
** acts_as_counter: initialized properly.
|
54
|
+
** acts_as_counter: initialized properly.
|
55
|
+
** acts_as_counter: initialized properly.
|
56
|
+
** acts_as_counter: initialized properly.
|
57
|
+
** acts_as_counter: initialized properly.
|
58
|
+
** acts_as_counter: initialized properly.
|
59
|
+
** acts_as_counter: initialized properly.
|
60
|
+
** acts_as_counter: initialized properly.
|
61
|
+
** acts_as_counter: initialized properly.
|
62
|
+
** acts_as_counter: initialized properly.
|
63
|
+
** acts_as_counter: initialized properly.
|
64
|
+
** acts_as_counter: initialized properly.
|
65
|
+
** acts_as_counter: initialized properly.
|
66
|
+
** acts_as_counter: initialized properly.
|
67
|
+
** acts_as_counter: initialized properly.
|
68
|
+
** acts_as_counter: initialized properly.
|
69
|
+
** acts_as_counter: initialized properly.
|
70
|
+
** acts_as_counter: initialized properly.
|
71
|
+
** acts_as_counter: initialized properly.
|
72
|
+
** acts_as_counter: initialized properly.
|
73
|
+
** acts_as_counter: initialized properly.
|
74
|
+
** acts_as_counter: initialized properly.
|
75
|
+
** acts_as_counter: initialized properly.
|
76
|
+
** acts_as_counter: initialized properly.
|
77
|
+
** acts_as_counter: initialized properly.
|
78
|
+
** acts_as_counter: initialized properly.
|
79
|
+
** acts_as_counter: initialized properly.
|
80
|
+
** acts_as_counter: initialized properly.
|
81
|
+
** acts_as_counter: initialized properly.
|
data/spec/schema.rb
ADDED
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'rubygems'
|
4
|
+
require 'active_record'
|
5
|
+
require 'cache_counters'
|
6
|
+
require 'spec'
|
7
|
+
require 'spec/autorun'
|
8
|
+
|
9
|
+
TEST_DATABASE_FILE = File.join(File.dirname(__FILE__), '..', 'test.sqlite3')
|
10
|
+
|
11
|
+
File.unlink(TEST_DATABASE_FILE) if File.exist?(TEST_DATABASE_FILE)
|
12
|
+
ActiveRecord::Base.establish_connection(
|
13
|
+
"adapter" => "sqlite3", "database" => TEST_DATABASE_FILE
|
14
|
+
)
|
15
|
+
|
16
|
+
RAILS_DEFAULT_LOGGER = Logger.new(File.join(File.dirname(__FILE__), "debug.log"))
|
17
|
+
|
18
|
+
ActiveRecord::Base.silence do
|
19
|
+
ActiveRecord::Migration.verbose = false
|
20
|
+
load(File.dirname(__FILE__) + '/schema.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
$: << File.join(File.dirname(__FILE__), '..', 'lib')
|
24
|
+
require File.join(File.dirname(__FILE__), '..', 'init')
|
25
|
+
|
26
|
+
class Counter < ActiveRecord::Base
|
27
|
+
acts_as_counter
|
28
|
+
end
|
29
|
+
|
30
|
+
Spec::Runner.configure do |config|
|
31
|
+
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cache_counters
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brian Bommarito
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-23 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.2.9
|
24
|
+
version:
|
25
|
+
description: Creates a generic counter cache for those counter caches that do not fit anywere else
|
26
|
+
email: bommarito.brian.matthew@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- LICENSE
|
36
|
+
- README.rdoc
|
37
|
+
- Rakefile
|
38
|
+
- VERSION
|
39
|
+
- lib/cache_counters.rb
|
40
|
+
- lib/cache_counters/acts_as_cache.rb
|
41
|
+
- lib/cache_counters/class_methods.rb
|
42
|
+
- rails_generators/cache_counters/cache_counters_generator.rb
|
43
|
+
- rails_generators/cache_counters/templates/counter.rb
|
44
|
+
- rails_generators/cache_counters/templates/migration.rb
|
45
|
+
- spec/cache_counters_spec.rb
|
46
|
+
- spec/debug.log
|
47
|
+
- spec/schema.rb
|
48
|
+
- spec/spec.opts
|
49
|
+
- spec/spec_helper.rb
|
50
|
+
has_rdoc: true
|
51
|
+
homepage: http://github.com/bommaritobrianmatthew/cache_counters
|
52
|
+
licenses: []
|
53
|
+
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options:
|
56
|
+
- --charset=UTF-8
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "0"
|
70
|
+
version:
|
71
|
+
requirements: []
|
72
|
+
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.3.5
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: Counter caches for general counters
|
78
|
+
test_files:
|
79
|
+
- spec/schema.rb
|
80
|
+
- spec/cache_counters_spec.rb
|
81
|
+
- spec/spec_helper.rb
|