Total Complexity | 3 |
Total Lines | 38 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | # frozen_string_literal: true |
||
11 | class Filter |
||
12 | include Mixin::Entity |
||
13 | |||
14 | attribute :roles, [Enumerable, T: [Enumerable, T: String]] |
||
15 | |||
16 | normalizer_block do |entity, *| |
||
17 | entity.roles.map do |path| |
||
18 | path.join('.') |
||
19 | end .join('+') |
||
20 | end |
||
21 | |||
22 | denormalizer_block do |input, *| |
||
23 | break input if input.is_a?(Filter.class) |
||
24 | unless input.is_a?(String) |
||
25 | raise "Expected String, got #{input.class}" |
||
26 | end |
||
27 | Filter.new.tap do |instance| |
||
28 | instance.roles = input.split('+').map do |role| |
||
29 | role.split('.').map(&:strip).map(&:to_sym).reject(&:empty?) |
||
30 | end |
||
31 | end |
||
32 | end |
||
33 | |||
34 | def initialize(roles = []) |
||
35 | self.roles = roles |
||
36 | end |
||
37 | |||
38 | # @param [AMA::Chef::User::Model::Client] account |
||
39 | def apply(account) |
||
40 | roles.all? do |role| |
||
41 | account.roles.contains(role) |
||
42 | end |
||
43 | end |
||
44 | |||
45 | def to_s |
||
46 | "Partition.Filter `#{normalize}`" |
||
47 | end |
||
48 | end |
||
49 | end |
||
54 |