comboy-cached_record 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/tasks/spec.rake ADDED
@@ -0,0 +1,55 @@
1
+ # $Id$
2
+
3
+ if HAVE_SPEC_RAKE_SPECTASK
4
+ require 'spec/rake/verify_rcov'
5
+
6
+ namespace :spec do
7
+
8
+ desc 'Run all specs with basic output'
9
+ Spec::Rake::SpecTask.new(:run) do |t|
10
+ t.ruby_opts = PROJ.ruby_opts
11
+ t.spec_opts = PROJ.spec.opts
12
+ t.spec_files = PROJ.spec.files
13
+ t.libs += PROJ.libs
14
+ end
15
+
16
+ desc 'Run all specs with text output'
17
+ Spec::Rake::SpecTask.new(:specdoc) do |t|
18
+ t.ruby_opts = PROJ.ruby_opts
19
+ t.spec_opts = PROJ.spec.opts + ['--format', 'specdoc']
20
+ t.spec_files = PROJ.spec.files
21
+ t.libs += PROJ.libs
22
+ end
23
+
24
+ if HAVE_RCOV
25
+ desc 'Run all specs with RCov'
26
+ Spec::Rake::SpecTask.new(:rcov) do |t|
27
+ t.ruby_opts = PROJ.ruby_opts
28
+ t.spec_opts = PROJ.spec.opts
29
+ t.spec_files = PROJ.spec.files
30
+ t.libs += PROJ.libs
31
+ t.rcov = true
32
+ t.rcov_dir = PROJ.rcov.dir
33
+ t.rcov_opts = PROJ.rcov.opts + ['--exclude', 'spec']
34
+ end
35
+
36
+ RCov::VerifyTask.new(:verify) do |t|
37
+ t.threshold = PROJ.rcov.threshold
38
+ t.index_html = File.join(PROJ.rcov.dir, 'index.html')
39
+ t.require_exact_threshold = PROJ.rcov.threshold_exact
40
+ end
41
+
42
+ task :verify => :rcov
43
+ remove_desc_for_task %w(spec:clobber_rcov)
44
+ end
45
+
46
+ end # namespace :spec
47
+
48
+ desc 'Alias to spec:run'
49
+ task :spec => 'spec:run'
50
+
51
+ task :clobber => 'spec:clobber_rcov' if HAVE_RCOV
52
+
53
+ end # if HAVE_SPEC_RAKE_SPECTASK
54
+
55
+ # EOF
data/tasks/svn.rake ADDED
@@ -0,0 +1,48 @@
1
+ # $Id$
2
+
3
+ if HAVE_SVN
4
+
5
+ unless PROJ.svn.root
6
+ info = %x/svn info ./
7
+ m = %r/^Repository Root:\s+(.*)$/.match(info)
8
+ PROJ.svn.root = (m.nil? ? '' : m[1])
9
+ end
10
+ PROJ.svn.root = File.join(PROJ.svn.root, PROJ.svn.path) unless PROJ.svn.path.empty?
11
+
12
+ namespace :svn do
13
+
14
+ # A prerequisites task that all other tasks depend upon
15
+ task :prereqs
16
+
17
+ desc 'Show tags from the SVN repository'
18
+ task :show_tags => 'svn:prereqs' do |t|
19
+ tags = %x/svn list #{File.join(PROJ.svn.root, PROJ.svn.tags)}/
20
+ tags.gsub!(%r/\/$/, '')
21
+ tags = tags.split("\n").sort {|a,b| b <=> a}
22
+ puts tags
23
+ end
24
+
25
+ desc 'Create a new tag in the SVN repository'
26
+ task :create_tag => 'svn:prereqs' do |t|
27
+ v = ENV['VERSION'] or abort 'Must supply VERSION=x.y.z'
28
+ abort "Versions don't match #{v} vs #{PROJ.version}" if v != PROJ.version
29
+
30
+ svn = PROJ.svn
31
+ trunk = File.join(svn.root, svn.trunk)
32
+ tag = "%s-%s" % [PROJ.name, PROJ.version]
33
+ tag = File.join(svn.root, svn.tags, tag)
34
+ msg = "Creating tag for #{PROJ.name} version #{PROJ.version}"
35
+
36
+ puts "Creating SVN tag '#{tag}'"
37
+ unless system "svn cp -m '#{msg}' #{trunk} #{tag}"
38
+ abort "Tag creation failed"
39
+ end
40
+ end
41
+
42
+ end # namespace :svn
43
+
44
+ task 'gem:release' => 'svn:create_tag'
45
+
46
+ end # if PROJ.svn.path
47
+
48
+ # EOF
data/tasks/test.rake ADDED
@@ -0,0 +1,38 @@
1
+ # $Id$
2
+
3
+ require 'rake/testtask'
4
+
5
+ namespace :test do
6
+
7
+ Rake::TestTask.new(:run) do |t|
8
+ t.libs = PROJ.libs
9
+ t.test_files = if test(?f, PROJ.test.file) then [PROJ.test.file]
10
+ else PROJ.test.files end
11
+ t.ruby_opts += PROJ.ruby_opts
12
+ t.ruby_opts += PROJ.test.opts
13
+ end
14
+
15
+ if HAVE_RCOV
16
+ desc 'Run rcov on the unit tests'
17
+ task :rcov => :clobber_rcov do
18
+ opts = PROJ.rcov.opts.dup << '-o' << PROJ.rcov.dir
19
+ opts = opts.join(' ')
20
+ files = if test(?f, PROJ.test.file) then [PROJ.test.file]
21
+ else PROJ.test.files end
22
+ files = files.join(' ')
23
+ sh "#{RCOV} #{files} #{opts}"
24
+ end
25
+
26
+ task :clobber_rcov do
27
+ rm_r 'coverage' rescue nil
28
+ end
29
+ end
30
+
31
+ end # namespace :test
32
+
33
+ desc 'Alias to test:run'
34
+ task :test => 'test:run'
35
+
36
+ task :clobber => 'test:clobber_rcov' if HAVE_RCOV
37
+
38
+ # EOF
@@ -0,0 +1,7 @@
1
+ #
2
+ # To change this template, choose Tools | Templates
3
+ # and open the template in the editor.
4
+
5
+
6
+ puts "Hello World case
7
+ "
@@ -0,0 +1,84 @@
1
+ require 'lib/cached_record'
2
+ require 'pp'
3
+
4
+
5
+ module CachedRecord
6
+
7
+ class Base
8
+ def self.find_by_sql(sql)
9
+ # Not so nice solution, just like global variable,
10
+ # but that's just testing that will always run on one thread
11
+ ::CachedRecord::TestEnvironment.last_query_time = Time.now
12
+ super
13
+ end
14
+ end
15
+
16
+ class TestEnvironment
17
+
18
+ #class << self
19
+ cattr_accessor :last_query_time
20
+
21
+
22
+ def self.db_file
23
+ "test/tmp/test.db"
24
+ end
25
+
26
+ def self.establish_connection
27
+ ActiveRecord::Base.configurations = {
28
+ 'test_db' => {
29
+ :adapter => 'sqlite3',
30
+ :database => db_file,
31
+ :timeout => 5000
32
+ },
33
+ 'empty' => {
34
+ :adapter => 'sqlite3',
35
+ :database => ':memory:',
36
+ :timeout => 5000
37
+ }
38
+ }
39
+ ActiveRecord::Base.establish_connection 'test_db'
40
+ end
41
+
42
+ def self.setup_db
43
+ Dir.glob('test/migrations/*.rb').each { |f| require f }
44
+ File.unlink db_file if File.exists? db_file
45
+ setup
46
+ CreateUsers.up
47
+ load_fixture :users
48
+ end
49
+
50
+ def self.load_fixture(name)
51
+ klass = name.to_s.classify.constantize
52
+ fixture = YAML.load_file("test/fixtures/#{name}.yml")
53
+ fixture.each do |row|
54
+ klass.create row
55
+ end
56
+ end
57
+
58
+ def self.setup
59
+ ::CachedRecord.cache_store = MemCache.new :namespace => (rand(9999999).to_s(36)+Time.now.to_i.to_s)
60
+ ::CachedRecord.cache_store.servers = 'localhost:11211'
61
+ end
62
+
63
+ def self.prepare
64
+ establish_connection
65
+ Dir.glob('test/models/*.rb').each { |f| require f }
66
+ end
67
+
68
+ end
69
+
70
+ module TestHelper
71
+ def without_db
72
+ ActiveRecord::Base.establish_connection nil
73
+ yield
74
+ establish_connection
75
+ end
76
+
77
+ def assert_no_query
78
+ ::CachedRecord::TestEnvironment.last_query_time = nil
79
+ yield
80
+ assert !::CachedRecord::TestEnvironment.last_query_time, "Database called when cache should be used"
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,10 @@
1
+ ---
2
+ - :name: Jonh
3
+ :age: 32
4
+ :is_active: false
5
+ - :name: Bob
6
+ :age: 5
7
+ :is_active: true
8
+ - :name: Knuth
9
+ :age: 57
10
+ :is_active: true
@@ -0,0 +1,14 @@
1
+ class CreateNotes < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :notes do |t|
4
+ t.text :body
5
+ t.string :titile
6
+ t.integer :user_id
7
+ t.timestamps
8
+ end
9
+ end
10
+
11
+ def self.down
12
+ drop_table :notes
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :users do |t|
4
+ t.string :name, :login
5
+ t.integer :age
6
+ t.boolean :is_active
7
+ t.timestamps
8
+ end
9
+ end
10
+
11
+ def self.down
12
+ drop_table :users
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ class Note < CachedRecord::Base
2
+ belongs_to :user
3
+ end
@@ -0,0 +1,8 @@
1
+ # Used for directy accessing database
2
+
3
+ class OriginalNote < ActiveRecord::Base
4
+ self.table_name = 'notes'
5
+
6
+ belongs_to :user
7
+
8
+ end
@@ -0,0 +1,5 @@
1
+ # Used for directly accessing database
2
+
3
+ class OriginalUser < ActiveRecord::Base
4
+ self.table_name = 'users'
5
+ end
@@ -0,0 +1,3 @@
1
+ class User < CachedRecord::Base
2
+
3
+ end
@@ -0,0 +1,58 @@
1
+ require 'test/environment'
2
+ require 'test/unit'
3
+
4
+
5
+ CachedRecord::TestEnvironment.prepare
6
+
7
+ class CachedRecordTest < Test::Unit::TestCase
8
+
9
+ include CachedRecord::TestHelper
10
+
11
+ def setup
12
+ CachedRecord::TestEnvironment.setup
13
+ end
14
+
15
+ def test_base
16
+ assert u1 = User.find_by_id(1)
17
+ assert u2 = OriginalUser.find_by_id(1)
18
+ assert u1.name
19
+ assert_equal u1.name,u2.name
20
+ end
21
+
22
+ def test_caching_for_Find_by_id
23
+ assert user = User.find(1), "could not find user "
24
+ assert_no_query do
25
+ assert user2 = User.find(1), "could not find user second time"
26
+ assert_equal user, user2, "cached user differs from original"
27
+ end
28
+ end
29
+
30
+ def test_invalidating_on_save
31
+ user = User.find(1)
32
+ name = rand(10**8).to_s(36)
33
+ user.name = name
34
+ user.save
35
+ u2 = User.find(1)
36
+ assert_equal name, u2.name
37
+ u3 = OriginalUser.find(1)
38
+ assert_equal name, u3.name
39
+ end
40
+
41
+ def test_invalidating_on_update_attributes
42
+ user = User.find(1)
43
+ name = rand(10**8).to_s(36)
44
+ user.update_attributes :name => name
45
+ u2 = User.find(1)
46
+ assert_equal name, u2.name
47
+ end
48
+
49
+ def test_invalidating_on_update_attribute
50
+ user = User.find(1)
51
+ name = rand(10**8).to_s(36)
52
+ user.update_attribute(:name,name)
53
+ u2 = User.find(1)
54
+ assert_equal name, u2.name
55
+ end
56
+
57
+ end
58
+
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: comboy-cached_record
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - "Kacper Cie\xC5\x9Bla"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-27 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: kacper.ciesla@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.txt
24
+ files:
25
+ - tasks
26
+ - tasks/bones.rake
27
+ - tasks/spec.rake
28
+ - tasks/notes.rake
29
+ - tasks/rubyforge.rake
30
+ - tasks/test.rake
31
+ - tasks/git.rake
32
+ - tasks/post_load.rake
33
+ - tasks/setup.rb
34
+ - tasks/svn.rake
35
+ - tasks/gem.rake
36
+ - tasks/ann.rake
37
+ - tasks/rdoc.rake
38
+ - tasks/manifest.rake
39
+ - Manifest.txt
40
+ - lib
41
+ - lib/cached_record
42
+ - lib/cached_record/base.rb
43
+ - lib/cached_record.rb
44
+ - Rakefile
45
+ - README.txt
46
+ - History.txt
47
+ has_rdoc: true
48
+ homepage: ""
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --main
52
+ - README.txt
53
+ - --inline-source
54
+ - --charset=UTF-8
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.2.0
73
+ signing_key:
74
+ specification_version: 2
75
+ summary: memcached caching for active record
76
+ test_files:
77
+ - test/migrations
78
+ - test/migrations/create_notes.rb
79
+ - test/migrations/create_users.rb
80
+ - test/models
81
+ - test/models/note.rb
82
+ - test/models/original_user.rb
83
+ - test/models/original_note.rb
84
+ - test/models/user.rb
85
+ - test/test_cached_record.rb
86
+ - test/fixtures
87
+ - test/fixtures/users.yml
88
+ - test/cases
89
+ - test/cases/hello.rb
90
+ - test/environment.rb