Class: Rend::Acl::Role::Registry
- Inherits:
-
Object
- Object
- Rend::Acl::Role::Registry
- Includes:
- Core::Helpers::Php
- Defined in:
- lib/rend/acl/role/registry.rb,
lib/rend/acl/role/registry/exception.rb
Defined Under Namespace
Classes: Exception
Instance Attribute Summary collapse
-
#roles ⇒ Object
Internal Role registry data storage.
Instance Method Summary collapse
-
#add!(role, parents = nil) ⇒ Object
Adds a Role having an identifier unique to the registry.
-
#get!(role) ⇒ Object
Returns the identified Role.
-
#has?(role) ⇒ Boolean
Returns true if and only if the Role exists in the registry.
-
#inherits?(role, inherit, only_parents = false) ⇒ Boolean
Returns true if and only if role inherits from inherit.
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
-
#parents(role) ⇒ Object
Returns an array of an existing Role’s parents.
-
#remove!(role) ⇒ Object
Removes the Role from the registry.
- #remove_all! ⇒ Object
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
12 13 14 |
# File 'lib/rend/acl/role/registry.rb', line 12 def initialize self.roles = {} end |
Instance Attribute Details
#roles ⇒ Object
Internal Role registry data storage
10 11 12 |
# File 'lib/rend/acl/role/registry.rb', line 10 def roles @roles end |
Instance Method Details
#add!(role, parents = nil) ⇒ Object
Adds a Role having an identifier unique to the registry
The parents parameter may be a reference to, or the string identifier for, a Role existing in the registry, or parents may be passed as an array of these - mixing string identifiers and objects is ok - to indicate the Roles from which the newly added Role will directly inherit.
In order to resolve potential ambiguities with conflicting rules inherited from different parents, the most recently added parent takes precedence over parents that were previously added. In other words, the first parent added will have the least priority, and the last parent added will have the highest priority.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rend/acl/role/registry.rb', line 33 def add!(role, parents = nil) type_hint! Rend::Acl::Role, role, :is_required => true role_id = role.id raise Exception, "Role id 'role_id' already exists in the registry" if has?(role_id) role_parents = {} if parents Array(parents).each do |parent| begin role_parent_id = (parent.class <= Rend::Acl::Role) ? parent.id : parent role_parent = get!(role_parent_id) rescue Exception raise Exception, "Parent Role id 'role_parent_id' does not exist" end role_parents[role_parent_id] = role_parent roles[role_parent_id][:children][role_id] = role # roles[role_parent_id][:instance].children[role_id] = role # future end end # role.parents = role_parents -- future roles[role_id] = { :instance => role, :parents => role_parents, :children => {} } self end |
#get!(role) ⇒ Object
Returns the identified Role
The role parameter can either be a Role or a Role identifier.
74 75 76 77 78 |
# File 'lib/rend/acl/role/registry.rb', line 74 def get!(role) raise Exception, "Role 'role_id' not found" unless has?(role) role_id = (role.class <= Rend::Acl::Role) ? role.id : role.to_s roles[role_id][:instance] end |
#has?(role) ⇒ Boolean
Returns true if and only if the Role exists in the registry
The role parameter can either be a Role or a Role identifier.
87 88 89 90 |
# File 'lib/rend/acl/role/registry.rb', line 87 def has?(role) role_id = (role.class <= Rend::Acl::Role) ? role.id : role.to_s roles.has_key?(role_id) end |
#inherits?(role, inherit, only_parents = false) ⇒ Boolean
Returns true if and only if role inherits from inherit
Both parameters may be either a Role or a Role identifier. If only_parents is true, then role must inherit directly from inherit in order to return true. By default, this method looks through the entire inheritance DAG to determine whether role inherits from inherit through its ancestor Roles.
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/rend/acl/role/registry.rb', line 123 def inherits?(role, inherit, only_parents = false) role_id = get!(role).id inherit_id = get!(inherit).id inherits = roles[role_id][:parents].has_key?(inherit_id) return inherits if inherits || only_parents roles[role_id][:parents].each do |parent_id, parent| return true if inherits?(parent_id, inherit_id) end false end |
#parents(role) ⇒ Object
Returns an array of an existing Role’s parents
The array keys are the identifiers of the parent Roles, and the values are the parent Role instances. The parent Roles are ordered in this array by ascending priority. The highest priority parent Role, last in the array, corresponds with the parent Role most recently added.
If the Role does not have any parents, then an empty array is returned.
105 106 107 |
# File 'lib/rend/acl/role/registry.rb', line 105 def parents(role) roles[get!(role).id][:parents] end |
#remove!(role) ⇒ Object
Removes the Role from the registry
The role parameter can either be a Role or a Role identifier.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/rend/acl/role/registry.rb', line 144 def remove!(role) role_id = get!(role).id roles[role_id][:children].each do |child_id, child| roles[child_id][:parents].delete(role_id) end roles[role_id][:parents].each do |parent_id, parent| roles[parent_id][:children][role_id] end roles.delete(role_id) self end |
#remove_all! ⇒ Object
160 161 162 163 |
# File 'lib/rend/acl/role/registry.rb', line 160 def remove_all! roles.replace({}) self end |