module Sequel::Plugins::SubsetStaticCache::CachedDatasetMethods

  1. lib/sequel/plugins/subset_static_cache.rb

Public Instance methods

all(&block)

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

[show source]
    # File lib/sequel/plugins/subset_static_cache.rb
130 def all(&block)
131   return super unless all = @cache[:subset_static_cache_all]
132 
133   array = all.dup
134   array.each(&block) if block
135   array
136 end
as_hash(key_column = nil, value_column = nil, opts = OPTS)

Use the cache instead of a query to get the results if possible

[show source]
    # File lib/sequel/plugins/subset_static_cache.rb
195 def as_hash(key_column = nil, value_column = nil, opts = OPTS)
196   return super unless all = @cache[:subset_static_cache_all]
197 
198   if key_column.nil? && value_column.nil?
199     if opts[:hash]
200       key_column = model.primary_key
201     else
202       return Hash[@cache[:subset_static_cache_map]]
203     end
204   end
205 
206   h = opts[:hash] || {}
207   if value_column
208     if value_column.is_a?(Array)
209       if key_column.is_a?(Array)
210         all.each{|r| h[r.values.values_at(*key_column)] = r.values.values_at(*value_column)}
211       else
212         all.each{|r| h[r[key_column]] = r.values.values_at(*value_column)}
213       end
214     else
215       if key_column.is_a?(Array)
216         all.each{|r| h[r.values.values_at(*key_column)] = r[value_column]}
217       else
218         all.each{|r| h[r[key_column]] = r[value_column]}
219       end
220     end
221   elsif key_column.is_a?(Array)
222     all.each{|r| h[r.values.values_at(*key_column)] = r}
223   else
224     all.each{|r| h[r[key_column]] = r}
225   end
226   h
227 end
count(*a, &block)

Get the number of records in the cache, without issuing a database query, if no arguments or block are provided.

[show source]
    # File lib/sequel/plugins/subset_static_cache.rb
140 def count(*a, &block)
141   if a.empty? && !block && (all = @cache[:subset_static_cache_all])
142     all.size
143   else
144     super
145   end
146 end
each(&block)

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

[show source]
    # File lib/sequel/plugins/subset_static_cache.rb
174 def each(&block)
175   return super unless all = @cache[:subset_static_cache_all]
176   all.each(&block)
177 end
first(*args)

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).

[show source]
    # File lib/sequel/plugins/subset_static_cache.rb
154 def first(*args)
155   if !defined?(yield) && args.length <= 1 && (args.length == 0 || args[0].is_a?(Integer)) && (all = @cache[:subset_static_cache_all])
156     all.first(*args)
157   else
158     super
159   end
160 end
map(column=nil, &block)

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

[show source]
    # File lib/sequel/plugins/subset_static_cache.rb
180 def map(column=nil, &block)
181   return super unless all = @cache[:subset_static_cache_all]
182   if column
183     raise(Error, "Cannot provide both column and block to map") if block
184     if column.is_a?(Array)
185       all.map{|r| r.values.values_at(*column)}
186     else
187       all.map{|r| r[column]}
188     end
189   else
190     all.map(&block)
191   end
192 end
to_hash(*a)

Alias of as_hash for backwards compatibility.

[show source]
    # File lib/sequel/plugins/subset_static_cache.rb
230 def to_hash(*a)
231   as_hash(*a)
232 end
to_hash_groups(key_column, value_column = nil, opts = OPTS)

Use the cache instead of a query to get the results

[show source]
    # File lib/sequel/plugins/subset_static_cache.rb
235 def to_hash_groups(key_column, value_column = nil, opts = OPTS)
236   return super unless all = @cache[:subset_static_cache_all]
237 
238   h = opts[:hash] || {}
239   if value_column
240     if value_column.is_a?(Array)
241       if key_column.is_a?(Array)
242         all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r.values.values_at(*value_column)}
243       else
244         all.each{|r| (h[r[key_column]] ||= []) << r.values.values_at(*value_column)}
245       end
246     else
247       if key_column.is_a?(Array)
248         all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r[value_column]}
249       else
250         all.each{|r| (h[r[key_column]] ||= []) << r[value_column]}
251       end
252     end
253   elsif key_column.is_a?(Array)
254     all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r}
255   else
256     all.each{|r| (h[r[key_column]] ||= []) << r}
257   end
258   h
259 end
with_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/subset_static_cache.rb
164 def with_pk(pk)
165   if cache = @cache[:subset_static_cache_map]
166     cache[pk]
167   else
168     super
169   end
170 end