static_record_cache 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ class ContextCarrier < ActiveRecord::Base
2
+ set_table_name 'carriers'
3
+ extend StaticActiveRecordContext
4
+ has_many :phone_numbers, :foreign_key => :carrier_id
5
+ end
@@ -0,0 +1,5 @@
1
+ class PhoneNumber < ActiveRecord::Base
2
+ belongs_to :carrier
3
+ belongs_to :context_carrier, :class_name => 'ContextCarrier', :foreign_key => :carrier_id
4
+ belongs_to :static_carrier, :class_name => 'StaticCarrier', :foreign_key => :carrier_id
5
+ end
@@ -0,0 +1,54 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class StaticActiveRecordContextTest < TestCaseSuperClass
4
+ self.fixtures :carriers, :phone_numbers
5
+
6
+
7
+ def test_should_store_one_record
8
+ ContextCarrier.context_cache = {}
9
+ phone = phone_numbers(:phone_number_1)
10
+ phone.context_carrier
11
+ phone.reload
12
+ phone.context_carrier
13
+
14
+ assert_equal 1, ContextCarrier.context_cache[ContextCarrier].size
15
+ assert_equal(carriers(:carrier_1).attributes, ContextCarrier.cached[phone.carrier.id].attributes)
16
+ end
17
+
18
+ def test_should_store_all_records_in_cache
19
+ ContextCarrier.context_cache = {}
20
+
21
+ records = ContextCarrier.find(:all)
22
+ assert_equal 13, ContextCarrier.context_cache[ContextCarrier].size
23
+
24
+ records.each{ |record|
25
+ carrier = carriers("carrier_#{record.to_param}")
26
+ assert_equal carrier.attributes, ContextCarrier.cached[carrier.id].attributes
27
+ }
28
+ end
29
+
30
+ def test_should_retain_cache_after_other_record_context
31
+ PhoneNumber.with_context do
32
+ PhoneNumber.find(:all)
33
+ assert_equal PhoneNumber.count, PhoneNumber.context_cache[PhoneNumber].size
34
+ ContextCarrier.find(:all)
35
+ assert_equal ContextCarrier.count, ContextCarrier.context_cache[ContextCarrier].size
36
+ end
37
+
38
+ assert_nil PhoneNumber.context_cache
39
+ assert_equal ContextCarrier.count, ContextCarrier.context_cache[ContextCarrier].size
40
+ end
41
+
42
+ def test_should_retain_cache_after_same_record_context
43
+ ContextCarrier.with_context do
44
+ PhoneNumber.find(:all)
45
+ assert_equal PhoneNumber.count, PhoneNumber.context_cache[PhoneNumber].size
46
+ ContextCarrier.find(:all)
47
+ assert_equal ContextCarrier.count, ContextCarrier.context_cache[ContextCarrier].size
48
+ end
49
+
50
+ assert_nil PhoneNumber.context_cache
51
+ assert_equal ContextCarrier.count, ContextCarrier.context_cache[ContextCarrier].size
52
+ end
53
+
54
+ end
@@ -0,0 +1,99 @@
1
+
2
+
3
+ require 'ostruct'
4
+
5
+ require File.dirname(__FILE__)+"/../../../../config/boot"
6
+
7
+ begin ; require 'active_record' ; rescue LoadError; require 'rubygems'; require 'active_record'; end
8
+
9
+ require 'active_support'
10
+ require 'active_record'
11
+ require 'active_record/version'
12
+ require 'active_record/fixtures'
13
+ require 'mocha'
14
+ require 'test/unit'
15
+ require 'fileutils'
16
+
17
+
18
+ dir = File.dirname(__FILE__)
19
+
20
+ require File.join(dir, '/../init')
21
+
22
+ ActiveRecord::Base.logger = Logger.new("debug.log")
23
+
24
+ config = ActiveRecord::Base.configurations['test'] = {
25
+ :adapter => "mysql",
26
+ :username => "root",
27
+ :encoding => "utf8",
28
+ :host => '127.0.0.1',
29
+ :database => 'static_record_cache' }
30
+
31
+ ActiveRecord::Base.establish_connection( config )
32
+
33
+
34
+ if File.exists?(active_record_context_dir = File.join(dir, '../../active_record_context'))
35
+ require File.join(active_record_context_dir, '/lib/technoweenie/active_record_context.rb')
36
+ require File.join(active_record_context_dir, '/init')
37
+ else
38
+ raise LoadError.new("Cannot load active record context please install plugin")
39
+ end
40
+
41
+ require File.join( dir, '/db/schema.rb' )
42
+
43
+ models_dir = File.join( dir, 'models' )
44
+ $: << models_dir
45
+ Dir[ models_dir + '/*.rb'].each { |m| require m }
46
+
47
+
48
+
49
+
50
+
51
+ if ActiveRecord::VERSION::STRING < '2.3.1'
52
+
53
+ TestCaseSuperClass = Test::Unit::TestCase
54
+ class Test::Unit::TestCase #:nodoc:
55
+ self.use_transactional_fixtures = true
56
+ self.use_instantiated_fixtures = false
57
+
58
+ def assert_queries(num = 1)
59
+ $queries_executed = []
60
+ yield
61
+ ensure
62
+ %w{ BEGIN COMMIT }.each { |x| $queries_executed.delete(x) }
63
+ assert_equal num, $queries_executed.size, "#{$queries_executed.size} instead of #{num} queries were executed.#{$queries_executed.size == 0 ? '' : "\nQueries:\n#{$queries_executed.join("\n")}"}"
64
+ end
65
+
66
+ end
67
+
68
+ Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
69
+ else
70
+
71
+
72
+
73
+ TestCaseSuperClass = ActiveRecord::TestCase
74
+ require 'active_record/test_case'
75
+ class ActiveRecord::TestCase #:nodoc:
76
+ include ActiveRecord::TestFixtures
77
+ self.use_transactional_fixtures = true
78
+ self.use_instantiated_fixtures = false
79
+ self.fixtures :all
80
+ end
81
+ ActiveRecord::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
82
+ end
83
+
84
+ ActiveRecord::Base.connection.class.class_eval do
85
+ IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /^SAVEPOINT/, /^ROLLBACK TO SAVEPOINT/, /^RELEASE SAVEPOINT/, /SHOW FIELDS/]
86
+
87
+ def execute_with_query_record(sql, name = nil, &block)
88
+ $queries_executed ||= []
89
+ $queries_executed << sql unless IGNORED_SQL.any? { |r| sql =~ r }
90
+ execute_without_query_record(sql, name, &block)
91
+ end
92
+
93
+ alias_method_chain :execute, :query_record
94
+ end
95
+
96
+ class TestCaseSuperClass
97
+ def logger; ActiveRecord::Base.logger; end
98
+ def self.logger; ActiveRecord::Base.logger; end
99
+ end
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: static_record_cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Blythe Dunham
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-31 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: "Permanently caches subclasses of ActiveRecord in memory. "
26
+ email: blythe@snowgiraffe.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.rdoc
33
+ files:
34
+ - .gitignore
35
+ - MIT-LICENSE
36
+ - README.rdoc
37
+ - Rakefile
38
+ - VERSION
39
+ - init.rb
40
+ - install.rb
41
+ - lib/acts_as_static_record.rb
42
+ - lib/static_active_record_context.rb
43
+ - lib/static_record_cache.rb
44
+ - static_record_cache.gemspec
45
+ - tasks/static_record_cache_tasks.rake
46
+ - test/acts_as_static_record_test.rb
47
+ - test/db/schema.rb
48
+ - test/fixtures/carriers.yml
49
+ - test/fixtures/phone_numbers.yml
50
+ - test/models/carrier.rb
51
+ - test/models/context_carrier.rb
52
+ - test/models/phone_number.rb
53
+ - test/static_active_record_context_test.rb
54
+ - test/test_helper.rb
55
+ - uninstall.rb
56
+ has_rdoc: true
57
+ homepage: http://github.com/blythedunham/static_record_cache
58
+ licenses: []
59
+
60
+ post_install_message:
61
+ rdoc_options:
62
+ - --charset=UTF-8
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ version:
77
+ requirements: []
78
+
79
+ rubyforge_project:
80
+ rubygems_version: 1.3.5
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Permanently caches subclasses of ActiveRecord in memory.
84
+ test_files:
85
+ - test/acts_as_static_record_test.rb
86
+ - test/db/schema.rb
87
+ - test/models/carrier.rb
88
+ - test/models/context_carrier.rb
89
+ - test/models/phone_number.rb
90
+ - test/static_active_record_context_test.rb
91
+ - test/test_helper.rb