lookup_by 0.10.9 → 0.11.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/.ruby-version +1 -1
  3. data/.travis.yml +17 -9
  4. data/Appraisals +4 -4
  5. data/Gemfile +3 -1
  6. data/README.md +11 -15
  7. data/Rakefile +20 -2
  8. data/circle.yml +4 -2
  9. data/gemfiles/rails_4.1.gemfile +1 -1
  10. data/gemfiles/rails_4.2.gemfile +1 -1
  11. data/gemfiles/{rails_4.0.gemfile → rails_5.0.gemfile} +2 -2
  12. data/lib/lookup_by.rb +2 -0
  13. data/lib/lookup_by/cache.rb +15 -20
  14. data/lib/lookup_by/caching/safe_lru.rb +1 -1
  15. data/lib/lookup_by/lookup.rb +26 -12
  16. data/lib/lookup_by/version.rb +1 -1
  17. data/lookup_by.gemspec +1 -1
  18. data/spec/association_spec.rb +1 -0
  19. data/spec/{dummy → internal}/app/models/account.rb +0 -0
  20. data/spec/{dummy → internal}/app/models/address.rb +0 -0
  21. data/spec/{dummy → internal}/app/models/city.rb +0 -0
  22. data/spec/{dummy → internal}/app/models/country.rb +0 -0
  23. data/spec/{dummy → internal}/app/models/email_address.rb +0 -0
  24. data/spec/{dummy → internal}/app/models/ip_address.rb +0 -0
  25. data/spec/{dummy → internal}/app/models/path.rb +0 -0
  26. data/spec/{dummy → internal}/app/models/phone_number.rb +0 -0
  27. data/spec/{dummy → internal}/app/models/postal_code.rb +0 -0
  28. data/spec/{dummy → internal}/app/models/raisin.rb +0 -0
  29. data/spec/{dummy → internal}/app/models/read_through_raisin.rb +0 -0
  30. data/spec/{dummy → internal}/app/models/state.rb +0 -0
  31. data/spec/{dummy → internal}/app/models/status.rb +0 -0
  32. data/spec/{dummy → internal}/app/models/street.rb +0 -0
  33. data/spec/{dummy → internal}/app/models/uncacheable.rb +0 -0
  34. data/spec/{dummy → internal}/app/models/unfindable.rb +0 -0
  35. data/spec/{dummy → internal}/app/models/unsynchronizable.rb +0 -0
  36. data/spec/{dummy → internal}/app/models/user_agent.rb +0 -0
  37. data/spec/internal/config/database.yml +8 -0
  38. data/spec/internal/db/schema.rb +48 -0
  39. data/spec/{dummy → internal}/lib/missing.rb +0 -0
  40. data/spec/internal/log/.gitignore +1 -0
  41. data/spec/lookup_by_spec.rb +19 -5
  42. data/spec/rails_helper.rb +5 -6
  43. data/spec/support/shared_examples_for_a_lookup.rb +28 -7
  44. metadata +50 -86
  45. data/spec/dummy/Rakefile +0 -22
  46. data/spec/dummy/app/models/.gitkeep +0 -0
  47. data/spec/dummy/bin/bundle +0 -3
  48. data/spec/dummy/bin/rails +0 -4
  49. data/spec/dummy/bin/rake +0 -4
  50. data/spec/dummy/config.ru +0 -4
  51. data/spec/dummy/config/application.rb +0 -28
  52. data/spec/dummy/config/boot.rb +0 -6
  53. data/spec/dummy/config/database.yml +0 -44
  54. data/spec/dummy/config/environment.rb +0 -5
  55. data/spec/dummy/config/environments/development.rb +0 -20
  56. data/spec/dummy/config/environments/test.rb +0 -19
  57. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
  58. data/spec/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  59. data/spec/dummy/config/initializers/inflections.rb +0 -16
  60. data/spec/dummy/config/locales/en.yml +0 -23
  61. data/spec/dummy/db/migrate/20121019040009_create_tables.rb +0 -41
  62. data/spec/dummy/db/seeds.rb +0 -1
  63. data/spec/dummy/db/structure.sql +0 -1053
  64. data/spec/dummy/log/.gitkeep +0 -0
  65. data/spec/dummy/script/rails +0 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 355a248ab95bea93cf3a8699d345152225be7c4f
4
- data.tar.gz: ce156dc5c175049034b04de0e4cee2ba4a080aea
3
+ metadata.gz: ecd1d5a836f7058b70e524c02a5ae22072ddcacf
4
+ data.tar.gz: 2cbc2fd59837ca832f04e17f20bdcf13326e2f8b
5
5
  SHA512:
6
- metadata.gz: 30288404d1e8b43cb885caaeb83a71ac6cb8d557fecce56cb54ee76cc5c2e9372cc1a525384032d411bf61b1c75b228cd1f1cd41cb11f6c3c7d8422be3378cdc
7
- data.tar.gz: 7186d2cfa8894e8f20ebd299eaf3f558f52efb5590b161859160fd8601e6f8381abfb3521f8cb39bc7c3243afed8f65922bcc1d06c0b5c3a12afebed2da9cbea
6
+ metadata.gz: 79b99b92afb52b979310de7202dec4ec171a21541606144fc36349c65db1a33b58afa1f870dff45dd61df51bff76cdc512f76ee5685742bb65281834b26e4587
7
+ data.tar.gz: 34468ee2bd864940cdd7e63cbc17a020a91f5afdfa6c561330e84092cd3239f34d43e19b614edd3503c53fab304aca43b66dead993a1ffe27ef78f86066fe895
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-2.3.0
1
+ ruby-2.3.3
data/.travis.yml CHANGED
@@ -1,16 +1,24 @@
1
1
  language: ruby
2
+
3
+ cache:
4
+ bundler: true
5
+
2
6
  rvm:
3
- - 1.9.3-p551
4
- - 2.0.0
5
- - 2.1.8
6
- - 2.2.4
7
- - 2.3.0
8
- - rbx-3.19
7
+ - 2.2.2
8
+ - 2.2.5
9
+ - 2.3.1
10
+
9
11
  services:
10
12
  - postgresql
11
- before_script:
12
- - psql -c 'create database lookup_by_test' -U postgres
13
+
14
+ # Travis defaults to PostgreSQL 9.1
15
+ addons:
16
+ postgresql: "9.4"
17
+
13
18
  script:
14
- - bundle exec rake app:db:test:prepare
15
19
  - bundle exec appraisal install
16
20
  - bundle exec appraisal rake
21
+
22
+ general:
23
+ artifacts:
24
+ - coverage
data/Appraisals CHANGED
@@ -1,7 +1,3 @@
1
- appraise 'rails-4.0' do
2
- gem 'rails', '~> 4.0.0'
3
- end
4
-
5
1
  appraise 'rails-4.1' do
6
2
  gem 'rails', '~> 4.1.0'
7
3
  end
@@ -9,3 +5,7 @@ end
9
5
  appraise 'rails-4.2' do
10
6
  gem 'rails', '~> 4.2.0'
11
7
  end
8
+
9
+ appraise 'rails-5.0' do
10
+ gem 'rails', '~> 5.0.0'
11
+ end
data/Gemfile CHANGED
@@ -8,12 +8,14 @@ gemspec
8
8
  group :development, :test do
9
9
  gem 'appraisal', '~> 2.1.0', require: false
10
10
 
11
+ gem 'combustion'
12
+
11
13
  gem 'rspec'
12
14
  gem 'rspec-its'
13
15
  gem 'rspec-rails'
14
16
 
15
17
  gem "pg", platform: :ruby
16
- gem "activerecord-jdbcpostgresql-adapter", platform: :jruby
18
+ # gem "activerecord-jdbcpostgresql-adapter", platform: :jruby
17
19
 
18
20
  gem "simplecov", require: false
19
21
  gem 'coveralls', require: false
data/README.md CHANGED
@@ -19,15 +19,15 @@
19
19
  LookupBy is a thread-safe lookup table cache for ActiveRecord that reduces normalization pains.
20
20
 
21
21
  * Configurable lookup column
22
- * Caching (read-through, write-through, least recently used (LRU))
22
+ * Caching (read-through, write-through, least-recently used (LRU))
23
23
  * Symbolized values
24
24
  * Normalized values, _e.g. canonicalizing UTF-8 before lookup_
25
25
 
26
26
  ### Dependencies
27
27
 
28
- * Rails 4.0+ (_tested on Rails 4.0, 4.1, and 4.2_)
29
- * Ruby 1.9.3+ (_tested on Ruby 1.9.3, 2.0, 2.1, 2.2, 2.3 and Rubinius 3.19_)
30
- * PostgreSQL
28
+ * Rails 4.1+ (_tested on Rails 4.1, 4.2, and 5.0_)
29
+ * Ruby 2.2+ (_tested on Ruby 2.2, 2.3 and Rubinius 3.45_)
30
+ * PostgreSQL 9.2+
31
31
 
32
32
  ### Development
33
33
 
@@ -41,7 +41,7 @@ LookupBy is a thread-safe lookup table cache for ActiveRecord that reduces norma
41
41
 
42
42
  Please create [Issues][] to submit bug reports and feature requests. However, I ask that you'd kindly review [these bug reporting guidelines](https://github.com/companygardener/lookup_by/wiki/Bug-Reports) first.
43
43
 
44
- _If you find a security bug, **_do not_** use the public issue tracker. Instead, send an email to: thecompanygardener@gmail.com._
44
+ _If you find a security bug, **do not** use the public issue tracker. Instead, send an email to: thecompanygardener@gmail.com._
45
45
 
46
46
  # Installation
47
47
 
@@ -223,11 +223,10 @@ lookup_by :column_name
223
223
  # Cache all
224
224
  # Use for a small finite list (e.g. status codes, US states)
225
225
  #
226
- # Defaults to no read-through
227
- # options[:find] = false
226
+ # Defaults to no read-through, e.g. options[:find] = false
228
227
  lookup_by :column_name, cache: true
229
228
 
230
- # Cache N (with LRU eviction)
229
+ # Cache N records, evicting the least-recently used (LRU)
231
230
  # Use for large sets with uneven distribution (e.g. email domain, city)
232
231
  #
233
232
  # Requires read-through
@@ -290,7 +289,7 @@ lookup_by :column_name, cache: true, find: true, raise: true
290
289
 
291
290
  ```ruby
292
291
  # Normalize
293
- # Run through the your attribute's setter
292
+ # Call the attribute's setter
294
293
  lookup_by :column_name, normalize: true
295
294
  ```
296
295
 
@@ -306,14 +305,11 @@ lookup_by :column_name, allow_blank: true
306
305
 
307
306
  ### Threadsafety
308
307
 
309
- Force the LRU to be threadsafe (used to test the SafeLRU).
310
-
311
- With Rails 4, the Safe LRU is selected automatically in production-like environments.
308
+ Disable threadsafety using the `:safe` option.
312
309
 
313
310
  ```ruby
314
- # Safe
315
- # Use threadsafe cache
316
- lookup_by :column_name, cache: 10, safe: true
311
+ # Default: true
312
+ lookup_by :column_name, cache: 10, safe: false
317
313
  ```
318
314
 
319
315
  # Integration
data/Rakefile CHANGED
@@ -5,12 +5,30 @@ rescue LoadError
5
5
  puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
6
  end
7
7
 
8
- APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
9
- load 'rails/tasks/engine.rake'
8
+ require 'combustion'
10
9
 
11
10
  Bundler::GemHelper.install_tasks
12
11
 
13
12
  require "rspec/core/rake_task"
14
13
  RSpec::Core::RakeTask.new(:spec)
15
14
 
15
+ desc 'Start a console'
16
+ task :console do
17
+ Combustion.initialize! :active_record
18
+
19
+ ARGV.clear
20
+
21
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
22
+
23
+ require 'pry'
24
+ Pry.start
25
+ end
26
+
27
+ namespace :db do
28
+ desc 'Setup db'
29
+ task :setup do
30
+ Combustion.initialize! :active_record
31
+ end
32
+ end
33
+
16
34
  task :default => :spec
data/circle.yml CHANGED
@@ -2,8 +2,10 @@ dependencies:
2
2
  post:
3
3
  - bundle exec appraisal install
4
4
 
5
+ database:
6
+ override:
7
+ - echo "Skipping default"
8
+
5
9
  test:
6
- pre:
7
- - bundle exec rake app:db:test:prepare
8
10
  override:
9
11
  - bundle exec appraisal rake
@@ -6,11 +6,11 @@ gem "rails", "~> 4.1.0"
6
6
 
7
7
  group :development, :test do
8
8
  gem "appraisal", "~> 2.1.0", :require => false
9
+ gem "combustion"
9
10
  gem "rspec"
10
11
  gem "rspec-its"
11
12
  gem "rspec-rails"
12
13
  gem "pg", :platform => :ruby
13
- gem "activerecord-jdbcpostgresql-adapter", :platform => :jruby
14
14
  gem "simplecov", :require => false
15
15
  gem "coveralls", :require => false
16
16
  gem "pry", :require => false
@@ -6,11 +6,11 @@ gem "rails", "~> 4.2.0"
6
6
 
7
7
  group :development, :test do
8
8
  gem "appraisal", "~> 2.1.0", :require => false
9
+ gem "combustion"
9
10
  gem "rspec"
10
11
  gem "rspec-its"
11
12
  gem "rspec-rails"
12
13
  gem "pg", :platform => :ruby
13
- gem "activerecord-jdbcpostgresql-adapter", :platform => :jruby
14
14
  gem "simplecov", :require => false
15
15
  gem "coveralls", :require => false
16
16
  gem "pry", :require => false
@@ -2,15 +2,15 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rails", "~> 4.0.0"
5
+ gem "rails", "~> 5.0.0"
6
6
 
7
7
  group :development, :test do
8
8
  gem "appraisal", "~> 2.1.0", :require => false
9
+ gem "combustion"
9
10
  gem "rspec"
10
11
  gem "rspec-its"
11
12
  gem "rspec-rails"
12
13
  gem "pg", :platform => :ruby
13
- gem "activerecord-jdbcpostgresql-adapter", :platform => :jruby
14
14
  gem "simplecov", :require => false
15
15
  gem "coveralls", :require => false
16
16
  gem "pry", :require => false
data/lib/lookup_by.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require "lookup_by/version"
2
2
  require "lookup_by/railtie" if defined? Rails
3
3
 
4
+ require 'active_support'
5
+
4
6
  module LookupBy
5
7
  class Error < StandardError; end
6
8
 
@@ -1,6 +1,7 @@
1
1
  module LookupBy
2
2
  class Cache
3
3
  attr_reader :cache, :field, :stats
4
+
4
5
  attr_accessor :testing
5
6
 
6
7
  def initialize(klass, options = {})
@@ -18,7 +19,7 @@ module LookupBy
18
19
  @raise_on_miss = options[:raise] || false
19
20
  @testing = false
20
21
  @enabled = true
21
- @safe = options[:safe] || concurrent?
22
+ @safe = options.fetch(:safe, true)
22
23
  @mutex = Mutex.new if @safe
23
24
 
24
25
  @stats = { db: Hash.new(0), cache: Hash.new(0) }
@@ -51,36 +52,35 @@ module LookupBy
51
52
  end
52
53
  end
53
54
 
54
- def reload
55
+ def load
55
56
  return unless @type == :all
56
57
 
57
- clear
58
-
59
58
  ::ActiveRecord::Base.connection.send :log, "", "#{@klass.name} Load Cache All" do
60
- @klass.order(@order).each do |object|
59
+ @klass.order(@order).readonly.each do |object|
61
60
  cache_write(object)
62
61
  end
63
62
  end
64
63
  end
65
64
 
65
+ def reload
66
+ clear
67
+ load
68
+ end
69
+
66
70
  def clear
67
71
  @cache.clear
68
72
  end
69
73
 
70
74
  def create(*args, &block)
71
- created = @klass.create(*args, &block)
72
-
73
- cache_write(created) if cache?
74
-
75
- created
75
+ @klass.create(*args, &block).tap do |created|
76
+ cache_write(created) if cache?
77
+ end
76
78
  end
77
79
 
78
80
  def create!(*args, &block)
79
- created = @klass.create!(*args, &block)
80
-
81
- cache_write(created) if cache?
82
-
83
- created
81
+ @klass.create!(*args, &block).tap do |created|
82
+ cache_write(created) if cache?
83
+ end
84
84
  end
85
85
 
86
86
  def seed(*values)
@@ -154,11 +154,6 @@ module LookupBy
154
154
 
155
155
  private
156
156
 
157
- # RAILS_ENV=test will not use the SafeLRU
158
- def concurrent?
159
- Rails.configuration.cache_classes && Rails.configuration.eager_load
160
- end
161
-
162
157
  def primary_key?(value)
163
158
  case @primary_key_type
164
159
  when :integer
@@ -1,4 +1,4 @@
1
- # TODO: Evaluate using ThreadSafe::Hash and ThreadSafe::Array.
1
+ # @todo: Evaluate using ThreadSafe::Hash and ThreadSafe::Array.
2
2
 
3
3
  require "mutex_m"
4
4
 
@@ -55,7 +55,7 @@ module LookupBy
55
55
  end
56
56
 
57
57
  @lookup = Cache.new(self, options.merge(field: field))
58
- @lookup.reload
58
+ @lookup.load
59
59
  end
60
60
 
61
61
  LookupBy.register self
@@ -63,22 +63,36 @@ module LookupBy
63
63
  end
64
64
 
65
65
  module ClassMethods
66
- # TODO: Rails 4 needs to return a proxy object here
67
- def all(*args)
68
- return super if Rails::VERSION::MAJOR >= 4
66
+ # Rails 4.1, 4.2, 5.0
67
+ def all
68
+ return super if current_scope
69
+
69
70
  return super if @lookup.read_through?
71
+ return super if @lookup.cache.empty?
70
72
  return super if @lookup.disabled?
71
- return super if args.any?
72
73
 
73
- @lookup.cache.values
74
+ relation.tap do |rel|
75
+ rel.instance_variable_set(:@records, @lookup.cache.values)
76
+ rel.instance_variable_set(:@loaded, true)
77
+ end
74
78
  end
75
79
 
76
- def count(column_name = nil, options = {})
77
- return super if @lookup.read_through?
78
- return super if @lookup.disabled?
79
- return super if column_name
80
+ if Rails::VERSION::MAJOR <= 4
81
+ # Rails 4.1, 4.2
82
+ def count(column_name = nil, options = {})
83
+ return super if @lookup.read_through?
84
+ return super if column_name
80
85
 
81
- @lookup.cache.size
86
+ @lookup.cache.size
87
+ end
88
+ else
89
+ # Rails 5.0
90
+ def count(column_name = nil)
91
+ return super if @lookup.read_through?
92
+ return super if column_name
93
+
94
+ @lookup.cache.size
95
+ end
82
96
  end
83
97
 
84
98
  def pluck(*column_names)
@@ -103,7 +117,7 @@ module LookupBy
103
117
  when self then arg
104
118
  else raise TypeError, "#{name}[*args]: args must be at least one String, Symbol, Integer, IPAddr, nil, or #{name}"
105
119
  end
106
- else return args.map { |arg| self[arg] }
120
+ else return args.map { |arg| self[arg] }
107
121
  end
108
122
  end
109
123
 
@@ -1,3 +1,3 @@
1
1
  module LookupBy
2
- VERSION = "0.10.9"
2
+ VERSION = "0.11.1"
3
3
  end
data/lookup_by.gemspec CHANGED
@@ -22,7 +22,7 @@ Gem::Specification.new do |gem|
22
22
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
23
23
  gem.require_paths = ["lib"]
24
24
 
25
- gem.add_dependency "activerecord", ">= 4.0.0"
25
+ gem.add_dependency "activerecord", ">= 4.1.0"
26
26
 
27
27
  gem.add_development_dependency "bundler", ">= 1.7.0"
28
28
  gem.add_development_dependency "rake"
@@ -113,6 +113,7 @@ describe LookupBy::Association do
113
113
  end
114
114
  end
115
115
 
116
+ # spec/internal/lib/missing.rb
116
117
  context "Missing.lookup_for :city" do
117
118
  it "does not raise foreign key error when table hasn't been created" do
118
119
  expect { require "missing" }.to_not raise_error
File without changes
File without changes
File without changes
File without changes