Total Complexity | 2 |
Total Lines | 32 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | # frozen_string_literal: true |
||
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 |
||
48 |