Class: Roadblock::Stack
- Inherits:
-
Object
- Object
- Roadblock::Stack
- Defined in:
- lib/roadblock/stack.rb
Constant Summary collapse
- NULL_AUTHORIZER_PROC =
lambda { |object| false }
Instance Method Summary collapse
-
#add(*auths) ⇒ Array<Authorizer>
Adds one or more authorizers to the Stack.
-
#can?(action, objects) ⇒ true, false
Checks if the given action can be performed on all the objects.
-
#initialize(auth_object, scopes: []) ⇒ self
constructor
The Stack allows you to create middleware layers of authorizer to reduce duplication, escape early, etc.
- #method_missing(method, *args) ⇒ Object
- #respond_to?(method) ⇒ Boolean
Constructor Details
#initialize(auth_object, scopes: []) ⇒ self
The Stack allows you to create middleware layers of authorizer to reduce duplication, escape early, etc.
13 14 15 16 17 |
# File 'lib/roadblock/stack.rb', line 13 def initialize(auth_object, scopes: []) self.auth_object = auth_object self.scopes = scopes self. = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/roadblock/stack.rb', line 56 def method_missing(method, *args) match = /can_(.*?)\?/.match(method) if match can?(match[1], *args) else super end end |
Instance Method Details
#add(*auths) ⇒ Array<Authorizer>
Adds one or more authorizers to the Stack.
24 25 26 |
# File 'lib/roadblock/stack.rb', line 24 def add(*auths) self. = + auths end |
#can?(action, objects) ⇒ true, false
Checks if the given action can be performed on all the objects.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/roadblock/stack.rb', line 34 def can?(action, objects) objects = [*objects] stack = .reverse.inject(NULL_AUTHORIZER_PROC) do |, | lambda { |obj| = .new(auth_object, scopes: scopes) .can?(action, obj) do |inner_obj| .send("can_#{action}?", inner_obj, &) end } end objects .map { |object| stack.call(object) } .all? end |
#respond_to?(method) ⇒ Boolean
52 53 54 |
# File 'lib/roadblock/stack.rb', line 52 def respond_to?(method) /can_(.*?)\?/.match(method) ? true : false end |