Class: Allowy::Registry

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

Instance Method Summary collapse

Constructor Details

#initialize(ctx, options = {}) ⇒ Registry

Returns a new instance of Registry.



3
4
5
6
7
8
# File 'lib/allowy/registry.rb', line 3

def initialize(ctx, options={})
  options.assert_valid_keys(:access_suffix)
  @context = ctx
  @registry = {}
  @options = options
end

Instance Method Details

#access_control_for(subject) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/allowy/registry.rb', line 16

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

  # Try subject as an object
  clazz = class_for subject.class.name unless clazz

  # Try subject as a class
  clazz = class_for subject.name 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



10
11
12
13
14
# File 'lib/allowy/registry.rb', line 10

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