RoleTree   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 3 1
A contains() 0 13 1
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 Client
10
          # Internal structure for client roles
11
          class RoleTree
12
            include Mixin::Entity
13
14
            attribute :root, [Hash, K: Symbol, V: [:*, NilClass]], default: {}
15
16
            normalizer_block do |entity, *|
17
              entity.root
18
            end
19
20
            denormalizer_block do |input, *|
21
              break RoleTree.new(input) if input.is_a?(Hash)
22
              raise "Expected Hash, received #{input.class}"
23
            end
24
25
            def initialize(root = {})
26
              @root = root
27
            end
28
29
            def contains(path)
30
              cursor = @root
31
              path.each do |segment|
32
                return false unless cursor.respond_to?(:key?)
33
                candidates = [segment.to_s, segment.to_sym]
34
                next_segment = candidates.find do |candidate|
35
                  cursor.key?(candidate)
36
                end
37
                return false unless next_segment
38
                cursor = cursor[next_segment]
39
              end
40
              true
41
            end
42
          end
43
        end
44
      end
45
    end
46
  end
47
end
48