Class: CanTango::PermissionEngine::Loader::Permissions

Inherits:
Base
  • Object
show all
Includes:
ClassMethods
Defined in:
lib/cantango/permission_engine/loader/permissions.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#file_name

Instance Method Summary collapse

Methods inherited from Base

inherited, #yml_content

Constructor Details

#initialize(file_name) ⇒ Permissions

Returns a new instance of Permissions.



7
8
9
10
# File 'lib/cantango/permission_engine/loader/permissions.rb', line 7

def initialize file_name
  self.file_name = file_name
  load!
end

Instance Attribute Details

#permissionsObject

Returns the value of attribute permissions.



5
6
7
# File 'lib/cantango/permission_engine/loader/permissions.rb', line 5

def permissions
  @permissions
end

Instance Method Details

#load!Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cantango/permission_engine/loader/permissions.rb', line 26

def load!
  return if yml_content.empty?
  yml_content.each do |type, groups|
    (permissions[type] = {} # This is for having fx empty users: section 
     next) if groups.nil?   #
    groups.each do |group, rules|
      permissions[type] ||= {}
      parser.parse(group, rules) do |permission|
        permissions[type][permission.name] = permission
      end
    end
  end
rescue => e
  raise "PermissionsLoader Error: The permissions for the file #{file_name} could not be loaded - cause was #{e}"
end

#load_from_hash(hash) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cantango/permission_engine/loader/permissions.rb', line 12

def load_from_hash hash
  return if hash.empty?
  hash.each do |type, groups|
    groups.each do |group, rules|
      permissions[type] ||= {}
      parser.parse(group, rules) do |permission|
        permissions[type][permission.name] = permission
      end
    end
  end
rescue => e
  raise "PermissionsLoader Error: The permissions for the file #{file_name} could not be loaded - cause was #{e}"
end

#parserObject



46
47
48
# File 'lib/cantango/permission_engine/loader/permissions.rb', line 46

def parser
  @parser ||= CanTango::PermissionEngine::Parser::Permissions.new
end