Class: Etna::Permissions

Inherits:
Object
  • Object
show all
Defined in:
lib/etna/permissions.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(permissions) ⇒ Permissions

Returns a new instance of Permissions.



3
4
5
# File 'lib/etna/permissions.rb', line 3

def initialize(permissions)
  @permissions = permissions
end

Class Method Details

.from_encoded_permissions(encoded_permissions) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/etna/permissions.rb', line 7

def self.from_encoded_permissions(encoded_permissions)
  perms = encoded_permissions.split(/\;/).map do |roles|
    role, projects = roles.split(/:/)

    projects.split(/\,/).reduce([]) do |perms, project_name|
      perms << Etna::Permission.new(role, project_name)
    end
  end.flatten

  Etna::Permissions.new(perms)
end

.from_hash(permissions_hash) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/etna/permissions.rb', line 19

def self.from_hash(permissions_hash)
  perms = permissions_hash.map do |project_name, role_hash|
    Etna::Permission.new(
      Etna::Role.new(role_hash[:role], role_hash[:restricted]).key,
      project_name
    )
  end

  Etna::Permissions.new(perms)
end

Instance Method Details

#add_permission(permission) ⇒ Object



47
48
49
50
51
# File 'lib/etna/permissions.rb', line 47

def add_permission(permission)
  return if current_project_names.include?(permission.project_name)

  @permissions << permission
end

#any?(&block) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/etna/permissions.rb', line 53

def any?(&block)
  @permissions.any?(&block)
end

#to_hashObject



41
42
43
44
45
# File 'lib/etna/permissions.rb', line 41

def to_hash
  @permissions_hash ||= @permissions.map do |permission|
    [permission.project_name, permission.to_hash]
  end.to_h
end

#to_stringObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/etna/permissions.rb', line 30

def to_string
  @permissions_string ||= @permissions.group_by(&:role_key)
    .sort_by(&:first)
    .map do |role_key, permissions|
    [
      role_key,
      permissions.map(&:project_name).sort.join(","),
    ].join(":")
  end.join(";")
end