Public Instance methods
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.
# File lib/sequel/plugins/subset_static_cache.rb 129 def all(&block) 130 return super unless all = @cache[:subset_static_cache_all] 131 132 array = all.dup 133 array.each(&block) if block 134 array 135 end
Use the cache instead of a query to get the results if possible
# File lib/sequel/plugins/subset_static_cache.rb 194 def as_hash(key_column = nil, value_column = nil, opts = OPTS) 195 return super unless all = @cache[:subset_static_cache_all] 196 197 if key_column.nil? && value_column.nil? 198 if opts[:hash] 199 key_column = model.primary_key 200 else 201 return Hash[@cache[:subset_static_cache_map]] 202 end 203 end 204 205 h = opts[:hash] || {} 206 if value_column 207 if value_column.is_a?(Array) 208 if key_column.is_a?(Array) 209 all.each{|r| h[r.values.values_at(*key_column)] = r.values.values_at(*value_column)} 210 else 211 all.each{|r| h[r[key_column]] = r.values.values_at(*value_column)} 212 end 213 else 214 if key_column.is_a?(Array) 215 all.each{|r| h[r.values.values_at(*key_column)] = r[value_column]} 216 else 217 all.each{|r| h[r[key_column]] = r[value_column]} 218 end 219 end 220 elsif key_column.is_a?(Array) 221 all.each{|r| h[r.values.values_at(*key_column)] = r} 222 else 223 all.each{|r| h[r[key_column]] = r} 224 end 225 h 226 end
Get the number of records in the cache, without issuing a database query, if no arguments or block are provided.
# File lib/sequel/plugins/subset_static_cache.rb 139 def count(*a, &block) 140 if a.empty? && !block && (all = @cache[:subset_static_cache_all]) 141 all.size 142 else 143 super 144 end 145 end
Yield each of the dataset’s frozen instances to the block, without issuing a database query.
# File lib/sequel/plugins/subset_static_cache.rb 173 def each(&block) 174 return super unless all = @cache[:subset_static_cache_all] 175 all.each(&block) 176 end
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).
# File lib/sequel/plugins/subset_static_cache.rb 153 def first(*args) 154 if !defined?(yield) && args.length <= 1 && (args.length == 0 || args[0].is_a?(Integer)) && (all = @cache[:subset_static_cache_all]) 155 all.first(*args) 156 else 157 super 158 end 159 end
Use the cache instead of a query to get the results.
# File lib/sequel/plugins/subset_static_cache.rb 179 def map(column=nil, &block) 180 return super unless all = @cache[:subset_static_cache_all] 181 if column 182 raise(Error, "Cannot provide both column and block to map") if block 183 if column.is_a?(Array) 184 all.map{|r| r.values.values_at(*column)} 185 else 186 all.map{|r| r[column]} 187 end 188 else 189 all.map(&block) 190 end 191 end
Alias of as_hash
for backwards compatibility.
# File lib/sequel/plugins/subset_static_cache.rb 229 def to_hash(*a) 230 as_hash(*a) 231 end
Use the cache instead of a query to get the results
# File lib/sequel/plugins/subset_static_cache.rb 234 def to_hash_groups(key_column, value_column = nil, opts = OPTS) 235 return super unless all = @cache[:subset_static_cache_all] 236 237 h = opts[:hash] || {} 238 if value_column 239 if value_column.is_a?(Array) 240 if key_column.is_a?(Array) 241 all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r.values.values_at(*value_column)} 242 else 243 all.each{|r| (h[r[key_column]] ||= []) << r.values.values_at(*value_column)} 244 end 245 else 246 if key_column.is_a?(Array) 247 all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r[value_column]} 248 else 249 all.each{|r| (h[r[key_column]] ||= []) << r[value_column]} 250 end 251 end 252 elsif key_column.is_a?(Array) 253 all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r} 254 else 255 all.each{|r| (h[r[key_column]] ||= []) << r} 256 end 257 h 258 end
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/subset_static_cache.rb 163 def with_pk(pk) 164 if cache = @cache[:subset_static_cache_map] 165 cache[pk] 166 else 167 super 168 end 169 end