Class: Regulator::PolicyFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/regulator/policy_finder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, controller_or_namespace = nil) ⇒ PolicyFinder

Returns a new instance of PolicyFinder.



7
8
9
10
11
12
13
14
15
16
# File 'lib/regulator/policy_finder.rb', line 7

def initialize(object, controller_or_namespace = nil)
  @object = object

  if controller_or_namespace.is_a?(Module)
    @namespace = controller_or_namespace
  elsif controller_or_namespace
    @controller = controller_or_namespace
    @namespace  = @controller.policy_namespace
  end
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



4
5
6
# File 'lib/regulator/policy_finder.rb', line 4

def controller
  @controller
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



5
6
7
# File 'lib/regulator/policy_finder.rb', line 5

def namespace
  @namespace
end

#objectObject (readonly)

Returns the value of attribute object.



3
4
5
# File 'lib/regulator/policy_finder.rb', line 3

def object
  @object
end

Instance Method Details

#policyObject



24
25
26
27
28
29
30
# File 'lib/regulator/policy_finder.rb', line 24

def policy
  klass = find
  klass = klass.constantize if klass.is_a?(String)
  klass
rescue NameError
  nil
end

#policy!Object

Raises:



37
38
39
40
# File 'lib/regulator/policy_finder.rb', line 37

def policy!
  raise NotDefinedError, "unable to find policy of nil" if object.nil?
  policy or raise NotDefinedError, "unable to find policy `#{find}` for `#{object.inspect}`"
end

#scopeObject



18
19
20
21
22
# File 'lib/regulator/policy_finder.rb', line 18

def scope
  policy::Scope if policy
rescue NameError
  nil
end

#scope!Object

Raises:



32
33
34
35
# File 'lib/regulator/policy_finder.rb', line 32

def scope!
  raise NotDefinedError, "unable to find policy scope of nil" if object.nil?
  scope or raise NotDefinedError, "unable to find scope `#{find}::Scope` for `#{object.inspect}`"
end