Method: Sequel::Plugins::SubsetStaticCache::CachedDatasetMethods#as_hash
- Defined in:
- lib/sequel/plugins/subset_static_cache.rb
#as_hash(key_column = nil, value_column = nil, opts = OPTS) ⇒ Object
Use the cache instead of a query to get the results if possible
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/sequel/plugins/subset_static_cache.rb', line 195 def as_hash(key_column = nil, value_column = nil, opts = OPTS) return super unless all = @cache[:subset_static_cache_all] if key_column.nil? && value_column.nil? if opts[:hash] key_column = model.primary_key else return Hash[@cache[:subset_static_cache_map]] end end h = opts[:hash] || {} if value_column if value_column.is_a?(Array) if key_column.is_a?(Array) all.each{|r| h[r.values.values_at(*key_column)] = r.values.values_at(*value_column)} else all.each{|r| h[r[key_column]] = r.values.values_at(*value_column)} end else if key_column.is_a?(Array) all.each{|r| h[r.values.values_at(*key_column)] = r[value_column]} else all.each{|r| h[r[key_column]] = r[value_column]} end end elsif key_column.is_a?(Array) all.each{|r| h[r.values.values_at(*key_column)] = r} else all.each{|r| h[r[key_column]] = r} end h end |