Class: Access::RoleList

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

Overview

A list of roles

Instance Method Summary collapse

Methods included from Enumerable

#join

Constructor Details

#initialize(owner, roles = nil) ⇒ RoleList

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



20
21
22
23
24
# File 'lib/access/rolelist.rb', line 20

def initialize(owner, roles=nil)
	@owner  = owner
	@roles  = {}
	roles.each { |role| @roles[role] = @owner.access.role[role] } if roles
end

Instance Method Details

#accessObject

:nodoc:



72
73
74
# File 'lib/access/rolelist.rb', line 72

def access
	@owner.access
end

#add(role) ⇒ Object

add a role (Role instance)



33
34
35
36
# File 'lib/access/rolelist.rb', line 33

def add(role)
	@roles[role.oid] = role
	@owner.save
end

#add_oid(role_oid) ⇒ Object



38
39
40
41
42
# File 'lib/access/rolelist.rb', line 38

def add_oid(role_oid)
	role = @owner.access.role[role_oid]
	raise "Role #{role_oid} does not exist" unless role
	add(role)
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)


28
29
30
# File 'lib/access/rolelist.rb', line 28

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

#each(&block) ⇒ Object

Iterate over the roles (oid & role) :yield: role_oid, role



51
52
53
# File 'lib/access/rolelist.rb', line 51

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

#each_oid(&block) ⇒ Object

Iterate over the role oids



56
57
58
# File 'lib/access/rolelist.rb', line 56

def each_oid(&block)
	@roles.each_key(&block)
end

#each_role(&block) ⇒ Object

Iterate over the roles



61
62
63
# File 'lib/access/rolelist.rb', line 61

def each_role(&block)
	@roles.each_value(&block)
end

#inspectObject

:nodoc:



91
92
93
94
95
96
97
# File 'lib/access/rolelist.rb', line 91

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

#listObject

all roles



45
46
47
# File 'lib/access/rolelist.rb', line 45

def list
	@roles.dup
end

#remove(role) ⇒ Object

delete a role (Role instance)



77
78
79
# File 'lib/access/rolelist.rb', line 77

def remove(role)
	remove_oid(role.oid)
end

#remove_oid(role_oid) ⇒ Object

delete a role by its oid



82
83
84
85
# File 'lib/access/rolelist.rb', line 82

def remove_oid(role_oid)
	@roles.delete(role_oid)
	@owner.save
end

#saveObject



87
88
89
# File 'lib/access/rolelist.rb', line 87

def save
	@owner.save
end

#storableObject

:nodoc: prepare for storage



67
68
69
# File 'lib/access/rolelist.rb', line 67

def storable
	@roles.map { |oid, role| oid }
end