module Mongoid::EncryptedField::ClassMethods

Constants

MARKER

Used to identify encrypted strings

Public Instance Methods

convert(object) click to toggle source
# File lib/mongoid-encrypted-fields/fields/encrypted_field.rb, line 57
def convert(object)
  raise NotImplementedError.new("convert must be implemented")
end
decrypt(encrypted) click to toggle source
# File lib/mongoid-encrypted-fields/fields/encrypted_field.rb, line 69
def decrypt(encrypted)
  unmarked = encrypted.slice(MARKER.size..-1)
  EncryptedFields.cipher.decrypt(unmarked)
end
demongoize(object) click to toggle source

Get the object as it was stored in the database, and instantiate this custom class from it.

# File lib/mongoid-encrypted-fields/fields/encrypted_field.rb, line 30
def demongoize(object)
  #EncryptedFields.logger.debug "#{name}##{__method__.to_s}: #{object.inspect}"
  case
    when object.is_a?(self.class) || object.blank?
      object
    else
      decrypted = is_encrypted?(object) ? decrypt(object) : object
      convert(decrypted)
  end
end
encrypt(plaintext) click to toggle source
# File lib/mongoid-encrypted-fields/fields/encrypted_field.rb, line 64
def encrypt(plaintext)
  encrypted = EncryptedFields.cipher.encrypt(plaintext).chomp
  MARKER + encrypted
end
evolve(object)

Converts the object that was supplied to a criteria and converts it into a database friendly form.

Alias for: mongoize
is_encrypted?(object) click to toggle source
# File lib/mongoid-encrypted-fields/fields/encrypted_field.rb, line 74
def is_encrypted?(object)
  object.is_a?(::String) && object.start_with?(MARKER)
end
mongoize(object) click to toggle source

Takes any possible object and converts it to how it would be stored in the database.

# File lib/mongoid-encrypted-fields/fields/encrypted_field.rb, line 42
def mongoize(object)
  #EncryptedFields.logger.debug "#{name}##{__method__.to_s}: #{object.inspect}"
  case
    when object.is_a?(self.class)
      object.mongoize
    when object.blank? || is_encrypted?(object)
      object
    else
      convert(object).mongoize
  end
end
Also aliased as: evolve