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
80 def all(&block)
81   array = @static_cache_frozen ? @all.dup : to_a
82   array.each(&block) if block
83   array
84 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
147 def as_hash(key_column = nil, value_column = nil, opts = OPTS)
148   if key_column.nil? && value_column.nil?
149     if @static_cache_frozen && !opts[:hash]
150       return Hash[cache]
151     else
152       key_column = primary_key
153     end
154   end
155 
156   h = opts[:hash] || {}
157   if value_column
158     if value_column.is_a?(Array)
159       if key_column.is_a?(Array)
160         @all.each{|r| h[r.values.values_at(*key_column)] = r.values.values_at(*value_column)}
161       else
162         @all.each{|r| h[r[key_column]] = r.values.values_at(*value_column)}
163       end
164     else
165       if key_column.is_a?(Array)
166         @all.each{|r| h[r.values.values_at(*key_column)] = r[value_column]}
167       else
168         @all.each{|r| h[r[key_column]] = r[value_column]}
169       end
170     end
171   elsif key_column.is_a?(Array)
172     @all.each{|r| h[r.values.values_at(*key_column)] = static_cache_object(r)}
173   else
174     @all.each{|r| h[r[key_column]] = static_cache_object(r)}
175   end
176   h
177 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
111 def cache_get_pk(pk)
112   static_cache_object(cache[pk])
113 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
101 def count(*a, &block)
102   if a.empty? && !block
103     @all.size
104   else
105     super
106   end
107 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
117 def each(&block)
118   if @static_cache_frozen
119     @all.each(&block)
120   else
121     @all.each{|o| yield(static_cache_object(o))}
122   end
123 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
92 def first(*args)
93   if defined?(yield) || args.length > 1 || (args.length == 1 && !args[0].is_a?(Integer))
94     super
95   else
96     @all.first(*args)
97   end
98 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
216 def load_cache
217   @all = load_static_cache_rows
218   h = {}
219   @all.each do |o|
220     o.errors.freeze
221     h[o.pk.freeze] = o.freeze
222   end
223   @cache = h.freeze
224 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
126 def map(column=nil, &block)
127   if column
128     raise(Error, "Cannot provide both column and block to map") if block
129     if column.is_a?(Array)
130       @all.map{|r| r.values.values_at(*column)}
131     else
132       @all.map{|r| r[column]}
133     end
134   elsif @static_cache_frozen
135     @all.map(&block)
136   elsif block
137     @all.map{|o| yield(static_cache_object(o))}
138   else
139     all.map
140   end
141 end
static_cache_allow_modifications?() click to toggle source

Ask whether modifications to this class are allowed.

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

Alias of as_hash for backwards compatibility.

    # File lib/sequel/plugins/static_cache.rb
180 def to_hash(*a)
181   as_hash(*a)
182 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
185 def to_hash_groups(key_column, value_column = nil, opts = OPTS)
186   h = opts[:hash] || {}
187   if value_column
188     if value_column.is_a?(Array)
189       if key_column.is_a?(Array)
190         @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r.values.values_at(*value_column)}
191       else
192         @all.each{|r| (h[r[key_column]] ||= []) << r.values.values_at(*value_column)}
193       end
194     else
195       if key_column.is_a?(Array)
196         @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r[value_column]}
197       else
198         @all.each{|r| (h[r[key_column]] ||= []) << r[value_column]}
199       end
200     end
201   elsif key_column.is_a?(Array)
202     @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << static_cache_object(r)}
203   else
204     @all.each{|r| (h[r[key_column]] ||= []) << static_cache_object(r)}
205   end
206   h
207 end

Private Instance Methods

load_static_cache_rows() click to toggle source

Load the static cache rows from the database.

Calls superclass method
    # File lib/sequel/plugins/static_cache.rb
229 def load_static_cache_rows
230   ret = super if defined?(super)
231   ret || dataset.all.freeze
232 end
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
236 def primary_key_lookup(pk)
237   static_cache_object(cache[pk])
238 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
243 def static_cache_object(o)
244   if @static_cache_frozen
245     o
246   elsif o
247     call(Hash[o.values])
248   end
249 end