simple_caches 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in better_counter_caches.gemspec
4
+ gemspec
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ simple_caches (0.0.1)
5
+ activerecord (~> 3.0.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ activemodel (3.0.1)
11
+ activesupport (= 3.0.1)
12
+ builder (~> 2.1.2)
13
+ i18n (~> 0.4.1)
14
+ activerecord (3.0.1)
15
+ activemodel (= 3.0.1)
16
+ activesupport (= 3.0.1)
17
+ arel (~> 1.0.0)
18
+ tzinfo (~> 0.3.23)
19
+ activesupport (3.0.1)
20
+ arel (1.0.1)
21
+ activesupport (~> 3.0.0)
22
+ builder (2.1.2)
23
+ i18n (0.4.2)
24
+ shoulda (2.11.3)
25
+ sqlite3-ruby (1.3.2)
26
+ tzinfo (0.3.23)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ activerecord (~> 3.0.0)
33
+ shoulda (~> 2.11.3)
34
+ simple_caches!
35
+ sqlite3-ruby (~> 1.3.0)
@@ -0,0 +1,3 @@
1
+ # simple_caches
2
+
3
+ Documentation coming soon...
@@ -0,0 +1,9 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new(:test) do |test|
6
+ test.libs << 'lib' << 'test'
7
+ test.pattern = 'test/**/test_*.rb'
8
+ test.verbose = true
9
+ end
@@ -0,0 +1,4 @@
1
+ require 'active_record'
2
+ require 'simple_caches/simple_cache'
3
+
4
+ ActiveRecord::Base.send(:include, SimpleCaches)
@@ -0,0 +1,17 @@
1
+ module SimpleCaches
2
+ module ClassMethods
3
+ def simple_cache(target, source)
4
+ define_method("cached_value_for_#{target}_#{source}") do
5
+ self.send("#{target}=", self.send(source))
6
+ end
7
+
8
+ before_save do
9
+ self.send("cached_value_for_#{target}_#{source}")
10
+ end
11
+ end
12
+ end
13
+
14
+ def self.included(receiver)
15
+ receiver.extend ClassMethods
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleCaches
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "simple_caches/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "simple_caches"
7
+ s.version = SimpleCaches::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["David Padilla"]
10
+ s.email = ["david@crowdint.com"]
11
+ s.homepage = "http://rubygems.org/gems/simple_caches"
12
+ s.summary = %q{Generate simple counter caches}
13
+ s.description = %q{Generate simple counter caches, just specify where to store them and where to get them}
14
+
15
+ s.add_dependency('activerecord', '~>3.0.0')
16
+ s.add_development_dependency('shoulda', '~>2.11.3')
17
+ s.add_development_dependency('sqlite3-ruby', '~>1.3.0')
18
+ s.rubyforge_project = "better_counter_caches"
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.require_paths = ["lib"]
24
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'bundler/setup'
4
+ require 'shoulda'
5
+ require 'simple_caches'
6
+
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
@@ -0,0 +1,52 @@
1
+ class Apple < ActiveRecord::Base
2
+ simple_cache :cached_value, :some_method
3
+
4
+ def some_method
5
+ self.variable_value
6
+ end
7
+ end
8
+
9
+ class TestSimpleCache < Test::Unit::TestCase
10
+ def setup
11
+ ::ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
12
+ ::ActiveRecord::Schema.define(:version => 1) do
13
+ create_table :apples do |t|
14
+ t.column :variable_value, :integer
15
+ t.column :cached_value, :integer
16
+ t.column :name, :string
17
+ end
18
+ end
19
+ end
20
+
21
+ context "after creating an object" do
22
+ setup do
23
+ @fruit = Apple.create(:variable_value => 200)
24
+ end
25
+
26
+ should "update the specified field with the return value of the method" do
27
+ assert_equal(200, @fruit.cached_value)
28
+ end
29
+
30
+ context "after manually updating the object" do
31
+ setup do
32
+ @fruit.variable_value = 1
33
+ @fruit.save!
34
+ end
35
+
36
+ should "update the value too" do
37
+ assert_equal(1, @fruit.cached_value)
38
+ end
39
+ end
40
+
41
+ context "after using update_attribute on some other field" do
42
+ setup do
43
+ @fruit.variable_value = 300
44
+ @fruit.update_attribute(:name, 'With something else')
45
+ end
46
+
47
+ should "update the value too" do
48
+ assert_equal(300, @fruit.cached_value)
49
+ end
50
+ end
51
+ end
52
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_caches
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - David Padilla
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-05 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activerecord
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 0
34
+ version: 3.0.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: shoulda
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 37
46
+ segments:
47
+ - 2
48
+ - 11
49
+ - 3
50
+ version: 2.11.3
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: sqlite3-ruby
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ hash: 27
62
+ segments:
63
+ - 1
64
+ - 3
65
+ - 0
66
+ version: 1.3.0
67
+ type: :development
68
+ version_requirements: *id003
69
+ description: Generate simple counter caches, just specify where to store them and where to get them
70
+ email:
71
+ - david@crowdint.com
72
+ executables: []
73
+
74
+ extensions: []
75
+
76
+ extra_rdoc_files: []
77
+
78
+ files:
79
+ - .gitignore
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - README.markdown
83
+ - Rakefile
84
+ - lib/simple_caches.rb
85
+ - lib/simple_caches/simple_cache.rb
86
+ - lib/simple_caches/version.rb
87
+ - simple_caches.gemspec
88
+ - test/test_helper.rb
89
+ - test/unit/test_simple_cache.rb
90
+ has_rdoc: true
91
+ homepage: http://rubygems.org/gems/simple_caches
92
+ licenses: []
93
+
94
+ post_install_message:
95
+ rdoc_options: []
96
+
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 3
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ hash: 3
114
+ segments:
115
+ - 0
116
+ version: "0"
117
+ requirements: []
118
+
119
+ rubyforge_project: better_counter_caches
120
+ rubygems_version: 1.3.7
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: Generate simple counter caches
124
+ test_files:
125
+ - test/test_helper.rb
126
+ - test/unit/test_simple_cache.rb