Total Complexity | 1 |
Total Lines | 26 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | # frozen_string_literal: true |
||
9 | class Privilege |
||
10 | include Mixin::Entity |
||
11 | |||
12 | attribute :type, Symbol |
||
13 | attribute :options, [Hash, K: Symbol, V: [:*, NilClass]], default: {} |
||
14 | |||
15 | denormalizer_block do |input, type, context, &block| |
||
16 | input = {} unless input.is_a?(Hash) |
||
17 | target = {} |
||
18 | keys = ['type', 'options', :type, :options] |
||
19 | keys.each do |key| |
||
20 | target[key.to_sym] = input[key] if input[key] |
||
21 | end |
||
22 | target[:options] = {} unless target[:options].is_a?(Hash) |
||
23 | input.each do |key, value| |
||
24 | next if keys.include?(key) |
||
25 | target[:options][key] = value |
||
26 | end |
||
27 | target[:type] = context.path.current.name unless target.key?(:type) |
||
28 | block.call(target, type, context) |
||
29 | end |
||
30 | |||
31 | def to_s |
||
32 | "Privilege :#{type}" |
||
33 | end |
||
34 | end |
||
35 | end |
||
39 |