lookup_by 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.ruby-version +1 -1
- data/.travis.yml +2 -2
- data/Gemfile +2 -21
- data/README.md +59 -29
- data/lib/lookup_by/lookup.rb +5 -1
- data/lib/lookup_by/version.rb +1 -1
- data/lookup_by.gemspec +1 -1
- data/spec/dummy/db/migrate/20121019040009_create_tables.rb +1 -1
- data/spec/dummy/db/structure.sql +1 -1
- data/spec/lookup_by_spec.rb +4 -0
- metadata +2 -3
- data/.rvmrc +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 08f7517649396422ab4bc56217543fff26eb0606
|
4
|
+
data.tar.gz: 7c6c3843e075d0a8b78af06ce75e644b072446ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e07af94417290aad36f97135a2fb6295e77388b531d7ef8d97c9bf14238cc08c8230ef3ad1ccc308d977d1c94f02edc60fc1ab5764db7745ef2d2c1b37e6d4fd
|
7
|
+
data.tar.gz: 42114c0e2a352d15770d5564a2196cf279ff9a90d71ab59226ceea90e2ae20a7071011c46730b1c894ee0502410282aab8cebb2e3bafba5e51189a814a7ba1d0
|
data/.gitignore
CHANGED
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-2.1.
|
1
|
+
ruby-2.1.1
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
@@ -18,25 +18,6 @@ group :development, :test do
|
|
18
18
|
gem "pry", require: false
|
19
19
|
gem 'colored', require: false
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
gem 'json'
|
24
|
-
|
25
|
-
# Simplecov and Coveralls
|
26
|
-
gem 'rubysl-coverage'
|
27
|
-
gem 'rubinius-coverage'
|
28
|
-
|
29
|
-
# Pry
|
30
|
-
gem 'rubysl-readline'
|
31
|
-
|
32
|
-
# Rails
|
33
|
-
gem 'rubysl-base64'
|
34
|
-
gem 'rubysl-benchmark'
|
35
|
-
gem 'rubysl-bigdecimal'
|
36
|
-
gem 'rubysl-digest'
|
37
|
-
gem 'rubysl-ipaddr'
|
38
|
-
gem 'rubysl-logger'
|
39
|
-
gem 'rubysl-mutex_m'
|
40
|
-
gem 'rubysl-singleton'
|
41
|
-
end
|
21
|
+
gem 'racc'
|
22
|
+
gem 'json'
|
42
23
|
end
|
data/README.md
CHANGED
@@ -14,12 +14,13 @@
|
|
14
14
|
[codeclimate]: https://codeclimate.com/github/companygardener/lookup_by
|
15
15
|
[gittip]: https://www.gittip.com/companygardener
|
16
16
|
|
17
|
-
|
17
|
+
# Overview
|
18
18
|
|
19
|
-
LookupBy is a thread-safe lookup table cache for ActiveRecord that reduces normalization pains
|
19
|
+
LookupBy is a thread-safe lookup table cache for ActiveRecord that reduces normalization pains and supports:
|
20
20
|
|
21
|
-
*
|
22
|
-
* caching (read-through, write-through,
|
21
|
+
* configurable column names
|
22
|
+
* caching (read-through, write-through, least recently used (LRU))
|
23
|
+
* symbols
|
23
24
|
|
24
25
|
### Dependencies
|
25
26
|
|
@@ -44,22 +45,24 @@ If you find a security bug, please **_do not_** use the issue tracker. Instead,
|
|
44
45
|
|
45
46
|
Please create [Issues][issues] to submit bug reports and feature requests.
|
46
47
|
|
47
|
-
_Provide a failing rspec test concisely demonstrates the issue._
|
48
|
+
_Provide a failing rspec test that concisely demonstrates the issue._
|
48
49
|
|
49
|
-
|
50
|
+
# Installation
|
50
51
|
|
51
|
-
|
52
|
+
Add this line to your application's Gemfile:
|
52
53
|
|
53
|
-
# in Gemfile
|
54
54
|
gem "lookup_by"
|
55
55
|
|
56
|
+
And then execute:
|
57
|
+
|
56
58
|
$ bundle
|
57
59
|
|
58
60
|
Or install it manually:
|
59
61
|
|
60
62
|
$ gem install lookup_by
|
61
63
|
|
62
|
-
|
64
|
+
|
65
|
+
# Usage
|
63
66
|
|
64
67
|
### ActiveRecord Plugin
|
65
68
|
|
@@ -78,13 +81,23 @@ lookup_for :status
|
|
78
81
|
|
79
82
|
```ruby
|
80
83
|
# db/migrate/201301010012_create_statuses_table.rb
|
81
|
-
create_table :statuses do |t|
|
82
|
-
t.
|
84
|
+
create_table :statuses, primary_key: :status_id do |t|
|
85
|
+
t.text :status, null: false
|
83
86
|
end
|
84
87
|
|
85
88
|
# Or use the shorthand
|
86
89
|
create_lookup_table :statuses
|
87
90
|
|
91
|
+
# UUID primary key
|
92
|
+
# options[:id] = :uuid
|
93
|
+
#
|
94
|
+
# SMALLSERIAL primary key
|
95
|
+
# options[:small] = true
|
96
|
+
#
|
97
|
+
# Change the lookup column
|
98
|
+
# options[:lookup_column] = "phone_number"
|
99
|
+
# options[:lookup_type] = :phone
|
100
|
+
|
88
101
|
# app/models/status.rb
|
89
102
|
class Status < ActiveRecord::Base
|
90
103
|
lookup_by :status
|
@@ -94,7 +107,7 @@ end
|
|
94
107
|
Status.new(name: "paid")
|
95
108
|
```
|
96
109
|
|
97
|
-
###
|
110
|
+
### Define an association
|
98
111
|
|
99
112
|
```ruby
|
100
113
|
# db/migrate/201301010123_create_orders_table.rb
|
@@ -108,7 +121,7 @@ class Order < ActiveRecord::Base
|
|
108
121
|
end
|
109
122
|
```
|
110
123
|
|
111
|
-
|
124
|
+
LookupBy creates methods that use the `status` attribute transparently:
|
112
125
|
|
113
126
|
```ruby
|
114
127
|
order = Order.new(status: "paid")
|
@@ -132,6 +145,8 @@ Order.column_names
|
|
132
145
|
=> ["order_id", "status_id"]
|
133
146
|
```
|
134
147
|
|
148
|
+
# Configuration
|
149
|
+
|
135
150
|
### Symbolize
|
136
151
|
|
137
152
|
Casts the attribute to a symbol. Enables the setter to take a symbol.
|
@@ -172,7 +187,7 @@ lookup_for :status, strict: false
|
|
172
187
|
|
173
188
|
The default is no caching. You can also cache all records or use an LRU.
|
174
189
|
|
175
|
-
_Note: caching is
|
190
|
+
_Note: caching is **per process**, make sure you think through the implications._
|
176
191
|
|
177
192
|
```ruby
|
178
193
|
# No caching - Not very useful
|
@@ -194,7 +209,9 @@ lookup_by :column_name, cache: true
|
|
194
209
|
lookup_by :column_name, cache: 50
|
195
210
|
```
|
196
211
|
|
197
|
-
###
|
212
|
+
### Cache miss
|
213
|
+
|
214
|
+
You can enable read-throughs using the `:find` option.
|
198
215
|
|
199
216
|
```ruby
|
200
217
|
# Return nil
|
@@ -210,7 +227,11 @@ lookup_by :column_name, cache: 10
|
|
210
227
|
lookup_by :column_name, cache: true, find: true
|
211
228
|
```
|
212
229
|
|
213
|
-
###
|
230
|
+
### DB miss
|
231
|
+
|
232
|
+
You can enable write-throughs using the `:find_or_create` option.
|
233
|
+
|
234
|
+
_Note: This will only work if the primary key is a sequence and all columns but the lookup column are optional._
|
214
235
|
|
215
236
|
```ruby
|
216
237
|
# Return nil
|
@@ -220,8 +241,6 @@ lookup_by :column_name
|
|
220
241
|
# Find or create
|
221
242
|
# Useful for user-submitted fields that grow over time
|
222
243
|
# e.g. user_agents, ip_addresses
|
223
|
-
#
|
224
|
-
# Note: Only works if attributes are nullable
|
225
244
|
lookup_by :column_name, cache: 20, find_or_create: true
|
226
245
|
```
|
227
246
|
|
@@ -281,23 +300,34 @@ To run the test suite:
|
|
281
300
|
rake app:db:test:prepare
|
282
301
|
rake
|
283
302
|
|
284
|
-
#
|
285
|
-
|
286
|
-
### Contributing
|
287
|
-
|
288
|
-
1. Fork
|
289
|
-
2. Create a feature branch `git checkout -b new-hotness`
|
290
|
-
3. Commit your changes `git commit -am 'Added some feature'`
|
291
|
-
4. Push to the branch `git push origin new-hotness`
|
292
|
-
5. Create a Pull Request
|
303
|
+
# Contribute
|
293
304
|
|
294
|
-
|
305
|
+
1. Fork
|
306
|
+
2. Create a feature branch `git checkout -b new-hotness`
|
307
|
+
3. Commit your changes `git commit -am 'Added some feature'`
|
308
|
+
4. Push to the branch `git push origin new-hotness`
|
309
|
+
5. Create a Pull Request
|
295
310
|
|
296
311
|
A list of authors can be found on the [Contributors][] page.
|
297
312
|
|
313
|
+
# License
|
314
|
+
|
298
315
|
Copyright © 2014 Erik Peterson
|
299
316
|
|
300
|
-
|
317
|
+
MIT License
|
318
|
+
|
319
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
320
|
+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
|
321
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
|
322
|
+
persons to whom the Software is furnished to do so, subject to the following conditions:
|
323
|
+
|
324
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
|
325
|
+
Software.
|
326
|
+
|
327
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
328
|
+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
|
329
|
+
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
330
|
+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
301
331
|
|
302
332
|
[development]: http://github.com/companygardener/lookup_by "LookupBy Development"
|
303
333
|
[issues]: http://github.com/companygardener/lookup_by/issues "LookupBy Issues"
|
data/lib/lookup_by/lookup.rb
CHANGED
@@ -133,8 +133,12 @@ module LookupBy
|
|
133
133
|
table_options = options.slice(:primary_key, :id)
|
134
134
|
table_options[:primary_key] ||= table.singularize + '_id'
|
135
135
|
|
136
|
+
table_options[:id] = false if options[:small]
|
137
|
+
|
136
138
|
create_table name, table_options do |t|
|
137
|
-
t.
|
139
|
+
t.column table_options[:primary_key], 'smallserial primary key' if options[:small]
|
140
|
+
|
141
|
+
t.column lookup_column, lookup_type, null: false
|
138
142
|
|
139
143
|
yield t if block_given?
|
140
144
|
end
|
data/lib/lookup_by/version.rb
CHANGED
data/lookup_by.gemspec
CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |gem|
|
|
12
12
|
gem.description = %q(Use database lookup tables in AR models.)
|
13
13
|
|
14
14
|
gem.authors = ["Erik Peterson"]
|
15
|
-
gem.email = ["
|
15
|
+
gem.email = ["thecompanygardener@gmail.com"]
|
16
16
|
|
17
17
|
gem.homepage = "https://www.github.com/companygardener/lookup_by"
|
18
18
|
gem.license = "MIT"
|
data/spec/dummy/db/structure.sql
CHANGED
data/spec/lookup_by_spec.rb
CHANGED
@@ -42,6 +42,10 @@ describe LookupBy::Lookup do
|
|
42
42
|
|
43
43
|
subject[" paid "].id.should == status.id
|
44
44
|
end
|
45
|
+
|
46
|
+
it "has a small primary key" do
|
47
|
+
expect { Status.create(status_id: 100_000, status: "too_big") }.to raise_error
|
48
|
+
end
|
45
49
|
end
|
46
50
|
|
47
51
|
context "EmailAddress.lookup_by :column, find_or_create: true" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lookup_by
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Erik Peterson
|
@@ -54,14 +54,13 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
description: Use database lookup tables in AR models.
|
56
56
|
email:
|
57
|
-
-
|
57
|
+
- thecompanygardener@gmail.com
|
58
58
|
executables: []
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
62
|
- ".gitignore"
|
63
63
|
- ".ruby-version"
|
64
|
-
- ".rvmrc"
|
65
64
|
- ".travis.yml"
|
66
65
|
- Gemfile
|
67
66
|
- MIT-LICENSE
|
data/.rvmrc
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
rvm use ruby-2.0.0-p353@lookup_by --create
|