Sha256: f03e3e7a4d958ba9a6af509322b92ba908bfbdb5a9c67ff0bf1d90172f34c7cd

Contents?: true

Size: 1.9 KB

Versions: 4

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true
require 'active_record'
require 'active_support'

require 'kasket/version'

module Kasket
  autoload :ReadMixin,              'kasket/read_mixin'
  autoload :WriteMixin,             'kasket/write_mixin'
  autoload :DirtyMixin,             'kasket/dirty_mixin'
  autoload :QueryParser,            'kasket/query_parser'
  autoload :ConfigurationMixin,     'kasket/configuration_mixin'
  autoload :Query,                  'kasket/query'
  autoload :Visitor,                'kasket/visitor'
  autoload :SelectManagerMixin,     'kasket/select_manager_mixin'
  autoload :RelationMixin,          'kasket/relation_mixin'

  CONFIGURATION = { max_collection_size: 100, write_through: false } # rubocop:disable Style/MutableConstant

  module_function

  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]

    ActiveRecord::Base.extend(Kasket::ConfigurationMixin)

    if defined?(ActiveRecord::Relation)
      ActiveRecord::Relation.send(:include, Kasket::RelationMixin)
      Arel::SelectManager.send(:include, Kasket::SelectManagerMixin)
    end
  end

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

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

  # Keys are the records being saved.
  # Values are either the saved record, or nil if the record has been destroyed.
  def self.pending_records
    Thread.current[:kasket_pending_records]
  end

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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
kasket-4.5.1 lib/kasket.rb
kasket-4.5.0 lib/kasket.rb
kasket-4.4.5 lib/kasket.rb
kasket-4.4.4 lib/kasket.rb