Class: Allowy::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/allowy/registry.rb

Instance Method Summary collapse

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
26
27
# File 'lib/allowy/registry.rb', line 14

def access_control_for(subject)
  # Try subject as decorated object
  clazz = class_for "#{subject.class.source_class.name}Access" if subject.class.respond_to?(:source_class)

  # Try subject as an object
  clazz = class_for "#{subject.class.name}Access" unless clazz

  # 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



29
30
31
# File 'lib/allowy/registry.rb', line 29

def class_for(name)
  name.safe_constantize
end