Filter.initialize()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
1
# frozen_string_literal: true
2
3
require_relative '../../mixin/entity'
4
5
module AMA
6
  module Chef
7
    module User
8
      module Model
9
        class Partition
10
          # Role filter for matching clients that fit into partition
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
50
      end
51
    end
52
  end
53
end
54