Class: Roadblock::Stack
- Inherits:
-
Object
show all
- Defined in:
- lib/roadblock/stack.rb
Constant Summary
collapse
- NULL_AUTHORIZER_PROC =
lambda { |object| false }
Instance Method Summary
collapse
Constructor Details
#initialize(auth_object, scopes: []) ⇒ Stack
Returns a new instance of Stack.
5
6
7
8
9
|
# File 'lib/roadblock/stack.rb', line 5
def initialize(auth_object, scopes: [])
self.auth_object = auth_object
self.scopes = scopes
self.authorizers = []
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
37
38
39
40
41
42
43
44
45
|
# File 'lib/roadblock/stack.rb', line 37
def method_missing(method, *args)
match = /can_(.*?)\?/.match(method)
if match
can?(match[1], *args)
else
super
end
end
|
Instance Method Details
#add(*auths) ⇒ Object
11
12
13
|
# File 'lib/roadblock/stack.rb', line 11
def add(*auths)
self.authorizers = authorizers + auths
end
|
#can?(action, objects) ⇒ Boolean
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/roadblock/stack.rb', line 15
def can?(action, objects)
objects = [*objects]
stack = authorizers.reverse.inject(NULL_AUTHORIZER_PROC) do |authorizer_proc, authorizer_klass|
lambda { |obj|
authorizer = authorizer_klass.new(auth_object, scopes: scopes)
authorizer.can?(action, obj) do |inner_obj|
authorizer.send("can_#{action}?", inner_obj, &authorizer_proc)
end
}
end
objects
.map { |object| stack.call(object) }
.all?
end
|
#respond_to?(method) ⇒ Boolean
33
34
35
|
# File 'lib/roadblock/stack.rb', line 33
def respond_to?(method)
/can_(.*?)\?/.match(method) ? true : false
end
|