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



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

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
            raise "Can not serialize value #{value} of type '#{value.class}' for encrypted attribute" unless value.is_a?(String)
        end
        [key, value]
    end.to_h.with_indifferent_access
end

#decode_encrypted_attributes(attributes) ⇒ Object



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

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



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

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



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

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