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)



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

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

Instance Method Details

#add(role) ⇒ Object

add a role (Role instance)



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

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)


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

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

#delete(role) ⇒ Object

delete a role (Role instance)



139
140
141
142
# File 'lib/access/role.rb', line 139

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

#each(&block) ⇒ Object

Iterate over the roles



150
151
152
# File 'lib/access/role.rb', line 150

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

#inspectObject

:nodoc:



160
161
162
163
164
165
166
# File 'lib/access/role.rb', line 160

def inspect # :nodoc:
	"#<%s:0x%08x roles=%s>" %  [
		self.class,
		object_id << 1,
		@roles.map { |r| r.id }.join(', '),
	]
end

#listObject

all roles



145
146
147
# File 'lib/access/role.rb', line 145

def list
	@roles.dup
end

#storableObject

:nodoc: prepare for storage



156
157
158
# File 'lib/access/role.rb', line 156

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