Method: Sequel::Plugins::SubsetStaticCache::CachedDatasetMethods#to_hash_groups

Defined in:
lib/sequel/plugins/subset_static_cache.rb

#to_hash_groups(key_column, value_column = nil, opts = OPTS) ⇒ Object

Use the cache instead of a query to get the results



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/sequel/plugins/subset_static_cache.rb', line 250

def to_hash_groups(key_column, value_column = nil, opts = OPTS)
  return super unless all = @cache[:subset_static_cache_all]

  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