lookup_by 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/lookup_by/cache.rb +12 -4
- data/lib/lookup_by/lookup.rb +27 -2
- data/lib/lookup_by/version.rb +1 -1
- data/spec/association_spec.rb +0 -4
- data/spec/support/shared_examples_for_a_lookup.rb +39 -0
- metadata +15 -19
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: faef63af11f71cb58ed963cb274535be04ee72a1
|
4
|
+
data.tar.gz: 52faac6d69885248e29fb4292433786c73ba4d9f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 141b39d68b61dc72c1bcb12f87a192ffbc41d312649b88ccd6e996db19003f65e051ec41412fa16462f59bd30ac9555175fd682e7b3475a5ff9ecbe80db92c06
|
7
|
+
data.tar.gz: b0ccddd30f0245895891cd39f4152c1a9a17aceb1e3c945b74a06057d4fc8c6cf084dd3ba73a28eb35cbbda4ffbf38503a7284503115271987eaa6a5612349dc
|
data/lib/lookup_by/cache.rb
CHANGED
@@ -51,6 +51,10 @@ module LookupBy
|
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
|
+
def clear
|
55
|
+
cache.clear if cache?
|
56
|
+
end
|
57
|
+
|
54
58
|
def create!(*args, &block)
|
55
59
|
created = klass.create!(*args, &block)
|
56
60
|
cache[created.id] = created if cache?
|
@@ -72,6 +76,10 @@ module LookupBy
|
|
72
76
|
found
|
73
77
|
end
|
74
78
|
|
79
|
+
def has_cache?
|
80
|
+
!!type
|
81
|
+
end
|
82
|
+
|
75
83
|
def read_through?
|
76
84
|
@read
|
77
85
|
end
|
@@ -118,10 +126,6 @@ module LookupBy
|
|
118
126
|
value.is_a?(Fixnum) ? primary_key : field
|
119
127
|
end
|
120
128
|
|
121
|
-
def cache?
|
122
|
-
!!type && enabled?
|
123
|
-
end
|
124
|
-
|
125
129
|
def enabled?
|
126
130
|
enabled
|
127
131
|
end
|
@@ -130,6 +134,10 @@ module LookupBy
|
|
130
134
|
type == :all
|
131
135
|
end
|
132
136
|
|
137
|
+
def cache?
|
138
|
+
!!type && enabled?
|
139
|
+
end
|
140
|
+
|
133
141
|
def write?
|
134
142
|
!!write
|
135
143
|
end
|
data/lib/lookup_by/lookup.rb
CHANGED
@@ -9,7 +9,7 @@ module LookupBy
|
|
9
9
|
options.symbolize_keys!
|
10
10
|
options.assert_valid_keys :order, :cache, :normalize, :find, :find_or_create, :raise
|
11
11
|
|
12
|
-
raise "#{self} already
|
12
|
+
raise "#{self} already called lookup_by" if is_a? LookupBy::Lookup::ClassMethods
|
13
13
|
raise "#{self} responds_to :[], needed for lookup_by" if respond_to? :[]
|
14
14
|
raise "#{self} responds_to :lookup, needed for lookup_by" if respond_to? :lookup
|
15
15
|
|
@@ -35,8 +35,9 @@ module LookupBy
|
|
35
35
|
end
|
36
36
|
|
37
37
|
module ClassMethods
|
38
|
-
def all
|
38
|
+
def all(*args)
|
39
39
|
return super if @lookup.read_through?
|
40
|
+
return super if args.any?
|
40
41
|
|
41
42
|
@lookup.cache.values
|
42
43
|
end
|
@@ -64,6 +65,30 @@ module LookupBy
|
|
64
65
|
else raise TypeError, "#{name}[arg]: arg must be a String, Symbol, Fixnum, nil, or #{name}"
|
65
66
|
end
|
66
67
|
end
|
68
|
+
|
69
|
+
def destroy_all(conditions = nil)
|
70
|
+
raise NotImplementedError, "#{name}.destroy_all is not supported on cached lookup tables." if @lookup.has_cache?
|
71
|
+
|
72
|
+
super
|
73
|
+
end
|
74
|
+
|
75
|
+
def destroy(id)
|
76
|
+
raise NotImplementedError, "#{name}.destroy(arg) is not supported on cached lookup tables" if @lookup.has_cache?
|
77
|
+
|
78
|
+
super
|
79
|
+
end
|
80
|
+
|
81
|
+
def delete_all(conditions = nil)
|
82
|
+
raise NotImplementedError, "#{name}.delete_all is not supported on cached lookup tables." if @lookup.has_cache?
|
83
|
+
|
84
|
+
super
|
85
|
+
end
|
86
|
+
|
87
|
+
def delete(id_or_array)
|
88
|
+
raise NotImplementedError, "#{name}.delete(arg) is not supported on cached lookup tables." if @lookup.has_cache?
|
89
|
+
|
90
|
+
super
|
91
|
+
end
|
67
92
|
end
|
68
93
|
|
69
94
|
module InstanceMethods
|
data/lib/lookup_by/version.rb
CHANGED
data/spec/association_spec.rb
CHANGED
@@ -39,6 +39,23 @@ shared_examples "a proxy" do
|
|
39
39
|
subject.update(original.id, name: "updated")
|
40
40
|
subject[original.id].name.should_not eq original.name
|
41
41
|
end
|
42
|
+
|
43
|
+
it "allows .destroy_all" do
|
44
|
+
subject.destroy_all.should == []
|
45
|
+
end
|
46
|
+
|
47
|
+
it "allows .destroy" do
|
48
|
+
instance = subject.create(name: "foo")
|
49
|
+
subject.destroy(instance.id).should == instance
|
50
|
+
end
|
51
|
+
|
52
|
+
it "allows .delete_all" do
|
53
|
+
subject.delete_all.should == 0
|
54
|
+
end
|
55
|
+
|
56
|
+
it "allows .delete" do
|
57
|
+
subject.delete(1).should == 0
|
58
|
+
end
|
42
59
|
end
|
43
60
|
|
44
61
|
shared_examples "a cache" do
|
@@ -56,6 +73,22 @@ shared_examples "a cache" do
|
|
56
73
|
|
57
74
|
subject.lookup.enabled = was_enabled
|
58
75
|
end
|
76
|
+
|
77
|
+
it "raises on .destroy_all" do
|
78
|
+
expect { subject.destroy_all }.to raise_error NotImplementedError, /destroy_all.*not supported/
|
79
|
+
end
|
80
|
+
|
81
|
+
it "raises on .destroy(id)" do
|
82
|
+
expect { subject.destroy(1) }.to raise_error NotImplementedError, /destroy.*not supported/
|
83
|
+
end
|
84
|
+
|
85
|
+
it "raises on .delete_all" do
|
86
|
+
expect { subject.delete_all }.to raise_error NotImplementedError, /delete_all.*not supported/
|
87
|
+
end
|
88
|
+
|
89
|
+
it "raises on .delete(id)" do
|
90
|
+
expect { subject.delete(1) }.to raise_error NotImplementedError, /delete.*not supported/
|
91
|
+
end
|
59
92
|
end
|
60
93
|
|
61
94
|
shared_examples "a strict cache" do
|
@@ -67,6 +100,12 @@ shared_examples "a strict cache" do
|
|
67
100
|
expect { subject.create(name: "add") }.to_not change(subject, :all)
|
68
101
|
end
|
69
102
|
|
103
|
+
it "reloads .all when called with args" do
|
104
|
+
new = subject.create(name: "new")
|
105
|
+
subject.all.should_not include(new)
|
106
|
+
subject.all({}).should include(new)
|
107
|
+
end
|
108
|
+
|
70
109
|
it "caches .pluck" do
|
71
110
|
subject.create(name: "pluck this")
|
72
111
|
subject.pluck(:name).should_not include("pluck this")
|
metadata
CHANGED
@@ -1,32 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lookup_by
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
version: 0.1.0
|
4
|
+
version: 0.1.1
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Erik Peterson
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-05-08 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
|
-
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 3.0.0
|
20
|
-
none: false
|
21
|
-
name: rails
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
|
-
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
23
|
requirements:
|
26
|
-
- -
|
24
|
+
- - ">="
|
27
25
|
- !ruby/object:Gem::Version
|
28
26
|
version: 3.0.0
|
29
|
-
none: false
|
30
27
|
description: Use database lookup tables in AR models.
|
31
28
|
email:
|
32
29
|
- erik@enova.com
|
@@ -34,9 +31,9 @@ executables: []
|
|
34
31
|
extensions: []
|
35
32
|
extra_rdoc_files: []
|
36
33
|
files:
|
37
|
-
- .gitignore
|
38
|
-
- .rvmrc
|
39
|
-
- .travis.yml
|
34
|
+
- ".gitignore"
|
35
|
+
- ".rvmrc"
|
36
|
+
- ".travis.yml"
|
40
37
|
- Gemfile
|
41
38
|
- MIT-LICENSE
|
42
39
|
- README.md
|
@@ -86,27 +83,26 @@ files:
|
|
86
83
|
- spec/support/shared_examples_for_a_lookup.rb
|
87
84
|
homepage: http://www.github.com/companygardener/lookup_by
|
88
85
|
licenses: []
|
86
|
+
metadata: {}
|
89
87
|
post_install_message:
|
90
88
|
rdoc_options: []
|
91
89
|
require_paths:
|
92
90
|
- lib
|
93
91
|
required_ruby_version: !ruby/object:Gem::Requirement
|
94
92
|
requirements:
|
95
|
-
- -
|
93
|
+
- - ">="
|
96
94
|
- !ruby/object:Gem::Version
|
97
95
|
version: '0'
|
98
|
-
none: false
|
99
96
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
97
|
requirements:
|
101
|
-
- -
|
98
|
+
- - ">="
|
102
99
|
- !ruby/object:Gem::Version
|
103
100
|
version: '0'
|
104
|
-
none: false
|
105
101
|
requirements: []
|
106
102
|
rubyforge_project:
|
107
|
-
rubygems_version:
|
103
|
+
rubygems_version: 2.2.2
|
108
104
|
signing_key:
|
109
|
-
specification_version:
|
105
|
+
specification_version: 4
|
110
106
|
summary: A thread-safe lookup table cache for ActiveRecord
|
111
107
|
test_files:
|
112
108
|
- spec/association_spec.rb
|