Module: Sequel::Plugins::StaticCache::ClassMethods

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cacheObject (readonly)

A frozen ruby hash holding all of the model’s frozen instances, keyed by frozen primary key.



76
77
78
# File 'lib/sequel/plugins/static_cache.rb', line 76

def cache
  @cache
end

Instance Method Details

#all(&block) ⇒ Object

An array of all of the model’s instances, without issuing a database query. If a block is given, yields each instance to the block.



80
81
82
83
84
# File 'lib/sequel/plugins/static_cache.rb', line 80

def all(&block)
  array = @static_cache_frozen ? @all.dup : to_a
  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.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/sequel/plugins/static_cache.rb', line 147

def as_hash(key_column = nil, value_column = nil, opts = OPTS)
  if key_column.nil? && value_column.nil?
    if @static_cache_frozen && !opts[:hash]
      return Hash[cache]
    else
      key_column = primary_key
    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)] = static_cache_object(r)}
  else
    @all.each{|r| h[r[key_column]] = static_cache_object(r)}
  end
  h
end

#cache_get_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.



111
112
113
# File 'lib/sequel/plugins/static_cache.rb', line 111

def cache_get_pk(pk)
  static_cache_object(cache[pk])
end

#count(*a, &block) ⇒ Object

Get the number of records in the cache, without issuing a database query.



101
102
103
104
105
106
107
# File 'lib/sequel/plugins/static_cache.rb', line 101

def count(*a, &block)
  if a.empty? && !block
    @all.size
  else
    super
  end
end

#each(&block) ⇒ Object

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



117
118
119
120
121
122
123
# File 'lib/sequel/plugins/static_cache.rb', line 117

def each(&block)
  if @static_cache_frozen
    @all.each(&block)
  else
    @all.each{|o| yield(static_cache_object(o))}
  end
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).



92
93
94
95
96
97
98
# File 'lib/sequel/plugins/static_cache.rb', line 92

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

#load_cacheObject

Reload the cache for this model by retrieving all of the instances in the dataset freezing them, and populating the cached array and hash.



216
217
218
219
220
221
222
223
224
# File 'lib/sequel/plugins/static_cache.rb', line 216

def load_cache
  @all = load_static_cache_rows
  h = {}
  @all.each do |o|
    o.errors.freeze
    h[o.pk.freeze] = o.freeze
  end
  @cache = h.freeze
end

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

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



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/sequel/plugins/static_cache.rb', line 126

def map(column=nil, &block)
  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
  elsif @static_cache_frozen
    @all.map(&block)
  elsif block
    @all.map{|o| yield(static_cache_object(o))}
  else
    all.map
  end
end

#static_cache_allow_modifications?Boolean

Ask whether modifications to this class are allowed.

Returns:

  • (Boolean)


210
211
212
# File 'lib/sequel/plugins/static_cache.rb', line 210

def static_cache_allow_modifications?
  !@static_cache_frozen
end

#to_hash(*a) ⇒ Object

Alias of as_hash for backwards compatibility.



180
181
182
# File 'lib/sequel/plugins/static_cache.rb', line 180

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



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/sequel/plugins/static_cache.rb', line 185

def to_hash_groups(key_column, value_column = nil, opts = OPTS)
  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)] ||= []) << static_cache_object(r)}
  else
    @all.each{|r| (h[r[key_column]] ||= []) << static_cache_object(r)}
  end
  h
end