Module: CouchbaseOrm::Encrypt

Included in:
Document, Persistence
Defined in:
lib/couchbase-orm/encrypt.rb

Instance Method Summary collapse

Instance Method Details

#as_json(*args, **kwargs) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/couchbase-orm/encrypt.rb', line 38

def as_json(*args, **kwargs)
  super(*args, **kwargs).map do |key, value|
    type = self.class.attribute_types[key.to_s]
    if type.is_a?(CouchbaseOrm::Types::Encrypted) && value && !value.is_a?(String)
      raise "Can not serialize value #{value} of type '#{value.class}' for encrypted attribute"
    end

    [key, value]
  end.to_h.with_indifferent_access
end

#decode_encrypted_attributes(attributes) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/couchbase-orm/encrypt.rb', line 23

def decode_encrypted_attributes(attributes)
  attributes.map do |key, value|
    key = key.to_s
    if key.start_with?('encrypted$')
      key = key.gsub('encrypted$', '')
      value = value.with_indifferent_access[:ciphertext]
    end
    [key, value]
  end.to_h
end

#encode_encrypted_attributesObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/couchbase-orm/encrypt.rb', line 6

def encode_encrypted_attributes
  attributes.map do |key, value|
    type = self.class.attribute_types[key.to_s]
    if type.is_a?(CouchbaseOrm::Types::Encrypted)
      next unless value
      raise "Can not serialize value #{value} of type '#{value.class}' for Tanker encrypted attribute" unless value.is_a?(String)

      ["encrypted$#{key}", {
          alg: type.alg,
          ciphertext: value
      }]
    else
      [key, value]
    end
  end.compact.to_h
end

#to_json(*args, **kwargs) ⇒ Object



34
35
36
# File 'lib/couchbase-orm/encrypt.rb', line 34

def to_json(*args, **kwargs)
  as_json.to_json(*args, **kwargs)
end