module ActiveCash::Utils
Public Instance Methods
build_cache_opts(type, opts, cache_opts, klass)
click to toggle source
# File lib/active_cash/utils.rb, line 165 def build_cache_opts(type, opts, cache_opts, klass) #add procs/lambdas for better finds raise_unknown_cache_type(type) unless type.to_sym == :existence cache_opts ||= {} name = opts[:name] || opts[:as] || type.to_sym raise_redefined_cache_error(name) unless cache_opts[name].nil? cache_opts[name] = {} cache_opts[name][:name] = name cache_opts[name][:type] = type.to_sym cache_opts[name][:find_by] = opts[:find_by] cache_opts[name][:returns] = opts[:returns] || :nil cache_opts[name][:update_on] = opts[:update_on] || [:create, :update, :destroy] cache_opts[name][:klass] = klass return cache_opts end
create_methods(model, opts)
click to toggle source
# File lib/active_cash/utils.rb, line 78 def create_methods(model, opts) #raise error when arg not given model.send(:define_singleton_method, "cached_#{opts[:name]}_by") do |args = {}| xor_array = ((args.keys | opts[:find_by]) - (args.keys & opts[:find_by])) if xor_array.empty? return ActiveCash::Cache.exists?( find_by: opts[:find_by].inject({}) {|h, arg| h[arg] = args[arg]; h}, method_name: opts[:name], klass: opts[:klass], returns: opts[:returns] ) else ActiveCash::Utils.raise_find_by_options_mismatch(xor_array, opts[:find_by]) end end model.send(:define_singleton_method, "delete_cached_#{opts[:name]}_by") do |args = {}| return ActiveCash::Cache.delete( find_by: opts[:find_by].inject({}) {|h, arg| h[arg] = args[arg]; h}, method_name: opts[:name], klass: opts[:klass] ) end end
extract_args(opts, instance)
click to toggle source
fix this
# File lib/active_cash/utils.rb, line 126 def extract_args(opts, instance) { find_by: opts[:find_by].inject({}){|h, arg| h[arg] = instance.send(arg); h }, method_name: opts[:name], klass: instance.class.to_s } end
extract_instance_args(opts, instance)
click to toggle source
and this
# File lib/active_cash/utils.rb, line 137 def extract_instance_args(opts, instance) { find_by: opts[:find_by].inject({}){|h, arg| h[arg] = instance.send(arg); h }, method_name: opts[:name], instance: instance } end
extract_instance_old_args(opts, instance)
click to toggle source
and this
# File lib/active_cash/utils.rb, line 148 def extract_instance_old_args(opts, instance) changes = OpenStruct.new(instance.previous_changes) { find_by: opts[:find_by].inject({}){|h, arg| if changes.arg != nil h[arg] = changes.send(arg).first else h[arg] = instance.send(arg) end h }, method_name: opts[:name], klass: instance.class } end
raise_find_by_options_mismatch(invalid, valid)
click to toggle source
# File lib/active_cash/utils.rb, line 102 def raise_find_by_options_mismatch(invalid, valid) if (invalid & valid).any? raise(FindByOptionsMismatchError, "Missing find_by options: #{invalid}") else raise(FindByOptionsMismatchError, "Not registerd find_by options: #{invalid}") end end
raise_redefined_cache_error(name)
click to toggle source
# File lib/active_cash/utils.rb, line 118 def raise_redefined_cache_error(name) raise( RedefinedCacheError, "#{name} cache already exists" ) end
raise_unknown_cache_type(type)
click to toggle source
# File lib/active_cash/utils.rb, line 111 def raise_unknown_cache_type(type) raise( UnknownCacheTypeError, "Unknown cache type #{type}. Valid options are: :existence" ) end
set_callbacks(model, opts)
click to toggle source
# File lib/active_cash/utils.rb, line 4 def set_callbacks(model, opts) if (opts[:update_on] & [:create]).blank? model.send(:after_commit, on: [:create]) do ActiveCash::Cache.instance_update_if_exists( ActiveCash::Utils.extract_instance_args(opts, self).merge( returns: self.try(opts[:returns]) ) ) end else model.send(:after_commit, on: [:create]) do ActiveCash::Cache.instance_update( ActiveCash::Utils.extract_instance_args(opts, self).merge( returns: self.try(opts[:returns]) ) ) end end if (opts[:update_on] & [:update]).blank? model.send(:after_commit, on: [:update]) do if ActiveCash::Cache.instance_updated?( find_by: opts[:find_by], instance: self, returns: opts[:returns] ) ActiveCash::Cache.delete( ActiveCash::Utils.extract_instance_old_args(opts, self).merge( returns: self.try(opts[:returns]) ) ) ActiveCash::Cache.instance_update_if_exists( ActiveCash::Utils.extract_instance_args(opts, self).merge( returns: self.try(opts[:returns]) ) ) end end else model.send(:after_commit, on: [:update]) do if ActiveCash::Cache.instance_updated?( find_by: opts[:find_by], instance: self, returns: opts[:returns] ) ActiveCash::Cache.delete( ActiveCash::Utils.extract_instance_old_args(opts, self).merge( returns: self.try(opts[:returns]) ) ) ActiveCash::Cache.instance_update( ActiveCash::Utils.extract_instance_args(opts, self).merge( returns: self.try(opts[:returns]) ) ) end end end if (opts[:update_on] & [:destroy]).blank? model.send(:after_commit, on: [:destroy]) do ActiveCash::Cache.delete( ActiveCash::Utils.extract_args(opts, self).merge( returns: self.try(opts[:returns]) ) ) end else model.send(:after_commit, on: [:destroy]) do ActiveCash::Cache.set_false( ActiveCash::Utils.extract_args(opts, self).merge( returns: self.try(opts[:returns]) ) ) end end end