Class: Allowy::Registry
- Inherits:
-
Object
- Object
- Allowy::Registry
- Defined in:
- lib/allowy/registry.rb
Instance Method Summary collapse
- #access_control_for(subject) ⇒ Object
- #access_control_for!(subject) ⇒ Object
- #class_for(name) ⇒ Object
-
#initialize(ctx) ⇒ Registry
constructor
A new instance of Registry.
Constructor Details
#initialize(ctx) ⇒ Registry
Returns a new instance of Registry.
3 4 5 6 |
# File 'lib/allowy/registry.rb', line 3 def initialize(ctx) @context = ctx @registry = {} end |
Instance Method Details
#access_control_for(subject) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/allowy/registry.rb', line 14 def access_control_for(subject) return unless subject # Try subject as an object clazz = class_for "#{subject.class.name}Access" # Try subject as a class clazz = class_for "#{subject.name}Access" if !clazz && subject.is_a?(Class) return unless clazz # No luck this time # create a new instance or return existing @registry[clazz] ||= clazz.new(@context) end |
#access_control_for!(subject) ⇒ Object
8 9 10 11 12 |
# File 'lib/allowy/registry.rb', line 8 def access_control_for!(subject) ac = access_control_for subject raise UndefinedAccessControl.new("Please define Access Control class for #{subject.inspect}") unless ac ac end |
#class_for(name) ⇒ Object
27 28 29 |
# File 'lib/allowy/registry.rb', line 27 def class_for(name) name.constantize rescue nil #TODO: Handle just the NameError end |