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 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
Use the cache instead of a query to get the results if possible
# 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
Use the cache instead of a query to get the results.
# File lib/sequel/plugins/subset_static_cache.rb 230 def as_set(column) 231 return super unless all = @cache[:subset_static_cache_all] 232 233 set = Set.new 234 235 if column.is_a?(Array) 236 all.each{|r| set.add(r.values.values_at(*column))} 237 else 238 all.each{|r| set.add(r[column])} 239 end 240 241 set 242 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 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
Yield each of the dataset’s frozen instances to the block, without issuing a database query.
# 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
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 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
Use the cache instead of a query to get the results.
# 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
Alias of as_hash for backwards compatibility.
# File lib/sequel/plugins/subset_static_cache.rb 245 def to_hash(*a) 246 as_hash(*a) 247 end
Use the cache instead of a query to get the results
# File lib/sequel/plugins/subset_static_cache.rb 250 def to_hash_groups(key_column, value_column = nil, opts = OPTS) 251 return super unless all = @cache[:subset_static_cache_all] 252 253 h = opts[:hash] || {} 254 if value_column 255 if value_column.is_a?(Array) 256 if key_column.is_a?(Array) 257 all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r.values.values_at(*value_column)} 258 else 259 all.each{|r| (h[r[key_column]] ||= []) << r.values.values_at(*value_column)} 260 end 261 else 262 if key_column.is_a?(Array) 263 all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r[value_column]} 264 else 265 all.each{|r| (h[r[key_column]] ||= []) << r[value_column]} 266 end 267 end 268 elsif key_column.is_a?(Array) 269 all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r} 270 else 271 all.each{|r| (h[r[key_column]] ||= []) << r} 272 end 273 h 274 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 164 def with_pk(pk) 165 if cache = @cache[:subset_static_cache_map] 166 cache[pk] 167 else 168 super 169 end 170 end