Class: CanTango::Ability::Cache

Inherits:
Object
  • Object
show all
Includes:
Helpers::Debug, Helpers::RoleMethods
Defined in:
lib/cantango/ability/cache.rb,
lib/cantango/adapter/moneta.rb,
lib/cantango/adapter/compiler.rb,
lib/cantango/ability/cache/key.rb,
lib/cantango/ability/cache/reader.rb,
lib/cantango/ability/cache/writer.rb,
lib/cantango/ability/cache/kompiler.rb,
lib/cantango/ability/cache/base_cache.rb,
lib/cantango/ability/cache/rules_cache.rb,
lib/cantango/ability/cache/moneta_cache.rb,
lib/cantango/ability/cache/session_cache.rb

Defined Under Namespace

Classes: BaseCache, Key, Kompiler, MonetaCache, Reader, RulesCache, SessionCache, Writer

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::RoleMethods

#config, #has_role_group_meth, #has_role_meth, #role_groups_list_meth, #roles_list_meth

Methods included from Helpers::Debug

#debug

Constructor Details

#initialize(ability, options = {}) ⇒ Cache

Returns a new instance of Cache.



14
15
16
17
18
19
# File 'lib/cantango/ability/cache.rb', line 14

def initialize ability, options = {}
  @ability = ability
  @cache_key = options[:cache_key]
  @key_method_names = options[:key_method_names]
  debug "Creating cache with key: #{cache_key.inspect} on #{key_method_names.inspect}"
end

Instance Attribute Details

#abilityObject (readonly)

Returns the value of attribute ability.



9
10
11
# File 'lib/cantango/ability/cache.rb', line 9

def ability
  @ability
end

#cache_keyObject



29
30
31
# File 'lib/cantango/ability/cache.rb', line 29

def cache_key
  @cache_key ||= :cache
end

#key_method_namesObject



25
26
27
# File 'lib/cantango/ability/cache.rb', line 25

def key_method_names
  @key_method_names ||= [roles_list_meth, role_groups_list_meth]
end

#rules_cachedObject (readonly)

Returns the value of attribute rules_cached.



9
10
11
# File 'lib/cantango/ability/cache.rb', line 9

def rules_cached
  @rules_cached
end

Instance Method Details

#cache_rules!Object



33
34
35
# File 'lib/cantango/ability/cache.rb', line 33

def cache_rules!
  writer.save(key, reader.prepared_rules) if cached?
end

#cached_rulesObject



37
38
39
# File 'lib/cantango/ability/cache.rb', line 37

def cached_rules
  @rules ||= reader.prepared_rules if cached?
end

#cached_rules?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/cantango/ability/cache.rb', line 53

def cached_rules?
  key.same?(session) && cached?
end

#compile_adapter?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/cantango/ability/cache.rb', line 75

def compile_adapter?
  CanTango.config.adapters.registered?(:compiler)
end

#compile_on?Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/cantango/ability/cache.rb', line 70

def compile_on?
  return false if !compile_adapter?
  CanTango.config.cache_engine.compile?
end

#compilerObject



41
42
43
# File 'lib/cantango/ability/cache.rb', line 41

def compiler
  @compiler ||= Kompiler.new
end

#empty?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/cantango/ability/cache.rb', line 21

def empty?
  cached_rules.blank?
end

#invalidate!Object



65
66
67
68
# File 'lib/cantango/ability/cache.rb', line 65

def invalidate!
  raise "no session" if !session
  rules_cache.invalidate! session[cache_key]
end

#keyObject



57
58
59
# File 'lib/cantango/ability/cache.rb', line 57

def key
  @key ||= Key.new ability.user, ability.subject, key_method_names
end

#readerObject



45
46
47
# File 'lib/cantango/ability/cache.rb', line 45

def reader
  @reader ||= Reader.new(self)
end

#rules_cacheObject



61
62
63
# File 'lib/cantango/ability/cache.rb', line 61

def rules_cache
  @rules_cache ||= RulesCache.new(session).instance
end

#writerObject



49
50
51
# File 'lib/cantango/ability/cache.rb', line 49

def writer
  @writer ||= Writer.new(self)
end