Class: Acl::Resource::Registry
- Inherits:
-
Object
- Object
- Acl::Resource::Registry
- Defined in:
- lib/acl.rb
Instance Attribute Summary collapse
-
#resources ⇒ Object
readonly
Returns the value of attribute resources.
Instance Method Summary collapse
- #add(resource, parent = nil) ⇒ Object
- #get(resource) ⇒ Object
- #get_parent(resource) ⇒ Object
- #has?(resource) ⇒ Boolean
- #inherits?(resource, inherit, only_parent = false) ⇒ Boolean
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
- #move(old_parent, new_parent) ⇒ Object
- #remove(resource) ⇒ Object
- #remove_all ⇒ Object
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
5 6 7 |
# File 'lib/acl.rb', line 5 def initialize @resources = {} end |
Instance Attribute Details
#resources ⇒ Object (readonly)
Returns the value of attribute resources.
4 5 6 |
# File 'lib/acl.rb', line 4 def resources @resources end |
Instance Method Details
#add(resource, parent = nil) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/acl.rb', line 14 def add(resource, parent=nil) if self.has?(resource) raise ArgumentError, "#{resource} existiert leider schon" end unless parent.nil? @resources[parent.to_sym][:children].merge!(resource.to_sym => resource) parent = self.get(parent) end @resources.merge!({resource.to_sym => { :instance => resource, :parent => parent, :children => {} }}) end |
#get(resource) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/acl.rb', line 28 def get(resource) unless self.has?(resource) raise ArgumentError, "#{resource} existiert leider nicht" end @resources[resource.to_sym][:instance] end |
#get_parent(resource) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/acl.rb', line 8 def get_parent(resource) unless self.has?(resource) raise ArgumentError, "#{resource} existiert leider nicht" end @resources[@resources[resource][:parent]][:instance] end |
#has?(resource) ⇒ Boolean
34 35 36 |
# File 'lib/acl.rb', line 34 def has?(resource) @resources.has_key? resource.to_sym end |
#inherits?(resource, inherit, only_parent = false) ⇒ Boolean
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/acl.rb', line 37 def inherits?(resource, inherit, only_parent=false) unless @resources[resource][:parent].nil? parent = self.get_parent(resource) if parent == inherit return true elsif only_parent return false end while !@resources[parent][:parent].nil? parent = self.get_parent(parent) if parent == inherit return true end end end return false end |
#move(old_parent, new_parent) ⇒ Object
68 69 70 |
# File 'lib/acl.rb', line 68 def move(old_parent, new_parent) end |
#remove(resource) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/acl.rb', line 54 def remove(resource) unless @resources[resource.to_sym][:parent].nil? @resources[@resources[resource.to_sym][:parent].to_sym][:children].delete(resource.to_sym) end if @resources[resource.to_sym][:children].respond_to?('each_key') @resources[resource.to_sym][:children].each_key do |children_id| self.remove(children_id) end end @resources.delete(resource.to_sym) end |
#remove_all ⇒ Object
65 66 67 |
# File 'lib/acl.rb', line 65 def remove_all() @resources = {} end |