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.
-
#static_cache_allow_modifications? ⇒ Boolean
Ask whether modifications to this class are allowed.
-
#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.
69 70 71 |
# File 'lib/sequel/plugins/static_cache.rb', line 69 def cache @cache end |
Instance Method Details
#all ⇒ Object
An array of all of the model’s frozen instances, without issuing a database query.
73 74 75 76 77 78 79 |
# File 'lib/sequel/plugins/static_cache.rb', line 73 def all if @static_cache_frozen @all.dup else map{|o| o} end 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.
92 93 94 |
# File 'lib/sequel/plugins/static_cache.rb', line 92 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.
82 83 84 85 86 87 88 |
# File 'lib/sequel/plugins/static_cache.rb', line 82 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.
98 99 100 101 102 103 104 |
# File 'lib/sequel/plugins/static_cache.rb', line 98 def each(&block) if @static_cache_frozen @all.each(&block) else @all.each{|o| yield(static_cache_object(o))} end end |
#map(column = nil, &block) ⇒ Object
Use the cache instead of a query to get the results.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/sequel/plugins/static_cache.rb', line 107 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.
186 187 188 |
# File 'lib/sequel/plugins/static_cache.rb', line 186 def static_cache_allow_modifications? !@static_cache_frozen end |
#to_hash(key_column = nil, value_column = nil) ⇒ Object
Use the cache instead of a query to get the results.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/sequel/plugins/static_cache.rb', line 128 def to_hash(key_column = nil, value_column = nil) if key_column.nil? && value_column.nil? if @static_cache_frozen return Hash[cache] else key_column = primary_key end end h = {} 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 |
#to_hash_groups(key_column, value_column = nil) ⇒ Object
Use the cache instead of a query to get the results
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/sequel/plugins/static_cache.rb', line 161 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) @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 |