Class: DeclarativePolicy::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/declarative_policy/configuration.rb

Constant Summary collapse

ConfigurationError =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



7
8
9
10
11
# File 'lib/declarative_policy/configuration.rb', line 7

def initialize
  @named_policies = {}
  @name_transformation = ->(name) { "#{name}Policy" }
  @class_for = ->(name) { Object.const_get(name) }
end

Instance Method Details

#class_for(&block) ⇒ Object



30
31
32
33
# File 'lib/declarative_policy/configuration.rb', line 30

def class_for(&block)
  @class_for = block
  nil
end

#name_transformation(&block) ⇒ Object



25
26
27
28
# File 'lib/declarative_policy/configuration.rb', line 25

def name_transformation(&block)
  @name_transformation = block
  nil
end

#named_policy(name, policy = nil) ⇒ Object



13
14
15
16
17
# File 'lib/declarative_policy/configuration.rb', line 13

def named_policy(name, policy = nil)
  @named_policies[name] = policy if policy

  @named_policies[name] || raise(ConfigurationError, "No #{name} policy configured")
end

#nil_policy(policy = nil) ⇒ Object



19
20
21
22
23
# File 'lib/declarative_policy/configuration.rb', line 19

def nil_policy(policy = nil)
  @nil_policy = policy if policy

  @nil_policy || ::DeclarativePolicy::NilPolicy
end

#policy_class(domain_class_name) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/declarative_policy/configuration.rb', line 35

def policy_class(domain_class_name)
  return unless domain_class_name

  @class_for.call((@name_transformation.call(domain_class_name)))
rescue NameError
  nil
end