Class: Acl::Role::Registry
- Inherits:
-
Object
- Object
- Acl::Role::Registry
- Defined in:
- lib/acl.rb
Instance Attribute Summary collapse
-
#roles ⇒ Object
readonly
Returns the value of attribute roles.
Instance Method Summary collapse
-
#add(role, parents = nil) ⇒ Object
will add a role to the hash role is an object of Acl::RoleObject parents will allow an array or a symbol.
-
#get(role) ⇒ Object
return the role.
- #get_parents(role) ⇒ Object
-
#has?(role) ⇒ Boolean
return true if role is in @roles hash.
-
#inherits?(role, inherit, only_parents = false) ⇒ Boolean
return true if role is in inherits? and if only_parents are true we will show only in parents hash of the role.
-
#initialize ⇒ Registry
constructor
initializer.
- #move(old_parent, new_parent) ⇒ Object
-
#remove(role) ⇒ Object
removes the role and will return true or false.
-
#remove_all ⇒ Object
will niling the @roles hash.
Constructor Details
#initialize ⇒ Registry
initializer
77 78 79 |
# File 'lib/acl.rb', line 77 def initialize @roles = {} end |
Instance Attribute Details
#roles ⇒ Object (readonly)
Returns the value of attribute roles.
75 76 77 |
# File 'lib/acl.rb', line 75 def roles @roles end |
Instance Method Details
#add(role, parents = nil) ⇒ Object
will add a role to the hash role is an object of Acl::RoleObject parents will allow an array or a symbol
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/acl.rb', line 83 def add(role, parents=nil) if self.has?(role) raise(ArgumentError, "Die rolle existiert leider schon!") end role_parents = {} if parents != nil unless parents.respond_to?(:each) parent_id = parents.to_sym parents = [parent_id] end parents.each do |parent| current_parent_id = parent.to_sym current_parent_object = self.get current_parent_id role_parents.merge!({current_parent_id => current_parent_object}) @roles[current_parent_id][:children].merge!({role.to_sym => role}) end end @roles.merge!({role.to_sym => { :instance => role, :parents => role_parents, :children => {} }}) end |
#get(role) ⇒ Object
return the role
107 108 109 110 111 112 |
# File 'lib/acl.rb', line 107 def get(role) unless self.has?(role) raise(ArgumentError, "#{role.to_sym} existiert leider nicht!") end @roles[role.to_sym][:instance] end |
#get_parents(role) ⇒ Object
117 118 119 |
# File 'lib/acl.rb', line 117 def get_parents(role) @roles[role.to_sym][:parents] end |
#has?(role) ⇒ Boolean
return true if role is in @roles hash
114 115 116 |
# File 'lib/acl.rb', line 114 def has?(role) @roles.has_key? role.to_sym end |
#inherits?(role, inherit, only_parents = false) ⇒ Boolean
return true if role is in inherits? and if only_parents are true we will show only in parents hash of the role
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/acl.rb', line 123 def inherits?(role, inherit, only_parents=false) begin role_object = self.get(role) #hier müssen wir ordentliches ausnahme handling einbaun rescue return false end role_id = role_object.to_sym inherit_id = self.get(inherit).to_sym inherits = (@roles[role_id][:parents][inherit_id].nil? == true) ? false : true if inherits || only_parents return inherits end @roles[role_id][:parents].each_key do |parent_id| if self.inherits?(parent_id, inherit_id) return true end end return false end |
#move(old_parent, new_parent) ⇒ Object
162 163 164 |
# File 'lib/acl.rb', line 162 def move(old_parent, new_parent) end |
#remove(role) ⇒ Object
removes the role and will return true or false
144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/acl.rb', line 144 def remove(role) role_id = self.get(role).to_sym if @roles[role_id][:children].respond_to?('each_key') @roles[role_id][:children].each_key do |children_id| @roles[children_id][:parents].delete(role_id) end end if @roles[role_id][:parents].respond_to?('each_key') @roles[role_id][:parents].each_key do |parent_id| @roles[parent_id][:children].delete(role_id) end end @roles.delete(role_id) != nil end |
#remove_all ⇒ Object
will niling the @roles hash
159 160 161 |
# File 'lib/acl.rb', line 159 def remove_all @roles = {} end |