Class: PunditRolePlay::Permissions

Inherits:
Object
  • Object
show all
Defined in:
lib/pundit_role_play/permissions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(role) ⇒ Permissions

Returns a new instance of Permissions.



7
8
9
10
11
12
13
14
# File 'lib/pundit_role_play/permissions.rb', line 7

def initialize(role)
  @permissions = role::PERMISSIONS
rescue TypeError => e
  # Handle the case where the role is nil
  raise e unless role.nil?

  @permissions = {}
end

Instance Attribute Details

#permissionsObject (readonly)

Returns the value of attribute permissions.



5
6
7
# File 'lib/pundit_role_play/permissions.rb', line 5

def permissions
  @permissions
end

Instance Method Details

#[](resource) ⇒ Object



23
24
25
# File 'lib/pundit_role_play/permissions.rb', line 23

def [](resource)
  permissions[resource.to_sym]
end

#can?(operation, resource) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
# File 'lib/pundit_role_play/permissions.rb', line 16

def can?(operation, resource)
  # If you can write to a resource, you can also read from it
  return true if permissions[resource.to_sym] == :write

  permissions[resource.to_sym] == operation.to_sym
end