Total Complexity | 5 |
Total Lines | 39 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | # frozen_string_literal: true |
||
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 |
||
57 |