lookup_by 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/.rvmrc +1 -1
- data/.travis.yml +1 -0
- data/README.md +139 -108
- data/lib/lookup_by/cache.rb +1 -1
- data/lib/lookup_by/lookup.rb +1 -1
- data/lib/lookup_by/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea1f94799e8cd1ce83a24d9c91ce3ca5e84a9db2
|
4
|
+
data.tar.gz: b2b4a26d4f25ccaf5a3435d7a8c2436393472300
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3ddc4e6763d8886696d8f7967e79e9920984adf1ec569da6c5bf0dc2711e114371533b5c3abfbe96103146b84b139492d86d2fadf2ccb5fde94f557acd7795f2
|
7
|
+
data.tar.gz: 145ab131848f7fc9a62eb2be3e2367854f2d6485e04216e36527872b90cbf8c950e030200f496c089ecbde93e321945fe8f92e3c40d3341f967ddd3e85a498d9
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0-p247
|
data/.rvmrc
CHANGED
@@ -1 +1 @@
|
|
1
|
-
rvm use
|
1
|
+
rvm use 2.0.0-p0@lookup_by --create
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -5,16 +5,7 @@
|
|
5
5
|
|
6
6
|
### Description
|
7
7
|
|
8
|
-
LookupBy is a thread-safe lookup table cache for ActiveRecord.
|
9
|
-
reduces normalization pains.
|
10
|
-
|
11
|
-
LookupBy adds two macro methods to ActiveRecord:
|
12
|
-
|
13
|
-
`lookup_by :column` — defines `.[]`, `.lookup`, and `.is_a_lookup?`
|
14
|
-
methods on the class.
|
15
|
-
|
16
|
-
`lookup_for :column` — defines `column` and `column=` accessors that
|
17
|
-
transparently reference the lookup table.
|
8
|
+
LookupBy is a thread-safe lookup table cache for ActiveRecord that reduces normalization pains.
|
18
9
|
|
19
10
|
### Features
|
20
11
|
|
@@ -36,56 +27,82 @@ transparently reference the lookup table.
|
|
36
27
|
|
37
28
|
### Issues
|
38
29
|
|
39
|
-
Please submit issues to this Github project in the [Issues
|
40
|
-
tab][issues]. _Provide a failing rspec test that works with the
|
41
|
-
existing test suite_.
|
30
|
+
Please submit issues to this Github project in the [Issues tab][issues]. _Provide a failing rspec test that works with the existing test suite_.
|
42
31
|
|
43
|
-
Installation
|
44
|
-
------------
|
32
|
+
## Installation
|
45
33
|
|
46
|
-
|
34
|
+
```
|
35
|
+
# in Gemfile
|
36
|
+
gem "lookup_by"
|
47
37
|
|
48
|
-
|
38
|
+
$ bundle
|
39
|
+
```
|
49
40
|
|
50
|
-
|
41
|
+
Or install it manually:
|
51
42
|
|
52
|
-
$
|
43
|
+
$ gem install lookup_by
|
53
44
|
|
54
|
-
|
45
|
+
# Usage / Configuration
|
55
46
|
|
56
|
-
|
47
|
+
### ActiveRecord Plugin
|
48
|
+
|
49
|
+
LookupBy adds 2 macro methods to `ActiveRecord::Base`
|
57
50
|
|
58
|
-
|
59
|
-
|
51
|
+
```ruby
|
52
|
+
lookup_by :column_name
|
53
|
+
# Defines .[], .lookup, and .is_a_lookup? class methods.
|
54
|
+
|
55
|
+
lookup_for :column_name
|
56
|
+
# Defines #column_name and #column_name= accessors that transparently reference the lookup table.
|
57
|
+
```
|
60
58
|
|
61
59
|
### Define the lookup model
|
62
60
|
|
63
|
-
|
64
|
-
|
65
|
-
|
61
|
+
```ruby
|
62
|
+
# db/migrate/201301010012_create_statuses_table.rb
|
63
|
+
create_table :statuses do |t|
|
64
|
+
t.string :status, null: false
|
65
|
+
end
|
66
|
+
|
67
|
+
# app/models/status.rb
|
68
|
+
class Status < ActiveRecord::Base
|
69
|
+
lookup_by :status # Replace :status with the name of your lookup column
|
70
|
+
end
|
66
71
|
|
67
|
-
|
68
|
-
|
72
|
+
# Aliases the lookup attribute to :name.
|
73
|
+
Status.new(name: "paid")
|
74
|
+
```
|
69
75
|
|
70
76
|
### Associations / Foreign Keys
|
71
77
|
|
72
|
-
|
73
|
-
|
74
|
-
|
78
|
+
```ruby
|
79
|
+
# db/migrate/201301010123_create_orders_table.rb
|
80
|
+
create_table :orders do |t|
|
81
|
+
t.belongs_to :status
|
82
|
+
end
|
83
|
+
|
84
|
+
# app/models/order.rb
|
85
|
+
class Order < ActiveRecord::Base
|
86
|
+
lookup_for :status
|
87
|
+
end
|
88
|
+
```
|
75
89
|
|
76
90
|
Creates accessors to use the `status` attribute transparently:
|
77
91
|
|
78
|
-
|
92
|
+
```ruby
|
93
|
+
order = Order.new(status: "paid")
|
79
94
|
|
80
|
-
|
81
|
-
|
95
|
+
order.status
|
96
|
+
=> "paid"
|
82
97
|
|
83
|
-
|
84
|
-
|
98
|
+
# Access the lookup object
|
99
|
+
order.raw_status
|
100
|
+
=> <#Status id: 1, status: "paid">
|
85
101
|
|
86
|
-
|
87
|
-
|
88
|
-
|
102
|
+
# Access the lookup value before type casting
|
103
|
+
order.status_before_type_cast
|
104
|
+
=> "paid"
|
105
|
+
```
|
89
106
|
|
90
107
|
### Symbolize
|
91
108
|
|
@@ -94,110 +111,125 @@ Casts the attribute to a symbol. Enables the setter to take a symbol.
|
|
94
111
|
_This is a bad idea if the set of lookup values is large. Symbols are
|
95
112
|
never garbage collected._
|
96
113
|
|
97
|
-
|
98
|
-
|
99
|
-
|
114
|
+
```ruby
|
115
|
+
class Order < ActiveRecord::Base
|
116
|
+
lookup_for :status, symbolize: true
|
117
|
+
end
|
100
118
|
|
101
|
-
|
119
|
+
order = Order.new(status: "paid")
|
102
120
|
|
103
|
-
|
104
|
-
|
121
|
+
order.status
|
122
|
+
=> :paid
|
105
123
|
|
106
|
-
|
107
|
-
|
124
|
+
order.status = :shipped
|
125
|
+
=> :shipped
|
126
|
+
```
|
108
127
|
|
109
128
|
### Strict
|
110
129
|
|
111
|
-
|
112
|
-
# Default
|
113
|
-
lookup_for :status
|
130
|
+
Do you want missing lookup values to raise an error?
|
114
131
|
|
115
|
-
|
116
|
-
|
132
|
+
```ruby
|
133
|
+
# Raise
|
134
|
+
# Default
|
135
|
+
lookup_for :status
|
117
136
|
|
118
|
-
|
119
|
-
|
137
|
+
# this will raise a LookupBy::Error
|
138
|
+
Order.status = "non-existent status"
|
139
|
+
|
140
|
+
# Set to nil
|
141
|
+
lookup_for :status, strict: false
|
142
|
+
```
|
120
143
|
|
121
144
|
### Caching
|
122
145
|
|
123
|
-
|
124
|
-
|
125
|
-
|
146
|
+
```ruby
|
147
|
+
# No caching - Not very useful
|
148
|
+
# Default
|
149
|
+
lookup_by :column_name
|
126
150
|
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
151
|
+
# Cache all
|
152
|
+
# Use for a small finite list (e.g. status codes, US states)
|
153
|
+
#
|
154
|
+
# find: false DEFAULT
|
155
|
+
lookup_by :column_name, cache: true
|
132
156
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
157
|
+
# Cache N (with LRU eviction)
|
158
|
+
# Use for a large list with uneven distribution (e.g. email domain, city)
|
159
|
+
#
|
160
|
+
# find: true DEFAULT and REQUIRED
|
161
|
+
lookup_by :column_name, cache: 50
|
162
|
+
```
|
138
163
|
|
139
164
|
### Configure cache misses
|
140
165
|
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
166
|
+
```ruby
|
167
|
+
# Return nil
|
168
|
+
# Default when caching all records
|
169
|
+
#
|
170
|
+
# Skips the database for these methods:
|
171
|
+
# .all, .count, .pluck
|
172
|
+
lookup_by :column_name, cache: true
|
147
173
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
174
|
+
# Find (read-through)
|
175
|
+
# Required when caching N records
|
176
|
+
lookup_by :column_name, cache: 10
|
177
|
+
lookup_by :column_name, cache: true, find: true
|
178
|
+
```
|
152
179
|
|
153
180
|
### Configure database misses
|
154
181
|
|
155
|
-
|
156
|
-
|
157
|
-
|
182
|
+
```ruby
|
183
|
+
# Return nil
|
184
|
+
# Default
|
185
|
+
lookup_by :column_name
|
158
186
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
187
|
+
# Find or create
|
188
|
+
# Useful for user-submitted fields that grow over time
|
189
|
+
# e.g. user_agents, ip_addresses
|
190
|
+
#
|
191
|
+
# Note: Only works if attributes are nullable
|
192
|
+
lookup_by :column_name, cache: 20, find_or_create: true
|
193
|
+
```
|
165
194
|
|
166
195
|
### Normalizing values
|
167
196
|
|
168
|
-
|
169
|
-
|
170
|
-
|
197
|
+
```ruby
|
198
|
+
# Normalize
|
199
|
+
# Run through the your attribute's setter
|
200
|
+
lookup_by :column_name, normalize: true
|
201
|
+
```
|
171
202
|
|
172
|
-
Integration
|
173
|
-
===========
|
203
|
+
# Integration
|
174
204
|
|
175
205
|
### Cucumber
|
176
206
|
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
207
|
+
```ruby
|
208
|
+
# features/support/env.rb
|
209
|
+
require 'lookup_by/cucumber'
|
210
|
+
```
|
181
211
|
|
182
|
-
This provides `Given I reload the cache for $plural_class_name
|
212
|
+
This provides: `Given I reload the cache for $plural_class_name`
|
183
213
|
|
184
214
|
### SimpleForm
|
185
215
|
|
186
|
-
|
187
|
-
|
188
|
-
|
216
|
+
```haml
|
217
|
+
= simple_form_for @order do |f|
|
218
|
+
= f.input :status
|
219
|
+
= f.input :status, :as => :radio_buttons
|
220
|
+
```
|
189
221
|
|
190
222
|
### Formtastic
|
191
223
|
|
192
|
-
|
193
|
-
|
194
|
-
|
224
|
+
```haml
|
225
|
+
= semantic_form_for @order do |f|
|
226
|
+
= f.input :status
|
227
|
+
= f.input :status, :as => :radio
|
228
|
+
```
|
195
229
|
|
196
|
-
Testing
|
197
|
-
-------
|
230
|
+
## Testing
|
198
231
|
|
199
|
-
This plugin uses rspec and pry for testing. Make sure you have them
|
200
|
-
installed:
|
232
|
+
This plugin uses rspec and pry for testing. Make sure you have them installed:
|
201
233
|
|
202
234
|
bundle
|
203
235
|
|
@@ -205,8 +237,7 @@ To run the test suite:
|
|
205
237
|
|
206
238
|
rake
|
207
239
|
|
208
|
-
Giving Back
|
209
|
-
===========
|
240
|
+
# Giving Back
|
210
241
|
|
211
242
|
### Contributing
|
212
243
|
|
data/lib/lookup_by/cache.rb
CHANGED
data/lib/lookup_by/lookup.rb
CHANGED
@@ -20,7 +20,7 @@ module LookupBy
|
|
20
20
|
|
21
21
|
class << self; attr_reader :lookup; end
|
22
22
|
|
23
|
-
#
|
23
|
+
# TODO: check for a db unique constraint or Rails validation
|
24
24
|
|
25
25
|
unless field == :name || column_names.include?("name")
|
26
26
|
alias_attribute :name, field
|
data/lib/lookup_by/version.rb
CHANGED
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.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Erik Peterson
|
@@ -32,6 +32,7 @@ extensions: []
|
|
32
32
|
extra_rdoc_files: []
|
33
33
|
files:
|
34
34
|
- ".gitignore"
|
35
|
+
- ".ruby-version"
|
35
36
|
- ".rvmrc"
|
36
37
|
- ".travis.yml"
|
37
38
|
- Gemfile
|