module Sequel::Plugins::StaticCache::ClassMethods

  1. lib/sequel/plugins/static_cache.rb
Parent: StaticCache

Attributes

cache [R]

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

Public Instance methods

all ()

An array of all of the model’s frozen instances, without issuing a database query.

[show source]
# File lib/sequel/plugins/static_cache.rb, line 53
def all
  if @static_cache_frozen
    @all.dup
  else
    map{|o| o}
  end
end
cache_get_pk (pk)

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

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

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

[show source]
# File lib/sequel/plugins/static_cache.rb, line 62
def count(*a, &block)
  if a.empty? && !block
    @all.size
  else
    super
  end
end
each (&block)

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

[show source]
# File lib/sequel/plugins/static_cache.rb, line 78
def each(&block)
  if @static_cache_frozen
    @all.each(&block)
  else
    @all.each{|o| yield(static_cache_object(o))}
  end
end
map (column=nil, &block)

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

[show source]
# File lib/sequel/plugins/static_cache.rb, line 87
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? ()

Ask whether modifications to this class are allowed.

[show source]
# File lib/sequel/plugins/static_cache.rb, line 166
def static_cache_allow_modifications?
  !@static_cache_frozen
end
to_hash (key_column = nil, value_column = nil)

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

[show source]
# File lib/sequel/plugins/static_cache.rb, line 108
def to_hash(key_column = nil, value_column = nil)
if key_column.nil? && value_column.nil?
  if @static_cache_frozen
    return cache.dup
  else
    key_column = primary_key
  end
end

h = {}
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
to_hash_groups (key_column, value_column = nil)

Use the cache instead of a query to get the results

[show source]
# File lib/sequel/plugins/static_cache.rb, line 141
def to_hash_groups(key_column, value_column = nil)
  h = {}
  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