Module: Sequel::Plugins::SubsetStaticCache::CachedDatasetMethods

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

Instance Method Summary collapse

Instance Method Details

#all(&block) ⇒ Object

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.

[View source]

130
131
132
133
134
135
136
# File 'lib/sequel/plugins/subset_static_cache.rb', line 130

def all(&block)
  return super unless all = @cache[:subset_static_cache_all]

  array = all.dup
  array.each(&block) if block
  array
end

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

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

[View source]

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

#count(*a, &block) ⇒ Object

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

[View source]

140
141
142
143
144
145
146
# File 'lib/sequel/plugins/subset_static_cache.rb', line 140

def count(*a, &block)
  if a.empty? && !block && (all = @cache[:subset_static_cache_all])
    all.size
  else
    super
  end
end

#each(&block) ⇒ Object

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

[View source]

174
175
176
177
# File 'lib/sequel/plugins/subset_static_cache.rb', line 174

def each(&block)
  return super unless all = @cache[:subset_static_cache_all]
  all.each(&block)
end

#first(*args) ⇒ Object

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

[View source]

154
155
156
157
158
159
160
# File 'lib/sequel/plugins/subset_static_cache.rb', line 154

def first(*args)
  if !defined?(yield) && args.length <= 1 && (args.length == 0 || args[0].is_a?(Integer)) && (all = @cache[:subset_static_cache_all])
    all.first(*args)
  else
    super
  end
end

#map(column = nil, &block) ⇒ Object

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

[View source]

180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/sequel/plugins/subset_static_cache.rb', line 180

def map(column=nil, &block)
  return super unless all = @cache[:subset_static_cache_all]
  if column
    raise(Error, "Cannot provide both column and block to map") if block
    if column.is_a?(Array)
      all.map{|r| r.values.values_at(*column)}
    else
      all.map{|r| r[column]}
    end
  else
    all.map(&block)
  end
end

#to_hash(*a) ⇒ Object

Alias of as_hash for backwards compatibility.

[View source]

230
231
232
# File 'lib/sequel/plugins/subset_static_cache.rb', line 230

def to_hash(*a)
  as_hash(*a)
end

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

Use the cache instead of a query to get the results

[View source]

235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/sequel/plugins/subset_static_cache.rb', line 235

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

#with_pk(pk) ⇒ Object

Return the frozen object with the given pk, or nil if no such object exists in the cache, without issuing a database query.

[View source]

164
165
166
167
168
169
170
# File 'lib/sequel/plugins/subset_static_cache.rb', line 164

def with_pk(pk)
  if cache = @cache[:subset_static_cache_map]
    cache[pk]
  else
    super
  end
end