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
77 def all(&block)
78   array = @static_cache_frozen ? @all.dup : to_a
79   array.each(&block) if block
80   array
81 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
144 def as_hash(key_column = nil, value_column = nil, opts = OPTS)
145   if key_column.nil? && value_column.nil?
146     if @static_cache_frozen && !opts[:hash]
147       return Hash[cache]
148     else
149       key_column = primary_key
150     end
151   end
152 
153   h = opts[:hash] || {}
154   if value_column
155     if value_column.is_a?(Array)
156       if key_column.is_a?(Array)
157         @all.each{|r| h[r.values.values_at(*key_column)] = r.values.values_at(*value_column)}
158       else
159         @all.each{|r| h[r[key_column]] = r.values.values_at(*value_column)}
160       end
161     else
162       if key_column.is_a?(Array)
163         @all.each{|r| h[r.values.values_at(*key_column)] = r[value_column]}
164       else
165         @all.each{|r| h[r[key_column]] = r[value_column]}
166       end
167     end
168   elsif key_column.is_a?(Array)
169     @all.each{|r| h[r.values.values_at(*key_column)] = static_cache_object(r)}
170   else
171     @all.each{|r| h[r[key_column]] = static_cache_object(r)}
172   end
173   h
174 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
108 def cache_get_pk(pk)
109   static_cache_object(cache[pk])
110 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
 98 def count(*a, &block)
 99   if a.empty? && !block
100     @all.size
101   else
102     super
103   end
104 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
114 def each(&block)
115   if @static_cache_frozen
116     @all.each(&block)
117   else
118     @all.each{|o| yield(static_cache_object(o))}
119   end
120 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
89 def first(*args)
90   if block_given? || args.length > 1 || (args.length == 1 && !args[0].is_a?(Integer))
91     super
92   else
93     @all.first(*args)
94   end
95 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
213 def load_cache
214   @all = load_static_cache_rows
215   h = {}
216   @all.each do |o|
217     o.errors.freeze
218     h[o.pk.freeze] = o.freeze
219   end
220   @cache = h.freeze
221 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
123 def map(column=nil, &block)
124   if column
125     raise(Error, "Cannot provide both column and block to map") if block
126     if column.is_a?(Array)
127       @all.map{|r| r.values.values_at(*column)}
128     else
129       @all.map{|r| r[column]}
130     end
131   elsif @static_cache_frozen
132     @all.map(&block)
133   elsif block
134     @all.map{|o| yield(static_cache_object(o))}
135   else
136     all.map
137   end
138 end
static_cache_allow_modifications?() click to toggle source

Ask whether modifications to this class are allowed.

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

Alias of as_hash for backwards compatibility.

    # File lib/sequel/plugins/static_cache.rb
177 def to_hash(*a)
178   as_hash(*a)
179 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
182 def to_hash_groups(key_column, value_column = nil, opts = OPTS)
183   h = opts[:hash] || {}
184   if value_column
185     if value_column.is_a?(Array)
186       if key_column.is_a?(Array)
187         @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r.values.values_at(*value_column)}
188       else
189         @all.each{|r| (h[r[key_column]] ||= []) << r.values.values_at(*value_column)}
190       end
191     else
192       if key_column.is_a?(Array)
193         @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r[value_column]}
194       else
195         @all.each{|r| (h[r[key_column]] ||= []) << r[value_column]}
196       end
197     end
198   elsif key_column.is_a?(Array)
199     @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << static_cache_object(r)}
200   else
201     @all.each{|r| (h[r[key_column]] ||= []) << static_cache_object(r)}
202   end
203   h
204 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
226 def load_static_cache_rows
227   ret = super if defined?(super)
228   ret || dataset.all.freeze
229 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
233 def primary_key_lookup(pk)
234   static_cache_object(cache[pk])
235 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
240 def static_cache_object(o)
241   if @static_cache_frozen
242     o
243   elsif o
244     call(Hash[o.values])
245   end
246 end