Skip to content

Commit

Permalink
Remove BackendResetter
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed Nov 1, 2018
1 parent 047bb39 commit 4da766c
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 115 deletions.
1 change: 0 additions & 1 deletion lib/mobility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ module Mobility
require "mobility/attributes"
require "mobility/backend"
require "mobility/backends"
require "mobility/backend_resetter"
require "mobility/configuration"
require "mobility/fallbacks"
require "mobility/loaded"
Expand Down
26 changes: 0 additions & 26 deletions lib/mobility/active_model/backend_resetter.rb

This file was deleted.

26 changes: 0 additions & 26 deletions lib/mobility/active_record/backend_resetter.rb

This file was deleted.

50 changes: 0 additions & 50 deletions lib/mobility/backend_resetter.rb

This file was deleted.

8 changes: 4 additions & 4 deletions lib/mobility/backends/key_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ def translation_for(locale, **options)
end
end

def clear_cache
@cache = {}
end

private

def cache
@cache ||= {}
end

def clear_cache
@cache = {}
end
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/mobility/backends/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ def translation_for(locale, **options)
end
end

def clear_cache
model_cache && model_cache.clear
end

private

def cache
Expand All @@ -152,10 +156,6 @@ def cache
def model_cache
model.instance_variable_get(:"@__mobility_#{association_name}_cache")
end

def clear_cache
model_cache && model_cache.clear
end
end
end
end
Expand Down
52 changes: 49 additions & 3 deletions lib/mobility/plugins/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module Cache
included_hook do |model_class, backend_class|
if options[:cache]
backend_class.include(BackendMethods) unless backend_class.apply_plugin(:cache)
model_class.include BackendResetter.for(model_class).new(names) { clear_cache }
CacheResetter.reset(model_class, names)
end
end

Expand Down Expand Up @@ -54,14 +54,60 @@ def write(locale, value, **options)
end
# @!endgroup

def clear_cache
@cache = {}
end

private

def cache
@cache ||= {}
end
end

def clear_cache
@cache = {}
module CacheResetter
def self.reset(klass, names)
if Loaded::ActiveRecord && klass < ::ActiveRecord::Base
klass.include ActiveRecordResetter.new(names)
elsif Loaded::ActiveRecord && klass.ancestors.include?(::ActiveModel::Dirty)
klass.include ActiveModelResetter.new(names)
elsif Loaded::Sequel && klass < ::Sequel::Model
klass.include SequelResetter.new(names)
end
end
end

class ActiveModelResetter < Module
# @param [Array<String>] attribute_names Names of attributes whose backends should be reset
def initialize(names)
%i[changes_applied clear_changes_information].each do |method|
define_method method do
super()
names.each { |name| mobility_backends[name].clear_cache }
end
end
end
end

class ActiveRecordResetter < ActiveModelResetter
def initialize(names)
super

define_method :reload do |*args|
super(*args).tap do
names.each { |name| mobility_backends[name].clear_cache }
end
end
end
end

class SequelResetter < Module
def initialize(names)
define_method :refresh do
super().tap do
names.each { |name| mobility_backends[name].clear_cache }
end
end
end
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/mobility/plugins/dirty.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# frozen_string_literal: true
require "mobility/backend_resetter"
require "mobility/plugins/fallthrough_accessors"

module Mobility
Expand Down

0 comments on commit 4da766c

Please sign in to comment.