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.
-
#each(&block) ⇒ Object
Yield each of the model’s frozen instances to the block, without issuing a database query.
-
#map(*a) ⇒ Object
If no arguments are given, yield each of the model’s frozen instances to the block.
-
#set_dataset ⇒ Object
Reload the cache when the dataset changes.
-
#to_hash(*a) ⇒ Object
If no arguments are given, yield an identity map for the model with frozen primary keys and instances, without issuing a database query.
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.
40 41 42 |
# File 'lib/sequel/plugins/static_cache.rb', line 40 def cache_get_pk(pk) cache[pk] end |
#each(&block) ⇒ Object
Yield each of the model’s frozen instances to the block, without issuing a database query.
46 47 48 |
# File 'lib/sequel/plugins/static_cache.rb', line 46 def each(&block) @all.each(&block) end |
#map(*a) ⇒ Object
If no arguments are given, yield each of the model’s frozen instances to the block. and return a new array, without issuing a database query. If any arguments are given, use the default Sequel behavior.
53 54 55 56 57 58 59 |
# File 'lib/sequel/plugins/static_cache.rb', line 53 def map(*a) if a.empty? @all.map(&(Proc.new if block_given?)) else super end end |
#set_dataset ⇒ Object
Reload the cache when the dataset changes.
62 63 64 65 66 |
# File 'lib/sequel/plugins/static_cache.rb', line 62 def set_dataset(*) s = super load_cache s end |
#to_hash(*a) ⇒ Object
If no arguments are given, yield an identity map for the model with frozen primary keys and instances, without issuing a database query. If any arguments are given, use the default Sequel behavior.
71 72 73 74 75 76 77 |
# File 'lib/sequel/plugins/static_cache.rb', line 71 def to_hash(*a) if a.empty? cache.dup else super end end |