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 34
def all
  @all.dup
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 49
def cache_get_pk(pk)
  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 39
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 55
def each(&block)
  @all.each(&block)
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 60
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
  else
    @all.map(&(Proc.new if block_given?))
  end
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 76
def to_hash(key_column = nil, value_column = nil)
return cache.dup if key_column.nil? && value_column.nil?

h = {}
if value_column
  if value_column.is_a?(Array)
    if key_column.is_a?(Array)
      each{|r| h[r.values.values_at(*key_column)] = r.values.values_at(*value_column)}
    else
      each{|r| h[r[key_column]] = r.values.values_at(*value_column)}
    end
  else
    if key_column.is_a?(Array)
      each{|r| h[r.values.values_at(*key_column)] = r[value_column]}
    else
      each{|r| h[r[key_column]] = r[value_column]}
    end
  end
elsif key_column.is_a?(Array)
  each{|r| h[r.values.values_at(*key_column)] = r}
else
  each{|r| h[r[key_column]] = 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 103
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)
        each{|r| (h[r.values.values_at(*key_column)] ||= []) << r.values.values_at(*value_column)}
      else
        each{|r| (h[r[key_column]] ||= []) << r.values.values_at(*value_column)}
      end
    else
      if key_column.is_a?(Array)
        each{|r| (h[r.values.values_at(*key_column)] ||= []) << r[value_column]}
      else
        each{|r| (h[r[key_column]] ||= []) << r[value_column]}
      end
    end
  elsif key_column.is_a?(Array)
    each{|r| (h[r.values.values_at(*key_column)] ||= []) << r}
  else
    each{|r| (h[r[key_column]] ||= []) << r}
  end
  h
end