Class: ActiveEncryption::EncryptionSetting::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/active_encryption/encryption_setting/record.rb

Overview

The ActiveEncryption::EncryptionSetting::Record contains the required settings for encrypting and decrypting content.

Constant Summary collapse

ATTRIBUTES =
%i[cipher digest id key purpose secret
secret_iterations secret_salt serializer].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Record

Returns a new instance of Record.



35
36
37
38
39
40
41
# File 'lib/active_encryption/encryption_setting/record.rb', line 35

def initialize(attributes)
  ATTRIBUTES.each do |name|
    instance_variable_set("@#{name}", attributes[name])
  end

  @serializer = Object.const_get(@serializer) if @serializer.is_a?(String)
end

Class Method Details

.merge(base_record, higher_priority_record) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/active_encryption/encryption_setting/record.rb', line 25

def self.merge(base_record, higher_priority_record)
  new(
    to_h(base_record)
      .merge(key: nil) # reset the computed key
      .merge(
        to_h(higher_priority_record)
      )
  )
end

.to_h(record) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/active_encryption/encryption_setting/record.rb', line 15

def self.to_h(record)
  if record.is_a?(Hash)
    record.slice(*ATTRIBUTES)
  else
    ATTRIBUTES.each_with_object({}) do |name, attributes|
      attributes[name] = record.public_send(name)
    end
  end
end

Instance Method Details

#keyObject



43
44
45
46
47
48
49
50
# File 'lib/active_encryption/encryption_setting/record.rb', line 43

def key
  @key ||= Key.new(
    secret,
    cipher: cipher,
    salt: secret_salt,
    iterations: secret_iterations
  ).value
end

#to_hObject



52
53
54
# File 'lib/active_encryption/encryption_setting/record.rb', line 52

def to_h
  self.class.to_h(self)
end