Class: CanI::Authorization
- Inherits:
-
Object
- Object
- CanI::Authorization
- Defined in:
- lib/can_i/authorization.rb
Instance Attribute Summary collapse
-
#auth_role ⇒ Object
readonly
This is the main class to interact with to determine if an action is permitted, or to define permissions.
Instance Method Summary collapse
- #can(action) ⇒ Object
- #can?(action) ⇒ Boolean
- #cannot(action) ⇒ Object
-
#initialize(role = :authorization) ⇒ Authorization
constructor
A new instance of Authorization.
Constructor Details
#initialize(role = :authorization) ⇒ Authorization
Returns a new instance of Authorization.
31 32 33 34 35 36 37 38 39 |
# File 'lib/can_i/authorization.rb', line 31 def initialize(role=:authorization) klass = CanI.const_defined?("#{role}_role".camelize) ? CanI.const_get("#{role}_role".camelize) : Kernel.const_get("#{role}_role".camelize) @auth_role = klass.new auth_role.class.registration_blocks.each do |more_roles| instance_eval &more_roles end end |
Instance Attribute Details
#auth_role ⇒ Object (readonly)
This is the main class to interact with to determine if an action is permitted, or to define permissions
Usage:
class AppDelegate
attr_accessor :authorization
def application(application, didFinishLaunchingWithOptions:launchOptions)
self. = CanI::Authorization.new :admin
true
end
end
class MyController < UIViewController
def viewDidLoad
if App.delegate..can? :get_drunk
App.alert "Let's get drunk!"
else
App.alert "I'm married"
end
end
end
29 30 31 |
# File 'lib/can_i/authorization.rb', line 29 def auth_role @auth_role end |
Instance Method Details
#can(action) ⇒ Object
41 42 43 |
# File 'lib/can_i/authorization.rb', line 41 def can(action) approved_actions << action.to_sym unless approved_actions.include?(action.to_sym) end |
#can?(action) ⇒ Boolean
45 46 47 |
# File 'lib/can_i/authorization.rb', line 45 def can?(action) approved_actions.include? action.to_sym end |
#cannot(action) ⇒ Object
49 50 51 |
# File 'lib/can_i/authorization.rb', line 49 def cannot(action) approved_actions.delete action.to_sym end |