Module: Kasket

Defined in:
lib/kasket.rb,
lib/kasket/events.rb,
lib/kasket/version.rb,
lib/kasket/visitor.rb,
lib/kasket/read_mixin.rb,
lib/kasket/dirty_mixin.rb,
lib/kasket/write_mixin.rb,
lib/kasket/query_parser.rb,
lib/kasket/relation_mixin.rb,
lib/kasket/configuration_mixin.rb,
lib/kasket/select_manager_mixin.rb

Defined Under Namespace

Modules: ConfigurationMixin, DirtyMixin, Events, ReadMixin, RelationMixin, SelectManagerMixin, WriteMixin Classes: QueryParser, Version, Visitor

Constant Summary collapse

CONFIGURATION =

rubocop:disable Style/MutableConstant

{ # rubocop:disable Style/MutableConstant
  max_collection_size: 100,
  write_through: false,
  default_expires_in: nil,
  events_callback: nil,
}
VERSION =
'4.14.1'

Class Method Summary collapse

Class Method Details

.add_pending_record(record, destroyed = false) ⇒ Object



71
72
73
74
# File 'lib/kasket.rb', line 71

def self.add_pending_record(record, destroyed = false)
  Thread.current[:kasket_pending_records] ||= {}
  Thread.current[:kasket_pending_records][record] = destroyed ? nil : record
end

.cache_storeObject Also known as: cache



56
57
58
# File 'lib/kasket.rb', line 56

def self.cache_store
  @cache_store ||= Rails.cache
end

.cache_store=(options) ⇒ Object



52
53
54
# File 'lib/kasket.rb', line 52

def self.cache_store=(options)
  @cache_store = ActiveSupport::Cache.lookup_store(options)
end

.clear_pending_recordsObject



76
77
78
# File 'lib/kasket.rb', line 76

def self.clear_pending_records
  Thread.current[:kasket_pending_records] = nil
end

.pending_recordsObject

Keys are the records being saved. Values are either the saved record, or nil if the record has been destroyed.



67
68
69
# File 'lib/kasket.rb', line 67

def self.pending_records
  Thread.current[:kasket_pending_records]
end

.setup(options = {}) ⇒ Object

Configure Kasket.

Parameters:

  • options (Hash) (defaults to: {})

    the configuration options for Kasket.

Options Hash (options):

  • :max_collection_size (Integer)

    max size limit for a cacheable collection of records.

  • :write_through (Boolean)
  • :default_expires_in (Integer, nil)

    the cache TTL.

  • :events_callback (#call)

    a callable object used to instrument Kasket operations. It is invoked with two arguments: the name of the event, as a String, and the Klass of the ActiveRecord model the event is about.



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/kasket.rb', line 39

def setup(options = {})
  return if ActiveRecord::Base.respond_to?(:has_kasket)

  CONFIGURATION[:max_collection_size] = options[:max_collection_size] if options[:max_collection_size]
  CONFIGURATION[:write_through]       = options[:write_through]       if options[:write_through]
  CONFIGURATION[:default_expires_in]  = options[:default_expires_in]  if options[:default_expires_in]
  CONFIGURATION[:events_callback]     = options[:events_callback]     if options[:events_callback]

  ActiveRecord::Base.extend(Kasket::ConfigurationMixin)
  ActiveRecord::Relation.include(Kasket::RelationMixin)
  Arel::SelectManager.include(Kasket::SelectManagerMixin)
end