module SimpleEncryptable

Constants

VERSION

Public Instance Methods

attr_encryptable(*attributes) click to toggle source
# File lib/simple_encryptable.rb, line 12
def attr_encryptable(*attributes)
  options = attributes.last.is_a?(Hash) ? attributes.pop : {}

  attributes.each do |attribute|
    define_method("#{attribute}=".to_sym) do |value|
      return if value.nil?

      public_send(
        "encrypted_#{attribute}=".to_sym,
        Encryptor.encrypt(value, options)
      )
    end

    define_method(attribute) do
      value = public_send("encrypted_#{attribute}".to_sym)
      Encryptor.decrypt(value, options) if value.present?
    end

    define_method("#{attribute}?".to_sym) do
      value = send(attribute)
      value.respond_to?(:empty?) ? !value.empty? : !!value
    end
  end
end