lookup_by 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: faef63af11f71cb58ed963cb274535be04ee72a1
4
- data.tar.gz: 52faac6d69885248e29fb4292433786c73ba4d9f
3
+ metadata.gz: ea1f94799e8cd1ce83a24d9c91ce3ca5e84a9db2
4
+ data.tar.gz: b2b4a26d4f25ccaf5a3435d7a8c2436393472300
5
5
  SHA512:
6
- metadata.gz: 141b39d68b61dc72c1bcb12f87a192ffbc41d312649b88ccd6e996db19003f65e051ec41412fa16462f59bd30ac9555175fd682e7b3475a5ff9ecbe80db92c06
7
- data.tar.gz: b0ccddd30f0245895891cd39f4152c1a9a17aceb1e3c945b74a06057d4fc8c6cf084dd3ba73a28eb35cbbda4ffbf38503a7284503115271987eaa6a5612349dc
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.9.3-p194@lookup_by --create
1
+ rvm use 2.0.0-p0@lookup_by --create
data/.travis.yml CHANGED
@@ -1,6 +1,7 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
+ - 2.0.0-p0
4
5
  - rbx-19mode
5
6
  - ruby-head
6
7
  services:
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. It
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
- Add this line to your application's Gemfile:
34
+ ```
35
+ # in Gemfile
36
+ gem "lookup_by"
47
37
 
48
- gem "lookup_by"
38
+ $ bundle
39
+ ```
49
40
 
50
- And then execute:
41
+ Or install it manually:
51
42
 
52
- $ bundle
43
+ $ gem install lookup_by
53
44
 
54
- Or install it yourself:
45
+ # Usage / Configuration
55
46
 
56
- $ gem install lookup_by
47
+ ### ActiveRecord Plugin
48
+
49
+ LookupBy adds 2 macro methods to `ActiveRecord::Base`
57
50
 
58
- Usage / Configuration
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
- class Status < ActiveRecord::Base
64
- lookup_by :column
65
- end
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
- # Aliases the `:column` attribute to `:name`.
68
- Status.new(name: "paid")
72
+ # Aliases the lookup attribute to :name.
73
+ Status.new(name: "paid")
74
+ ```
69
75
 
70
76
  ### Associations / Foreign Keys
71
77
 
72
- class Order < ActiveRecord::Base
73
- lookup_for :status
74
- end
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
- order = Order.new(status: "paid")
92
+ ```ruby
93
+ order = Order.new(status: "paid")
79
94
 
80
- order.status
81
- => "paid"
95
+ order.status
96
+ => "paid"
82
97
 
83
- order.raw_status
84
- => <#Status id: 1, status: "paid">
98
+ # Access the lookup object
99
+ order.raw_status
100
+ => <#Status id: 1, status: "paid">
85
101
 
86
- # Access to the lookup value before type casting
87
- order.status_before_type_cast
88
- => "paid"
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
- class Order < ActiveRecord::Base
98
- lookup_for :status, symbolize: true
99
- end
114
+ ```ruby
115
+ class Order < ActiveRecord::Base
116
+ lookup_for :status, symbolize: true
117
+ end
100
118
 
101
- order = Order.new(status: "paid")
119
+ order = Order.new(status: "paid")
102
120
 
103
- order.status
104
- => :paid
121
+ order.status
122
+ => :paid
105
123
 
106
- order.status = :shipped
107
- => :shipped
124
+ order.status = :shipped
125
+ => :shipped
126
+ ```
108
127
 
109
128
  ### Strict
110
129
 
111
- # Raise
112
- # Default
113
- lookup_for :status
130
+ Do you want missing lookup values to raise an error?
114
131
 
115
- # this will raise a LookupBy::Error
116
- Order.status = "non-existent status"
132
+ ```ruby
133
+ # Raise
134
+ # Default
135
+ lookup_for :status
117
136
 
118
- # Error
119
- lookup_for :status, strict: false
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
- # No caching - Not very useful
124
- # Default
125
- lookup_by :column_name
146
+ ```ruby
147
+ # No caching - Not very useful
148
+ # Default
149
+ lookup_by :column_name
126
150
 
127
- # Cache all
128
- # Use for a small finite list (e.g. status codes, US states)
129
- #
130
- # find: false DEFAULT
131
- lookup_by :column_name, cache: true
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
- # Cache N (with LRU eviction)
134
- # Use for a large list with uneven distribution (e.g. email domain, city)
135
- #
136
- # find: true DEFAULT and REQUIRED
137
- lookup_by :column_name, cache: 50
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
- # Return nil
142
- # Default when caching all records
143
- #
144
- # Skips the database for these methods:
145
- # .all, .count, .pluck
146
- lookup_by :column_name, cache: true
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
- # Find (read-through)
149
- # Required when caching N records
150
- lookup_by :column_name, cache: 10
151
- lookup_by :column_name, cache: true, find: true
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
- # Return nil
156
- # Default
157
- lookup_by :column_name
182
+ ```ruby
183
+ # Return nil
184
+ # Default
185
+ lookup_by :column_name
158
186
 
159
- # Find or create
160
- # Useful for user-submitted fields that grow over time
161
- # e.g. user_agents, ip_addresses
162
- #
163
- # Note: Only works if its attributes are nullable
164
- lookup_by :column_name, cache: 20, find_or_create: true
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
- # Normalize
169
- # Run through the your attribute's setter
170
- lookup_by :column_name, normalize: true
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
- LookupBy comes with a few cucumber steps. To use, `require` them
178
- from one of the ruby files under `features/support` (e.g. `env.rb`)
179
-
180
- require 'lookup_by/cucumber'
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
- = simple_form_for @order do |f|
187
- = f.input :status
188
- = f.input :status, :as => :radio_buttons
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
- = semantic_form_for @order do |f|
193
- = f.input :status
194
- = f.input :status, :as => :radio
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
 
@@ -42,7 +42,7 @@ module LookupBy
42
42
  def reload
43
43
  return unless cache_all?
44
44
 
45
- cache.clear
45
+ clear
46
46
 
47
47
  ::ActiveRecord::Base.connection.send :log, "", "#{klass.name} Load Cache All" do
48
48
  klass.order(order).each do |i|
@@ -20,7 +20,7 @@ module LookupBy
20
20
 
21
21
  class << self; attr_reader :lookup; end
22
22
 
23
- # validates field, presence: true, uniqueness: true
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
@@ -1,3 +1,3 @@
1
1
  module LookupBy
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
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.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