Class: Access::Roles

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/access/role.rb

Overview

A list of roles

Instance Method Summary collapse

Methods included from Enumerable

#join

Constructor Details

#initialize(owner, roles = nil) ⇒ Roles

owner must be capable of #save roles is the list of Role instances (array)



98
99
100
101
# File 'lib/access/role.rb', line 98

def initialize(owner, roles=nil)
	@owner = owner
	@roles = roles || []
end

Instance Method Details

#add(role) ⇒ Object

add a role (Role instance)



110
111
112
113
# File 'lib/access/role.rb', line 110

def add(role)
	@roles << role
	@owner.save
end

#allow?(privilege, condition = nil) ⇒ Boolean

Tests if any of the roles in self allows a privilege under given conditions (may be nil to indicate no condition)

Returns:

  • (Boolean)


105
106
107
# File 'lib/access/role.rb', line 105

def allow?(privilege, condition=nil)
	@roles.any? { |role| role.allows?(privilege, condition) }
end

#delete(role) ⇒ Object

delete a role (Role instance)



116
117
118
119
# File 'lib/access/role.rb', line 116

def delete(role)
	@roles.delete(role)
	@owner.save
end

#each(&block) ⇒ Object

Iterate over the roles



127
128
129
# File 'lib/access/role.rb', line 127

def each(&block)
	@roles.each(&block)
end

#listObject

all roles



122
123
124
# File 'lib/access/role.rb', line 122

def list
	@roles.dup
end

#storableObject

:nodoc: prepare for storage



133
134
135
# File 'lib/access/role.rb', line 133

def storable
	@roles.map { |role| role.id }
end