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 75
75:         def all
76:           if @static_cache_frozen
77:             @all.dup
78:           else
79:             map{|o| o}
80:           end
81:         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 94
94:         def cache_get_pk(pk)
95:           static_cache_object(cache[pk])
96:         end

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

[Source]

    # File lib/sequel/plugins/static_cache.rb, line 84
84:         def count(*a, &block)
85:           if a.empty? && !block
86:             @all.size
87:           else
88:             super
89:           end
90:         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 100
100:         def each(&block)
101:           if @static_cache_frozen
102:             @all.each(&block)
103:           else
104:             @all.each{|o| yield(static_cache_object(o))}
105:           end
106:         end

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

[Source]

     # File lib/sequel/plugins/static_cache.rb, line 109
109:         def map(column=nil, &block)
110:           if column
111:             raise(Error, "Cannot provide both column and block to map") if block
112:             if column.is_a?(Array)
113:               @all.map{|r| r.values.values_at(*column)}
114:             else
115:               @all.map{|r| r[column]}
116:             end
117:           elsif @static_cache_frozen
118:             @all.map(&block)
119:           elsif block
120:             @all.map{|o| yield(static_cache_object(o))}
121:           else
122:             all.map
123:           end
124:         end

Ask whether modifications to this class are allowed.

[Source]

     # File lib/sequel/plugins/static_cache.rb, line 188
188:         def static_cache_allow_modifications?
189:           !@static_cache_frozen
190:         end

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

[Source]

     # File lib/sequel/plugins/static_cache.rb, line 130
130:         def to_hash(key_column = nil, value_column = nil)
131:         if key_column.nil? && value_column.nil?
132:           if @static_cache_frozen
133:             return Hash[cache]
134:           else
135:             key_column = primary_key
136:           end
137:         end
138: 
139:         h = {}
140:         if value_column
141:           if value_column.is_a?(Array)
142:             if key_column.is_a?(Array)
143:               @all.each{|r| h[r.values.values_at(*key_column)] = r.values.values_at(*value_column)}
144:             else
145:               @all.each{|r| h[r[key_column]] = r.values.values_at(*value_column)}
146:             end
147:           else
148:             if key_column.is_a?(Array)
149:               @all.each{|r| h[r.values.values_at(*key_column)] = r[value_column]}
150:             else
151:               @all.each{|r| h[r[key_column]] = r[value_column]}
152:             end
153:           end
154:         elsif key_column.is_a?(Array)
155:           @all.each{|r| h[r.values.values_at(*key_column)] = static_cache_object(r)}
156:         else
157:           @all.each{|r| h[r[key_column]] = static_cache_object(r)}
158:         end
159:         h
160:         end

Use the cache instead of a query to get the results

[Source]

     # File lib/sequel/plugins/static_cache.rb, line 163
163:         def to_hash_groups(key_column, value_column = nil)
164:           h = {}
165:           if value_column
166:             if value_column.is_a?(Array)
167:               if key_column.is_a?(Array)
168:                 @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r.values.values_at(*value_column)}
169:               else
170:                 @all.each{|r| (h[r[key_column]] ||= []) << r.values.values_at(*value_column)}
171:               end
172:             else
173:               if key_column.is_a?(Array)
174:                 @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r[value_column]}
175:               else
176:                 @all.each{|r| (h[r[key_column]] ||= []) << r[value_column]}
177:               end
178:             end
179:           elsif key_column.is_a?(Array)
180:             @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << static_cache_object(r)}
181:           else
182:             @all.each{|r| (h[r[key_column]] ||= []) << static_cache_object(r)}
183:           end
184:           h
185:         end

[Validate]