Module Sequel::Plugins::StaticCache::ClassMethods
In: lib/sequel/plugins/static_cache.rb

Methods

Attributes

cache  [R]  A frozen ruby hash holding all of the model‘s frozen instances, keyed by frozen primary key.

Public Instance methods

An array of all of the model‘s frozen instances, without issuing a database query.

[Source]

    # File lib/sequel/plugins/static_cache.rb, line 56
56:         def all
57:           if @static_cache_frozen
58:             @all.dup
59:           else
60:             map{|o| o}
61:           end
62:         end

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

[Source]

    # File lib/sequel/plugins/static_cache.rb, line 75
75:         def cache_get_pk(pk)
76:           static_cache_object(cache[pk])
77:         end

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

[Source]

    # File lib/sequel/plugins/static_cache.rb, line 65
65:         def count(*a, &block)
66:           if a.empty? && !block
67:             @all.size
68:           else
69:             super
70:           end
71:         end

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

[Source]

    # File lib/sequel/plugins/static_cache.rb, line 81
81:         def each(&block)
82:           if @static_cache_frozen
83:             @all.each(&block)
84:           else
85:             @all.each{|o| yield(static_cache_object(o))}
86:           end
87:         end

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

[Source]

     # File lib/sequel/plugins/static_cache.rb, line 90
 90:         def map(column=nil, &block)
 91:           if column
 92:             raise(Error, "Cannot provide both column and block to map") if block
 93:             if column.is_a?(Array)
 94:               @all.map{|r| r.values.values_at(*column)}
 95:             else
 96:               @all.map{|r| r[column]}
 97:             end
 98:           elsif @static_cache_frozen
 99:             @all.map(&block)
100:           elsif block
101:             @all.map{|o| yield(static_cache_object(o))}
102:           else
103:             all.map
104:           end
105:         end

Ask whether modifications to this class are allowed.

[Source]

     # File lib/sequel/plugins/static_cache.rb, line 169
169:         def static_cache_allow_modifications?
170:           !@static_cache_frozen
171:         end

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

[Source]

     # File lib/sequel/plugins/static_cache.rb, line 111
111:         def to_hash(key_column = nil, value_column = nil)
112:         if key_column.nil? && value_column.nil?
113:           if @static_cache_frozen
114:             return cache.dup
115:           else
116:             key_column = primary_key
117:           end
118:         end
119: 
120:         h = {}
121:         if value_column
122:           if value_column.is_a?(Array)
123:             if key_column.is_a?(Array)
124:               @all.each{|r| h[r.values.values_at(*key_column)] = r.values.values_at(*value_column)}
125:             else
126:               @all.each{|r| h[r[key_column]] = r.values.values_at(*value_column)}
127:             end
128:           else
129:             if key_column.is_a?(Array)
130:               @all.each{|r| h[r.values.values_at(*key_column)] = r[value_column]}
131:             else
132:               @all.each{|r| h[r[key_column]] = r[value_column]}
133:             end
134:           end
135:         elsif key_column.is_a?(Array)
136:           @all.each{|r| h[r.values.values_at(*key_column)] = static_cache_object(r)}
137:         else
138:           @all.each{|r| h[r[key_column]] = static_cache_object(r)}
139:         end
140:         h
141:         end

Use the cache instead of a query to get the results

[Source]

     # File lib/sequel/plugins/static_cache.rb, line 144
144:         def to_hash_groups(key_column, value_column = nil)
145:           h = {}
146:           if value_column
147:             if value_column.is_a?(Array)
148:               if key_column.is_a?(Array)
149:                 @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r.values.values_at(*value_column)}
150:               else
151:                 @all.each{|r| (h[r[key_column]] ||= []) << r.values.values_at(*value_column)}
152:               end
153:             else
154:               if key_column.is_a?(Array)
155:                 @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r[value_column]}
156:               else
157:                 @all.each{|r| (h[r[key_column]] ||= []) << r[value_column]}
158:               end
159:             end
160:           elsif key_column.is_a?(Array)
161:             @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << static_cache_object(r)}
162:           else
163:             @all.each{|r| (h[r[key_column]] ||= []) << static_cache_object(r)}
164:           end
165:           h
166:         end

[Validate]