Skip to content
Browse files

refactored namespacing

  • Loading branch information...
1 parent 25251c6 commit a29e61c0d13e4d7f412e5673129146f04dcacaf9 @kristianmandrup committed Nov 24, 2011
View
86 lib/cantango/ability/cache/key.rb
@@ -0,0 +1,86 @@
+module CanTango
+ module Ability
+ module Cache
+ class Key
+ attr_reader :user, :subject
+
+ def initialize user, subject = nil, method_names = nil
+ @user = user
+ @subject = subject || user
+ @method_names = method_names if method_names
+ end
+
+ def method_names
+ @method_names ||= [:roles_list, :role_groups_list]
+ end
+
+ def self.create_for ability
+ self.new ability.user, ability.subject
+ end
+
+ def value
+ raise "No key could be generated for User:#{user.inspect} and Subject:#{subject} - key field: #{user_key_field}" if hash_values.empty?
+ @value ||= hash_values.hash
+ end
+
+ def same? session
+ raise "No session available" if !session
+ session[:cache_key] && (value == session[:cache_key].value)
+ end
+
+ def to_s
+ "key hash: #{value}"
+ end
+
+ protected
+
+ def hash_values
+ @hash_values ||= [user_key, subject_roles_hash, permissions_key].compact
+ end
+
+ def permissions_key
+ return subject.send(:permissions_hash) if permissions_key? && subject.respond_to?(:permissions_hash)
+ subject.send(:permissions) if permissions_key?
+ end
+
+ def permissions_key?
+ subject.respond_to?(:permissions) && permission_engine.modes.include?(:cache) && permission_engine.on?
+ end
+
+ def user_key
+ # raise "#{user.class} must have a method ##{user_key_field}. You can configure this with CanTango.config#user.unique_key_field" if !user.respond_to?(user_key_field)
+ user.send(user_key_field) if user.respond_to? user_key_field
+ end
+
+ def user_key_field
+ CanTango.config.user.unique_key_field || :email
+ end
+
+ def subject_roles_hash
+ role_hash_values.empty? ? nil : role_hash_values.hash
+ end
+
+ def role_hash_values
+ @role_hash_values ||= method_names.inject([]) do |result, meth_name|
+ result << subject.send(meth_name) if use_in_hash? meth_name
+ result
+ end
+ end
+
+ private
+
+ def permission_engine
+ CanTango.config.engine(:user_ac)
+ end
+
+ def use_in_hash? meth_name
+ subject.respond_to?(meth_name) && CanTango.config.permits.enabled_types.include?(meth_map[meth_name])
+ end
+
+ def meth_map
+ {:roles_list => :role, :role_groups_list => :role_group }
+ end
+ end
+ end
+ end
+end
View
40 lib/cantango/ability/cache/kompiler.rb
@@ -0,0 +1,40 @@
+require 'sourcify'
+
+module CanTango
+ module Ability
+ module Cache
+ class Kompiler
+ class CacheMarshallError < StandardError; end
+
+ def compile! rules_raw
+ return if !rules_raw
+ rules_compiled = rules_raw.map do |rule|
+ rule.block = rule.block.to_source if rule.block.class == Proc && sourcify?
+ rule
+ end
+ end
+
+ def decompile! rules_compiled
+ return if !rules_compiled
+ rules_raw = rules_compiled.map do |rule|
+ compiler_check! rule
+ rule.block = eval("#{rule.block}") if sourcify?
+ rule
+ end
+ end
+
+ protected
+
+ def sourcify?
+ @sourcify ||= Proc.new {}.respond_to?(:to_source)
+ end
+
+ def compiler_check! rule
+ if rule.block && !CanTango.config.adapters.registered?(:compiler)
+ raise "You can NOT marshal dynamic rules (with Procs) unless you register the :compiler adapter"
+ end
+ end
+ end
+ end
+ end
+end
View
34 lib/cantango/ability/cache/reader.rb
@@ -0,0 +1,34 @@
+module CanTango
+ module Ability
+ module Cache
+ class Reader
+ include CanTango::Helpers::Debug
+
+ attr_reader :cache
+
+ def initialize cache
+ @cache = cache
+ end
+
+ def prepared_rules
+ debug "reading rules from cache: #{cache.key}"
+ cache.compile_on? ? compiler.decompile!(loaded_rules) : loaded_rules
+ end
+
+ protected
+
+ def loaded_rules
+ rules_cache.load(cache.key)
+ end
+
+ def compiler
+ cache.compiler
+ end
+
+ def rules_cache
+ cache.rules_cache
+ end
+ end
+ end
+ end
+end
View
4 lib/cantango/ability_cache/writer.rb → lib/cantango/ability/cache/writer.rb
@@ -1,6 +1,6 @@
module CanTango
- class Ability
- class Cache
+ module Ability
+ module Cache
class Writer
include CanTango::Helpers::Debug
View
29 lib/cantango/ability/cached.rb
@@ -0,0 +1,29 @@
+module CanTango
+ module Ability
+ class Cached < Base
+ # Equivalent to a CanCan Ability#initialize call
+ # which executes all the permission logic
+ def initialize candidate, options = {}
+ raise "Candidate must be something!" if !candidate
+ @candidate, @options = candidate, options
+
+ # return if cached_rules?
+
+ clear_rules!
+ permit_rules
+
+ execute_engines! if engines_on?
+
+ # cache_rules!
+ end
+
+ def cached?
+ true
+ end
+
+ def permit_rules
+ end
+ end
+ end
+
+end
View
15 lib/cantango/ability/helper/cache.rb
@@ -0,0 +1,15 @@
+module CanTango
+ class Ability
+ module Helper
+ module Cache
+ include CanTango::Helpers::RoleMethods
+
+ delegate :cache_rules!, :cached_rules, :cached_rules?, :to => :cache
+
+ def cache options = {}
+ @cache ||= CanTango::Ability::Cache.new self, options
+ end
+ end
+ end
+ end
+end
View
150 lib/cantango/ability_cache.rb
@@ -1,78 +1,80 @@
module CanTango
- class AbilityCache
- autoload_modules :BaseCache, :SessionCache, :Reader, :Writer, :RulesCache, :Key
-
- include CanTango::Helpers::Debug
- include CanTango::Helpers::RoleMethods
-
- attr_reader :rules_cached, :ability
- attr_writer :key_method_names, :cache_key
-
- delegate :session, :cached?, :to => :ability
-
- 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
-
- def empty?
- cached_rules.blank?
- end
-
- def key_method_names
- @key_method_names ||= [roles_list_meth, role_groups_list_meth]
- end
-
- def cache_key
- @cache_key ||= :cache
- end
-
- def cache_rules!
- writer.save(key, reader.prepared_rules) if cached?
- end
-
- def cached_rules
- @rules ||= reader.prepared_rules if cached?
- end
-
- def compiler
- @compiler ||= Kompiler.new
- end
-
- def reader
- @reader ||= Reader.new(self)
- end
-
- def writer
- @writer ||= Writer.new(self)
- end
-
- def cached_rules?
- key.same?(session) && cached?
- end
-
- def key
- @key ||= Key.new ability.user, ability.subject, key_method_names
- end
-
- def rules_cache
- @rules_cache ||= RulesCache.new(session).instance
- end
-
- def invalidate!
- raise "no session" if !session
- rules_cache.invalidate! session[cache_key]
- end
-
- def compile_on?
- return false if !compile_adapter?
- CanTango.config.cache_engine.compile?
- end
-
- def compile_adapter?
- CanTango.config.adapters.registered?(:compiler)
+ module Ability
+ class Cache
+ autoload_modules :BaseCache, :SessionCache, :Reader, :Writer, :RulesCache, :Key
+
+ include CanTango::Helpers::Debug
+ include CanTango::Helpers::RoleMethods
+
+ attr_reader :rules_cached, :ability
+ attr_writer :key_method_names, :cache_key
+
+ delegate :session, :cached?, :to => :ability
+
+ 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
+
+ def empty?
+ cached_rules.blank?
+ end
+
+ def key_method_names
+ @key_method_names ||= [roles_list_meth, role_groups_list_meth]
+ end
+
+ def cache_key
+ @cache_key ||= :cache
+ end
+
+ def cache_rules!
+ writer.save(key, reader.prepared_rules) if cached?
+ end
+
+ def cached_rules
+ @rules ||= reader.prepared_rules if cached?
+ end
+
+ def compiler
+ @compiler ||= Kompiler.new
+ end
+
+ def reader
+ @reader ||= Reader.new(self)
+ end
+
+ def writer
+ @writer ||= Writer.new(self)
+ end
+
+ def cached_rules?
+ key.same?(session) && cached?
+ end
+
+ def key
+ @key ||= Key.new ability.user, ability.subject, key_method_names
+ end
+
+ def rules_cache
+ @rules_cache ||= RulesCache.new(session).instance
+ end
+
+ def invalidate!
+ raise "no session" if !session
+ rules_cache.invalidate! session[cache_key]
+ end
+
+ def compile_on?
+ return false if !compile_adapter?
+ CanTango.config.cache_engine.compile?
+ end
+
+ def compile_adapter?
+ CanTango.config.adapters.registered?(:compiler)
+ end
end
end
end
View
BIN lib/cantango/ability_cache/.DS_Store
Binary file not shown.
View
84 lib/cantango/ability_cache/key.rb
@@ -1,84 +0,0 @@
-module CanTango
- class AbilityCache
- class Key
- attr_reader :user, :subject
-
- def initialize user, subject = nil, method_names = nil
- @user = user
- @subject = subject || user
- @method_names = method_names if method_names
- end
-
- def method_names
- @method_names ||= [:roles_list, :role_groups_list]
- end
-
- def self.create_for ability
- self.new ability.user, ability.subject
- end
-
- def value
- raise "No key could be generated for User:#{user.inspect} and Subject:#{subject} - key field: #{user_key_field}" if hash_values.empty?
- @value ||= hash_values.hash
- end
-
- def same? session
- raise "No session available" if !session
- session[:cache_key] && (value == session[:cache_key].value)
- end
-
- def to_s
- "key hash: #{value}"
- end
-
- protected
-
- def hash_values
- @hash_values ||= [user_key, subject_roles_hash, permissions_key].compact
- end
-
- def permissions_key
- return subject.send(:permissions_hash) if permissions_key? && subject.respond_to?(:permissions_hash)
- subject.send(:permissions) if permissions_key?
- end
-
- def permissions_key?
- subject.respond_to?(:permissions) && permission_engine.modes.include?(:cache) && permission_engine.on?
- end
-
- def user_key
- # raise "#{user.class} must have a method ##{user_key_field}. You can configure this with CanTango.config#user.unique_key_field" if !user.respond_to?(user_key_field)
- user.send(user_key_field) if user.respond_to? user_key_field
- end
-
- def user_key_field
- CanTango.config.user.unique_key_field || :email
- end
-
- def subject_roles_hash
- role_hash_values.empty? ? nil : role_hash_values.hash
- end
-
- def role_hash_values
- @role_hash_values ||= method_names.inject([]) do |result, meth_name|
- result << subject.send(meth_name) if use_in_hash? meth_name
- result
- end
- end
-
- private
-
- def permission_engine
- CanTango.config.engine(:user_ac)
- end
-
- def use_in_hash? meth_name
- subject.respond_to?(meth_name) && CanTango.config.permits.enabled_types.include?(meth_map[meth_name])
- end
-
- def meth_map
- {:roles_list => :role, :role_groups_list => :role_group }
- end
- end
- end
-end
View
38 lib/cantango/ability_cache/kompiler.rb
@@ -1,38 +0,0 @@
-require 'sourcify'
-
-module CanTango
- class AbilityCache
- class Kompiler
- class CacheMarshallError < StandardError; end
-
- def compile! rules_raw
- return if !rules_raw
- rules_compiled = rules_raw.map do |rule|
- rule.block = rule.block.to_source if rule.block.class == Proc && sourcify?
- rule
- end
- end
-
- def decompile! rules_compiled
- return if !rules_compiled
- rules_raw = rules_compiled.map do |rule|
- compiler_check! rule
- rule.block = eval("#{rule.block}") if sourcify?
- rule
- end
- end
-
- protected
-
- def sourcify?
- @sourcify ||= Proc.new {}.respond_to?(:to_source)
- end
-
- def compiler_check! rule
- if rule.block && !CanTango.config.adapters.registered?(:compiler)
- raise "You can NOT marshal dynamic rules (with Procs) unless you register the :compiler adapter"
- end
- end
- end
- end
-end
View
32 lib/cantango/ability_cache/reader.rb
@@ -1,32 +0,0 @@
-module CanTango
- class AbilityCache
- class Reader
- include CanTango::Helpers::Debug
-
- attr_reader :cache
-
- def initialize cache
- @cache = cache
- end
-
- def prepared_rules
- debug "reading rules from cache: #{cache.key}"
- cache.compile_on? ? compiler.decompile!(loaded_rules) : loaded_rules
- end
-
- protected
-
- def loaded_rules
- rules_cache.load(cache.key)
- end
-
- def compiler
- cache.compiler
- end
-
- def rules_cache
- cache.rules_cache
- end
- end
- end
-end
View
27 lib/cantango/cached_ability.rb
@@ -1,27 +0,0 @@
-module CanTango
- class CachedAbility < Ability
- # Equivalent to a CanCan Ability#initialize call
- # which executes all the permission logic
- def initialize candidate, options = {}
- raise "Candidate must be something!" if !candidate
- @candidate, @options = candidate, options
-
- # return if cached_rules?
-
- clear_rules!
- permit_rules
-
- execute_engines! if engines_on?
-
- # cache_rules!
- end
-
- def cached?
- true
- end
-
- def permit_rules
- end
- end
-end
-

0 comments on commit a29e61c

Please sign in to comment.
Something went wrong with that request. Please try again.