Group   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 39
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A merge() 0 7 2
A policy=() 0 3 1
A initialize() 0 6 1
A to_s() 0 3 1
1
# frozen_string_literal: true
2
3
require 'set'
4
5
require_relative '../mixin/entity'
6
require_relative 'policy'
7
require_relative 'privilege'
8
9
module AMA
10
  module Chef
11
    module User
12
      module Model
13
        # Represents Linux user group
14
        class Group
15
          include Mixin::Entity
16
17
          attribute :id, Symbol
18
          attribute :members, [Set, T: Symbol], default: Set.new
19
          attribute :privileges, [Hash, K: Symbol, V: Privilege], default: {}
20
          attribute :policy, Policy, default: Policy::NONE
21
22
          denormalizer_block do |input, type, context, &block|
23
            if input.is_a?(Hash) && [:id, 'id'].none? { |key| input.key?(key) }
24
              input[:id] = context.path.current.name
25
            end
26
            block.call(input, type, context)
27
          end
28
29
          def initialize(id = nil)
30
            @id = id
31
            @members = Set.new
32
            @privileges = {}
33
            @policy = Policy::NONE
34
          end
35
36
          def policy=(policy)
37
            @policy = Policy.wrap(policy)
38
          end
39
40
          # @param [Group] other
41
          def merge(other)
42
            raise 'Different ids' unless id == other.id
43
            members.merge(other.members)
44
            privileges.merge!(other.privileges)
45
            self.policy = [policy, other.policy].max
46
            self
47
          end
48
49
          def to_s
50
            "Group :#{id}"
51
          end
52
        end
53
      end
54
    end
55
  end
56
end
57