cacheable_delegator 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/Gemfile +1 -0
- data/Gemfile.lock +4 -0
- data/VERSION +1 -1
- data/cacheable_delegator.gemspec +7 -2
- data/lib/cacheable_delegator.rb +51 -4
- data/spec/cacheable_delegator_spec.rb +6 -3
- data/spec/mixin_active_record_inline_schema_spec.rb +89 -0
- data/spec/spec_helper.rb +2 -37
- data/spec/spec_records.rb +52 -0
- metadata +17 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YTY0OTYzZmI1MjFmYmQ4YTMwZDk0MDNjMTFkNzZhYTAyNTMzMzJjYw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YzgxYjBmZjVjMzUxMjEzMDA4NjY4ZjZjYTc2Nzk3OTRiMDI2YWFkMQ==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NDQ4YTI3Mjg0NzE0ZDk3NDI3NGZjMjA5MzMwMmI3NjM3MDFkYjNiYmVhMTYz
|
10
|
+
ZDA1Mzk2M2U2MDQ0OGJiNzcyMmIyNTk0OWM3ZDc3YTk2ZWRiMGE1MmVhNDI1
|
11
|
+
NTVhNzU2YjI2OTYyMmI4YzVmM2MwMzQ1OTg2ZTA1OTRiNWM4NDY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NGRlZjEzODY2YWUxNGJmYzAwNzlkZmEyZDZkZmQzYzgyMzlkMGZhODc0OGEy
|
14
|
+
YzUzNmI5NDFmMzJiNGZhYWEwYTU5ZTViMTM3YmIzNWJkOTIzMmI3YWY3Mjk5
|
15
|
+
NGRlNjMwMjc0NzljYjZjM2IzYjliZjE4NTRjNWM2MjUzYTg4NWI=
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
+
active_record_inline_schema (0.6.1)
|
5
|
+
activerecord (>= 3)
|
6
|
+
activesupport
|
4
7
|
activemodel (3.2.14)
|
5
8
|
activesupport (= 3.2.14)
|
6
9
|
builder (~> 3.0.0)
|
@@ -80,6 +83,7 @@ PLATFORMS
|
|
80
83
|
ruby
|
81
84
|
|
82
85
|
DEPENDENCIES
|
86
|
+
active_record_inline_schema
|
83
87
|
activerecord (~> 3.2.14)
|
84
88
|
activesupport (~> 3.2.14)
|
85
89
|
bundler (~> 1.0)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
1.0.0
|
data/cacheable_delegator.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "cacheable_delegator"
|
8
|
-
s.version = "
|
8
|
+
s.version = "1.0.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Dan Nguyen"]
|
@@ -28,7 +28,9 @@ Gem::Specification.new do |s|
|
|
28
28
|
"cacheable_delegator.gemspec",
|
29
29
|
"lib/cacheable_delegator.rb",
|
30
30
|
"spec/cacheable_delegator_spec.rb",
|
31
|
-
"spec/
|
31
|
+
"spec/mixin_active_record_inline_schema_spec.rb",
|
32
|
+
"spec/spec_helper.rb",
|
33
|
+
"spec/spec_records.rb"
|
32
34
|
]
|
33
35
|
s.homepage = "http://github.com/dannguyen/cacheable_delegator"
|
34
36
|
s.licenses = ["MIT"]
|
@@ -42,6 +44,7 @@ Gem::Specification.new do |s|
|
|
42
44
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
43
45
|
s.add_runtime_dependency(%q<pry>, [">= 0"])
|
44
46
|
s.add_runtime_dependency(%q<activesupport>, ["~> 3.2.14"])
|
47
|
+
s.add_runtime_dependency(%q<active_record_inline_schema>, [">= 0"])
|
45
48
|
s.add_development_dependency(%q<rspec>, ["~> 2.14.1"])
|
46
49
|
s.add_development_dependency(%q<bundler>, ["~> 1.0"])
|
47
50
|
s.add_development_dependency(%q<jeweler>, ["~> 1.8.7"])
|
@@ -51,6 +54,7 @@ Gem::Specification.new do |s|
|
|
51
54
|
else
|
52
55
|
s.add_dependency(%q<pry>, [">= 0"])
|
53
56
|
s.add_dependency(%q<activesupport>, ["~> 3.2.14"])
|
57
|
+
s.add_dependency(%q<active_record_inline_schema>, [">= 0"])
|
54
58
|
s.add_dependency(%q<rspec>, ["~> 2.14.1"])
|
55
59
|
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
56
60
|
s.add_dependency(%q<jeweler>, ["~> 1.8.7"])
|
@@ -61,6 +65,7 @@ Gem::Specification.new do |s|
|
|
61
65
|
else
|
62
66
|
s.add_dependency(%q<pry>, [">= 0"])
|
63
67
|
s.add_dependency(%q<activesupport>, ["~> 3.2.14"])
|
68
|
+
s.add_dependency(%q<active_record_inline_schema>, [">= 0"])
|
64
69
|
s.add_dependency(%q<rspec>, ["~> 2.14.1"])
|
65
70
|
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
66
71
|
s.add_dependency(%q<jeweler>, ["~> 1.8.7"])
|
data/lib/cacheable_delegator.rb
CHANGED
@@ -1,19 +1,66 @@
|
|
1
1
|
require 'active_record'
|
2
2
|
require 'active_support'
|
3
|
+
require 'active_record_inline_schema'
|
3
4
|
|
4
5
|
module CacheableDelegator
|
5
6
|
extend ActiveSupport::Concern
|
6
7
|
|
8
|
+
EXCLUDED_FIELDS = [:id, :cache_created_at, :cache_updated_at, :source_record_id, :source_record_type]
|
9
|
+
COL_ATTRIBUTES_TO_UPGRADE = [:limit, :name, :null, :precision, :scale, :sql_type, :type]
|
10
|
+
|
7
11
|
included do
|
8
|
-
class_attribute :
|
12
|
+
class_attribute :source_class
|
9
13
|
end
|
10
14
|
|
11
15
|
module ClassMethods
|
12
|
-
|
16
|
+
|
17
|
+
def build_cache(source_obj)
|
18
|
+
att_hsh = self.value_column_names.inject({}) do |hsh, cname|
|
19
|
+
hsh[cname] = source_obj.send cname
|
20
|
+
hsh
|
21
|
+
end
|
22
|
+
|
23
|
+
obj = self.new(att_hsh)
|
24
|
+
obj.source_record = source_obj
|
25
|
+
|
26
|
+
obj
|
27
|
+
end
|
28
|
+
|
29
|
+
def create_cache(obj)
|
30
|
+
c = build_cache(obj)
|
31
|
+
c.save
|
32
|
+
|
33
|
+
c
|
34
|
+
end
|
35
|
+
|
36
|
+
def cache_and_delegate(klass, opts={}, &blk)
|
13
37
|
raise ArgumentError, "Must pass in a class, not a #{klass}" unless klass.is_a?(Class)
|
14
|
-
self.
|
38
|
+
self.source_class = klass
|
39
|
+
belongs_to :source_record, class_name: self.source_class.to_s
|
40
|
+
|
41
|
+
self.source_class
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
def source_columns
|
46
|
+
source_class.columns.reject{|c| EXCLUDED_FIELDS.any?{|f| f.to_s == c.name }}
|
47
|
+
end
|
48
|
+
|
49
|
+
# value columns are just columns that aren't in EXCLUDED_FIELDS
|
50
|
+
def value_column_names
|
51
|
+
self.column_names - EXCLUDED_FIELDS.map{|f| f.to_s}
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
# assumes @@source_class has been set
|
56
|
+
def upgrade_schema!
|
57
|
+
source_columns.each do |source_col|
|
58
|
+
source_col_atts = COL_ATTRIBUTES_TO_UPGRADE.inject({}){|hsh, att| hsh[att.to_sym] = source_col.send att; hsh }
|
59
|
+
|
60
|
+
col source_col.name, source_col_atts
|
61
|
+
end
|
15
62
|
|
16
|
-
|
63
|
+
self.auto_upgrade!( :gentle => true )
|
17
64
|
end
|
18
65
|
end
|
19
66
|
|
@@ -9,9 +9,9 @@ describe CacheableDelegator do
|
|
9
9
|
expect{ MyCachedRecord.cache_and_delegate("X::Foo") }.to raise_error ArgumentError
|
10
10
|
end
|
11
11
|
|
12
|
-
it 'should set the :
|
12
|
+
it 'should set the :source_class attribute' do
|
13
13
|
MyCachedRecord.cache_and_delegate MyRecord
|
14
|
-
expect(MyCachedRecord.
|
14
|
+
expect(MyCachedRecord.source_class).to eq MyRecord
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
@@ -38,7 +38,10 @@ describe CacheableDelegator do
|
|
38
38
|
it 'will use its own :foo, without :delegate_ prefix' do
|
39
39
|
@cache_record.foo = 'baz'
|
40
40
|
expect(@cache_record.foo).to eq 'baz'
|
41
|
-
end
|
41
|
+
end
|
42
42
|
end
|
43
43
|
end
|
44
|
+
|
45
|
+
|
46
|
+
|
44
47
|
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CacheableDelegator do
|
4
|
+
|
5
|
+
context 'compatability with active_record_inline_schema' do
|
6
|
+
context 'create schema based on @@source_class ' do
|
7
|
+
|
8
|
+
describe '.upgrade_schema! effects' do
|
9
|
+
describe 'default call' do
|
10
|
+
before(:each) do
|
11
|
+
MyCachedRecord.upgrade_schema!
|
12
|
+
end
|
13
|
+
|
14
|
+
after(:each) do
|
15
|
+
reload_records!
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should mimic column names' do
|
19
|
+
# in spec_records.rb, the default call is invoked
|
20
|
+
expect(MyCachedRecord.column_names).to include( 'awesome_value', 'name', 'address', 'question')
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should not erase anything on upgrades' do
|
24
|
+
expect(MyCachedRecord.column_names).to include( 'foo', 'foo_double_awesome_value', 'source_record_id')
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should mimic column types and attributes' do
|
28
|
+
a_col = MyCachedRecord.columns.find{|c| c.name == 'address'}
|
29
|
+
expect(a_col.limit).to eq 42
|
30
|
+
expect(a_col.type).to eq :string
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should allow addition of different columns'
|
36
|
+
it 'should allow exclusion of specified columns'
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
context 'record building' do
|
41
|
+
before(:each) do
|
42
|
+
MyCachedRecord.upgrade_schema!
|
43
|
+
|
44
|
+
@source = MyRecord.create awesome_value: 88
|
45
|
+
@cache = MyCachedRecord.build_cache(@source)
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
describe '#build_cache' do
|
50
|
+
|
51
|
+
|
52
|
+
it 'should have same awesome_value as its source_object' do
|
53
|
+
expect(@cache.awesome_value).to eq 88
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should also have expected derived awesome_value in its column' do
|
57
|
+
expect(@cache.foo_double_awesome_value).to eq @source.foo_double_awesome_value
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'should be associated to source_object' do
|
61
|
+
expect(@cache.source_record).to be @source
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe '#create_cache' do
|
66
|
+
it 'should be a saved record' do
|
67
|
+
savedcache = MyCachedRecord.create_cache(@source)
|
68
|
+
expect(savedcache.valid?).to be_true
|
69
|
+
expect(savedcache.new_record?).to be_false
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
context 'duplicating associated records' do
|
74
|
+
it 'should only duplicate one level down'
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'maintanence' do
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -6,45 +6,10 @@ require 'pry'
|
|
6
6
|
require 'database_cleaner'
|
7
7
|
DatabaseCleaner.strategy = :truncation
|
8
8
|
|
9
|
-
ActiveRecord::Base.establish_connection(
|
10
|
-
:adapter => "sqlite3",
|
11
|
-
:database => ":memory:"
|
12
|
-
)
|
13
|
-
ActiveRecord::Migration.verbose = false
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
ActiveRecord::Schema.define do
|
18
|
-
create_table :my_records do |t|
|
19
|
-
t.integer :values
|
20
|
-
end
|
21
|
-
create_table :my_cached_records do |t|
|
22
|
-
t.string :foo
|
23
|
-
t.integer :double_values
|
24
|
-
t.integer :source_record_id
|
25
|
-
t.string :source_record_type
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
|
30
|
-
class MyRecord < ActiveRecord::Base
|
31
9
|
|
32
|
-
|
33
|
-
"This is foo"
|
34
|
-
end
|
35
|
-
|
36
|
-
def double_values
|
37
|
-
values * 2
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
41
|
-
|
42
|
-
|
43
|
-
class MyCachedRecord < ActiveRecord::Base
|
44
|
-
include CacheableDelegator
|
45
|
-
cache_and_delegate MyRecord
|
46
|
-
end
|
10
|
+
ActiveRecord::Migration.verbose = false
|
47
11
|
|
12
|
+
require 'spec_records'
|
48
13
|
|
49
14
|
|
50
15
|
RSpec.configure do |config|
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module SpecBootstrap
|
2
|
+
def reload_records!
|
3
|
+
load(__FILE__)
|
4
|
+
end
|
5
|
+
end
|
6
|
+
include SpecBootstrap
|
7
|
+
|
8
|
+
|
9
|
+
ActiveRecord::Base.establish_connection(
|
10
|
+
:adapter => "sqlite3",
|
11
|
+
:database => ":memory:"
|
12
|
+
)
|
13
|
+
ActiveRecord::Schema.define do
|
14
|
+
|
15
|
+
|
16
|
+
drop_table :my_records if ActiveRecord::Base.connection.table_exists?(:my_records)
|
17
|
+
|
18
|
+
create_table :my_records, force: true do |t|
|
19
|
+
t.integer :awesome_value
|
20
|
+
t.string :name
|
21
|
+
t.string :address, limit: 42
|
22
|
+
t.boolean :question
|
23
|
+
end
|
24
|
+
|
25
|
+
drop_table :my_cached_records if ActiveRecord::Base.connection.table_exists?(:my_cached_records)
|
26
|
+
create_table :my_cached_records, force: true do |t|
|
27
|
+
t.string :foo
|
28
|
+
t.integer :foo_double_awesome_value
|
29
|
+
t.integer :source_record_id
|
30
|
+
t.string :source_record_type
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
class MyRecord < ActiveRecord::Base
|
36
|
+
def foo
|
37
|
+
"This is foo"
|
38
|
+
end
|
39
|
+
|
40
|
+
def foo_double_awesome_value
|
41
|
+
awesome_value * 2
|
42
|
+
end
|
43
|
+
end
|
44
|
+
MyRecord.reset_column_information
|
45
|
+
|
46
|
+
|
47
|
+
class MyCachedRecord < ActiveRecord::Base
|
48
|
+
include CacheableDelegator
|
49
|
+
cache_and_delegate MyRecord
|
50
|
+
end
|
51
|
+
MyCachedRecord.reset_column_information
|
52
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cacheable_delegator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dan Nguyen
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 3.2.14
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: active_record_inline_schema
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: rspec
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -141,7 +155,9 @@ files:
|
|
141
155
|
- cacheable_delegator.gemspec
|
142
156
|
- lib/cacheable_delegator.rb
|
143
157
|
- spec/cacheable_delegator_spec.rb
|
158
|
+
- spec/mixin_active_record_inline_schema_spec.rb
|
144
159
|
- spec/spec_helper.rb
|
160
|
+
- spec/spec_records.rb
|
145
161
|
homepage: http://github.com/dannguyen/cacheable_delegator
|
146
162
|
licenses:
|
147
163
|
- MIT
|