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

Methods

all   cache_get_pk   count   each   map   to_hash   to_hash_groups  

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 34
34:         def all
35:           @all.dup
36:         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 49
49:         def cache_get_pk(pk)
50:           cache[pk]
51:         end

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

[Source]

    # File lib/sequel/plugins/static_cache.rb, line 39
39:         def count(*a, &block)
40:           if a.empty? && !block
41:             @all.size
42:           else
43:             super
44:           end
45:         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 55
55:         def each(&block)
56:           @all.each(&block)
57:         end

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

[Source]

    # File lib/sequel/plugins/static_cache.rb, line 60
60:         def map(column=nil, &block)
61:           if column
62:             raise(Error, "Cannot provide both column and block to map") if block
63:             if column.is_a?(Array)
64:               @all.map{|r| r.values.values_at(*column)}
65:             else
66:               @all.map{|r| r[column]}
67:             end
68:           else
69:             @all.map(&(Proc.new if block_given?))
70:           end
71:         end

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

[Source]

     # File lib/sequel/plugins/static_cache.rb, line 76
 76:         def to_hash(key_column = nil, value_column = nil)
 77:         return cache.dup if key_column.nil? && value_column.nil?
 78: 
 79:         h = {}
 80:         if value_column
 81:           if value_column.is_a?(Array)
 82:             if key_column.is_a?(Array)
 83:               each{|r| h[r.values.values_at(*key_column)] = r.values.values_at(*value_column)}
 84:             else
 85:               each{|r| h[r[key_column]] = r.values.values_at(*value_column)}
 86:             end
 87:           else
 88:             if key_column.is_a?(Array)
 89:               each{|r| h[r.values.values_at(*key_column)] = r[value_column]}
 90:             else
 91:               each{|r| h[r[key_column]] = r[value_column]}
 92:             end
 93:           end
 94:         elsif key_column.is_a?(Array)
 95:           each{|r| h[r.values.values_at(*key_column)] = r}
 96:         else
 97:           each{|r| h[r[key_column]] = r}
 98:         end
 99:         h
100:         end

Use the cache instead of a query to get the results

[Source]

     # File lib/sequel/plugins/static_cache.rb, line 103
103:         def to_hash_groups(key_column, value_column = nil)
104:           h = {}
105:           if value_column
106:             if value_column.is_a?(Array)
107:               if key_column.is_a?(Array)
108:                 each{|r| (h[r.values.values_at(*key_column)] ||= []) << r.values.values_at(*value_column)}
109:               else
110:                 each{|r| (h[r[key_column]] ||= []) << r.values.values_at(*value_column)}
111:               end
112:             else
113:               if key_column.is_a?(Array)
114:                 each{|r| (h[r.values.values_at(*key_column)] ||= []) << r[value_column]}
115:               else
116:                 each{|r| (h[r[key_column]] ||= []) << r[value_column]}
117:               end
118:             end
119:           elsif key_column.is_a?(Array)
120:             each{|r| (h[r.values.values_at(*key_column)] ||= []) << r}
121:           else
122:             each{|r| (h[r[key_column]] ||= []) << r}
123:           end
124:           h
125:         end

[Validate]