Class: Rolypoly::RoleGatekeeper

Inherits:
Object
  • Object
show all
Defined in:
lib/rolypoly/role_gatekeeper.rb

Instance Method Summary collapse

Constructor Details

#initialize(roles, actions) ⇒ RoleGatekeeper

Returns a new instance of RoleGatekeeper.



4
5
6
7
8
9
# File 'lib/rolypoly/role_gatekeeper.rb', line 4

def initialize(roles, actions)
  self.roles = Set.new Array(roles).map(&:to_s)
  self.actions = Set.new Array(actions).map(&:to_s)
  self.all_actions = false
  self.public = false
end

Instance Method Details

#all_publicObject



38
39
40
41
# File 'lib/rolypoly/role_gatekeeper.rb', line 38

def all_public
  self.public = true
  self.all_actions = true
end

#allow?(current_roles, action) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/rolypoly/role_gatekeeper.rb', line 33

def allow?(current_roles, action)
  action?(action) &&
    role?(current_roles)
end

#to(*roles) ⇒ Object

restrict(*actions).to *roles



12
13
14
# File 'lib/rolypoly/role_gatekeeper.rb', line 12

def to(*roles)
  self.roles = self.roles.merge roles.flatten.compact.map(&:to_s)
end

#to_access(*actions) ⇒ Object

allow(*roles).to_access *actions



23
24
25
# File 'lib/rolypoly/role_gatekeeper.rb', line 23

def to_access(*actions)
  self.actions = self.actions.merge actions.flatten.compact.map(&:to_s)
end

#to_allObject

allow role access to all actions allow(*roles).to_all



29
30
31
# File 'lib/rolypoly/role_gatekeeper.rb', line 29

def to_all
  self.all_actions = true
end

#to_noneObject

make actions public basically restrict(:index).to_none



18
19
20
# File 'lib/rolypoly/role_gatekeeper.rb', line 18

def to_none
  self.public = true
end