Class: CachedEnumeration::Cache

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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=init_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

#optionsObject (readonly)

Returns the value of attribute options.



34
35
36
# File 'lib/cached_enumeration/cached_enumeration.rb', line 34

def options
  @options
end

Instance Method Details

#allObject



54
55
56
57
# File 'lib/cached_enumeration/cached_enumeration.rb', line 54

def all
  ensure_caches
  @all
end

#cache!Object

forces a cache

Returns:

  • Boolean true is it just cached, false if it was already cached



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

Returns:

  • (Boolean)


79
80
81
# File 'lib/cached_enumeration/cached_enumeration.rb', line 79

def cached?
  @status==:cached
end

#firstObject



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

Parameters:

  • String

    att name of the attribute

  • String

    key value of the attribute



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

Returns:

  • (Boolean)


68
69
70
# File 'lib/cached_enumeration/cached_enumeration.rb', line 68

def hashed_by?(att)
  options[:hashed].include?(att.to_s)
end

#orderObject



83
84
85
# File 'lib/cached_enumeration/cached_enumeration.rb', line 83

def order
  @options[:order]
end