Module: Mongoid::Encryptor::ClassMethods

Defined in:
lib/mongoid/encryptor.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#encrypts(*attrs) ⇒ Object

Parameters:

  • attrs (Hash)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mongoid/encryptor.rb', line 13

def encrypts(*attrs)
  base_options = attrs.last.is_a?(Hash) ? attrs.pop : {}

  attrs.each do |attr_name|
    options = base_options.dup
    attr_name = attr_name.to_s

    mode = options.delete(:mode) || :sha
    cipher_class = EncryptedStrings.const_get("#{mode.to_s.classify}Cipher")

    send(:after_validation) do |doc|
      doc.send(:write_encrypted_attribute, attr_name, cipher_class, options)
      true
    end

    define_method(attr_name) do
      read_encrypted_attribute(attr_name, cipher_class, options)
    end
  end
end