Class: CachedEnumeration::Cache
- Inherits:
-
Object
- Object
- CachedEnumeration::Cache
- Defined in:
- lib/cached_enumeration/cached_enumeration.rb
Overview
provide cached access to enumeration values
usage: add cache_enumeration <params> to ActiveRecord class
parameters are
:order order of items in cached_all (default: 'id')
:hashed list of attributes to provide hashes for (default: [ 'id', 'name' ];
:hashed list of attributes to provide hashes for (default: [ 'id', 'name' ];
id will always be added to that list, if missing
:constantize attribute to provide constants for (default: 'name')
use nil, not to generate constants
cached methods are: find_from_ids( <id> ) or find_from_ids( [ <id>, <id>, … ] )
providing cached find( <id> ) or find( [ <id>, <id>, ... ] )
find_by_XY / by_XY for all hashed attributes (by_XY is deprecated) cached_all
besides constants using the upcase name are set up providing the entries
note that all objects (arrays, maps and the models themselfs) are frozen to avoid unintentional changes.
Cachability of enumerations does not imply that all enumeration access should be cached. This is a question that needs to be well thought depending on the size of the enumeration and the number of accesses to the cached data.
The by_XY finder should be avoided as the find_by_XY will be available with and without cache.
Defined Under Namespace
Modules: ConstMissing
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #all ⇒ Object
-
#cache! ⇒ Object
forces a cache.
- #cached? ⇒ Boolean
- #first ⇒ Object
-
#get_by(att, key) ⇒ Object
returns a value from a cache.
- #hashed_by?(att) ⇒ Boolean
-
#initialize(base, params) ⇒ Cache
constructor
A new instance of Cache.
- #order ⇒ Object
Constructor Details
#initialize(base, params) ⇒ Cache
Returns a new instance of Cache.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/cached_enumeration/cached_enumeration.rb', line 36 def initialize(base, params) # p params @options=(params) @cache={} #cache by keys @all=[] #cache of all @status=:uncached #can be :uncached,:cashing,:cached @klass=base #base.extend(ClassMethods) #base.reset_column_information base_singleton = class << base; self end patch_const_missing(base_singleton) if @options[:constantize] #create_find_by_methods(base_singleton) end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
34 35 36 |
# File 'lib/cached_enumeration/cached_enumeration.rb', line 34 def @options end |
Instance Method Details
#all ⇒ Object
54 55 56 57 |
# File 'lib/cached_enumeration/cached_enumeration.rb', line 54 def all ensure_caches @all end |
#cache! ⇒ Object
forces a cache
74 75 76 77 |
# File 'lib/cached_enumeration/cached_enumeration.rb', line 74 def cache! #only load if loading not yet in progress ensure_caches if @status == :uncached end |
#cached? ⇒ Boolean
79 80 81 |
# File 'lib/cached_enumeration/cached_enumeration.rb', line 79 def cached? @status==:cached end |
#first ⇒ Object
87 88 89 |
# File 'lib/cached_enumeration/cached_enumeration.rb', line 87 def first @all.first end |
#get_by(att, key) ⇒ Object
returns a value from a cache
62 63 64 65 66 |
# File 'lib/cached_enumeration/cached_enumeration.rb', line 62 def get_by(att, key) ensure_caches key=key.to_i if att.to_s == "id" @cache[att.to_s][key] end |
#hashed_by?(att) ⇒ Boolean
68 69 70 |
# File 'lib/cached_enumeration/cached_enumeration.rb', line 68 def hashed_by?(att) [:hashed].include?(att.to_s) end |
#order ⇒ Object
83 84 85 |
# File 'lib/cached_enumeration/cached_enumeration.rb', line 83 def order @options[:order] end |