module Sequel::Plugins::StaticCache::ClassMethods

Attributes

cache[R]

A frozen ruby hash holding all of the model's frozen instances, keyed by frozen primary key.

Public Instance Methods

all(&block) click to toggle source

An array of all of the model's instances, without issuing a database query. If a block is given, yields each instance to the block.

# File lib/sequel/plugins/static_cache.rb, line 77
def all(&block)
  array = @static_cache_frozen ? @all.dup : to_a
  array.each(&block) if block
  array
end
as_hash(key_column = nil, value_column = nil, opts = OPTS) click to toggle source

Use the cache instead of a query to get the results.

# File lib/sequel/plugins/static_cache.rb, line 144
def as_hash(key_column = nil, value_column = nil, opts = OPTS)
  if key_column.nil? && value_column.nil?
    if @static_cache_frozen && !opts[:hash]
      return Hash[cache]
    else
      key_column = primary_key
    end
  end

  h = opts[:hash] || {}
  if value_column
    if value_column.is_a?(Array)
      if key_column.is_a?(Array)
        @all.each{|r| h[r.values.values_at(*key_column)] = r.values.values_at(*value_column)}
      else
        @all.each{|r| h[r[key_column]] = r.values.values_at(*value_column)}
      end
    else
      if key_column.is_a?(Array)
        @all.each{|r| h[r.values.values_at(*key_column)] = r[value_column]}
      else
        @all.each{|r| h[r[key_column]] = r[value_column]}
      end
    end
  elsif key_column.is_a?(Array)
    @all.each{|r| h[r.values.values_at(*key_column)] = static_cache_object(r)}
  else
    @all.each{|r| h[r[key_column]] = static_cache_object(r)}
  end
  h
end
cache_get_pk(pk) click to toggle source

Return the frozen object with the given pk, or nil if no such object exists in the cache, without issuing a database query.

# File lib/sequel/plugins/static_cache.rb, line 108
def cache_get_pk(pk)
  static_cache_object(cache[pk])
end
count(*a, &block) click to toggle source

Get the number of records in the cache, without issuing a database query.

Calls superclass method
# File lib/sequel/plugins/static_cache.rb, line 98
def count(*a, &block)
  if a.empty? && !block
    @all.size
  else
    super
  end
end
each() { |static_cache_object(o)| ... } click to toggle source

Yield each of the model's frozen instances to the block, without issuing a database query.

# File lib/sequel/plugins/static_cache.rb, line 114
def each(&block)
  if @static_cache_frozen
    @all.each(&block)
  else
    @all.each{|o| yield(static_cache_object(o))}
  end
end
first(*args) click to toggle source

If a block is given, multiple arguments are given, or a single non-Integer argument is given, performs the default behavior of issuing a database query. Otherwise, uses the cached values to return either the first cached instance (no arguments) or an array containing the number of instances specified (single integer argument).

Calls superclass method
# File lib/sequel/plugins/static_cache.rb, line 89
def first(*args)
  if block_given? || args.length > 1 || (args.length == 1 && !args[0].is_a?(Integer))
    super
  else
    @all.first(*args)
  end
end
load_cache() click to toggle source

Reload the cache for this model by retrieving all of the instances in the dataset freezing them, and populating the cached array and hash.

# File lib/sequel/plugins/static_cache.rb, line 213
def load_cache
  a = dataset.all
  h = {}
  a.each do |o|
    o.errors.freeze
    h[o.pk.freeze] = o.freeze
  end
  @all = a.freeze
  @cache = h.freeze
end
map(column=nil) { |static_cache_object(o)| ... } click to toggle source

Use the cache instead of a query to get the results.

# File lib/sequel/plugins/static_cache.rb, line 123
def map(column=nil, &block)
  if column
    raise(Error, "Cannot provide both column and block to map") if block
    if column.is_a?(Array)
      @all.map{|r| r.values.values_at(*column)}
    else
      @all.map{|r| r[column]}
    end
  elsif @static_cache_frozen
    @all.map(&block)
  elsif block
    @all.map{|o| yield(static_cache_object(o))}
  else
    all.map
  end
end
static_cache_allow_modifications?() click to toggle source

Ask whether modifications to this class are allowed.

# File lib/sequel/plugins/static_cache.rb, line 207
def static_cache_allow_modifications?
  !@static_cache_frozen
end
to_hash(*a) click to toggle source

Alias of #as_hash for backwards compatibility.

# File lib/sequel/plugins/static_cache.rb, line 177
def to_hash(*a)
  as_hash(*a)
end
to_hash_groups(key_column, value_column = nil, opts = OPTS) click to toggle source

Use the cache instead of a query to get the results

# File lib/sequel/plugins/static_cache.rb, line 182
def to_hash_groups(key_column, value_column = nil, opts = OPTS)
  h = opts[:hash] || {}
  if value_column
    if value_column.is_a?(Array)
      if key_column.is_a?(Array)
        @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r.values.values_at(*value_column)}
      else
        @all.each{|r| (h[r[key_column]] ||= []) << r.values.values_at(*value_column)}
      end
    else
      if key_column.is_a?(Array)
        @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r[value_column]}
      else
        @all.each{|r| (h[r[key_column]] ||= []) << r[value_column]}
      end
    end
  elsif key_column.is_a?(Array)
    @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << static_cache_object(r)}
  else
    @all.each{|r| (h[r[key_column]] ||= []) << static_cache_object(r)}
  end
  h
end

Private Instance Methods

primary_key_lookup(pk) click to toggle source

Return the frozen object with the given pk, or nil if no such object exists in the cache, without issuing a database query.

# File lib/sequel/plugins/static_cache.rb, line 228
def primary_key_lookup(pk)
  static_cache_object(cache[pk])
end
static_cache_object(o) click to toggle source

If frozen: false is not used, just return the argument. Otherwise, create a new instance with the arguments values if the argument is not nil.

# File lib/sequel/plugins/static_cache.rb, line 235
def static_cache_object(o)
  if @static_cache_frozen
    o
  elsif o
    call(Hash[o.values])
  end
end