Module: Sequel::Plugins::StaticCache::ClassMethods
- Defined in:
- lib/sequel/plugins/static_cache.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
A frozen ruby hash holding all of the model’s frozen instances, keyed by frozen primary key.
Instance Method Summary collapse
-
#all ⇒ Object
An array of all of the model’s frozen instances, without issuing a database query.
-
#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.
-
#count(*a, &block) ⇒ Object
Get the number of records in the cache, without issuing a database query.
-
#each(&block) ⇒ Object
Yield each of the model’s frozen instances to the block, without issuing a database query.
-
#map(column = nil, &block) ⇒ Object
Use the cache instead of a query to get the results.
-
#to_hash(key_column = nil, value_column = nil) ⇒ Object
Use the cache instead of a query to get the results.
-
#to_hash_groups(key_column, value_column = nil) ⇒ Object
Use the cache instead of a query to get the results.
Instance Attribute Details
#cache ⇒ Object (readonly)
A frozen ruby hash holding all of the model’s frozen instances, keyed by frozen primary key.
30 31 32 |
# File 'lib/sequel/plugins/static_cache.rb', line 30 def cache @cache end |
Instance Method Details
#all ⇒ Object
An array of all of the model’s frozen instances, without issuing a database query.
34 35 36 |
# File 'lib/sequel/plugins/static_cache.rb', line 34 def all @all.dup 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.
49 50 51 |
# File 'lib/sequel/plugins/static_cache.rb', line 49 def cache_get_pk(pk) cache[pk] end |
#count(*a, &block) ⇒ Object
Get the number of records in the cache, without issuing a database query.
39 40 41 42 43 44 45 |
# File 'lib/sequel/plugins/static_cache.rb', line 39 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.
55 56 57 |
# File 'lib/sequel/plugins/static_cache.rb', line 55 def each(&block) @all.each(&block) end |
#map(column = nil, &block) ⇒ Object
Use the cache instead of a query to get the results.
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/sequel/plugins/static_cache.rb', line 60 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 else @all.map(&(Proc.new if block_given?)) end end |
#to_hash(key_column = nil, value_column = nil) ⇒ Object
Use the cache instead of a query to get the results.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/sequel/plugins/static_cache.rb', line 76 def to_hash(key_column = nil, value_column = nil) return cache.dup if key_column.nil? && value_column.nil? h = {} if value_column if value_column.is_a?(Array) if key_column.is_a?(Array) each{|r| h[r.values.values_at(*key_column)] = r.values.values_at(*value_column)} else each{|r| h[r[key_column]] = r.values.values_at(*value_column)} end else if key_column.is_a?(Array) each{|r| h[r.values.values_at(*key_column)] = r[value_column]} else each{|r| h[r[key_column]] = r[value_column]} end end elsif key_column.is_a?(Array) each{|r| h[r.values.values_at(*key_column)] = r} else each{|r| h[r[key_column]] = r} end h end |
#to_hash_groups(key_column, value_column = nil) ⇒ Object
Use the cache instead of a query to get the results
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/sequel/plugins/static_cache.rb', line 103 def to_hash_groups(key_column, value_column = nil) h = {} if value_column if value_column.is_a?(Array) if key_column.is_a?(Array) each{|r| (h[r.values.values_at(*key_column)] ||= []) << r.values.values_at(*value_column)} else each{|r| (h[r[key_column]] ||= []) << r.values.values_at(*value_column)} end else if key_column.is_a?(Array) each{|r| (h[r.values.values_at(*key_column)] ||= []) << r[value_column]} else each{|r| (h[r[key_column]] ||= []) << r[value_column]} end end elsif key_column.is_a?(Array) each{|r| (h[r.values.values_at(*key_column)] ||= []) << r} else each{|r| (h[r[key_column]] ||= []) << r} end h end |