PrivateKey.merge()   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
dl 0
loc 6
rs 9.2
1
# frozen_string_literal: true
2
3
require 'digest'
4
5
require_relative '../mixin/entity'
6
require_relative 'private_key/remote'
7
8
module AMA
9
  module Chef
10
    module User
11
      module Model
12
        class PrivateKey
13
          include Mixin::Entity
14
15
          attribute :id, Symbol
16
          attribute :owner, Symbol
17
          attribute :type, Symbol, nullable: true
18
          attribute :content, String, sensitive: true, nullable: true
19
          attribute :digest, String
20
          attribute :public_key, String, sensitive: true, nullable: true
21
          attribute :passphrase, String, sensitive: true, nullable: true
22
          attribute :remotes, [Hash, K: Symbol, V: Remote], default: {}
23
          attribute :validate, TrueClass, FalseClass, default: true
24
          attribute :install_public_key, TrueClass, FalseClass, default: false
25
26
          denormalizer_block do |input, type, context, &block|
27
            input = { content: input } if input.is_a?(String)
28
            raise "Hash expected, got #{input.class}" unless input.is_a?(Hash)
29
            { id: -1, owner: -3 }.each do |key, index|
30
              if [key, key.to_s].none? { |v| input.key?(v) }
31
                segment = context.path.segments[index]
32
                input[key] = segment.name unless segment.nil?
33
              end
34
            end
35
            block.call(input, type, context)
36
          end
37
38
          normalizer_block do |entity, type, context, &block|
39
            normalized = block.call(entity, type, context)
40
            if context.include_sensitive_attributes
41
              normalized[:content] = entity.content
42
            end
43
            normalized
44
          end
45
46
          def content=(content)
47
            return @content = @digest = nil if content.nil?
48
            @digest = Digest::MD5.hexdigest(content)
49
            @content = content
50
          end
51
52
          def ==(other)
53
            return false unless other.is_a?(self.class)
54
            @id == other.id && @owner == other.owner && @digest == other.digest
55
          end
56
57
          def merge(other)
58
            @public_key = other.public_key if other.public_key
59
            @remotes.merge!(other.remotes)
60
            @validate = true if other.validate
61
            @install_public_key = true if other.install_public_key
62
          end
63
        end
64
      end
65
    end
66
  end
67
end
68