module Enkrip::Model

Public Instance Methods

decrypt() click to toggle source
# File lib/enkrip/model.rb, line 18
def decrypt
  self.class::ENCRYPTED_CONFIG.all_attributes.each do |attr|
    self[attr] = Enkrip::Engine.decrypt(self[attr], purpose: self.class::ENCRYPTED_CONFIG.purpose) if respond_to?(attr) && self[attr].present?
  end
end
encrypt() click to toggle source
# File lib/enkrip/model.rb, line 12
def encrypt
  self.class::ENCRYPTED_CONFIG.all_attributes.each do |attr|
    self[attr] = Enkrip::Engine.encrypt(self[attr], purpose: self.class::ENCRYPTED_CONFIG.purpose) if respond_to?(attr) && self[attr].present?
  end
end
enkrip_configure() { |config| ... } click to toggle source
Calls superclass method
# File lib/enkrip/model.rb, line 26
def enkrip_configure
  config = OpenStruct.new(
    string_attributes: [],
    numeric_attributes: [],
    purpose: nil,
    convert_method_for_numeric_attribute: :to_i,
    default_value_if_numeric_attribute_blank: 0
  )

  yield config

  config.all_attributes = config.string_attributes + config.numeric_attributes
  const_set :ENCRYPTED_CONFIG, config

  config.numeric_attributes.each do |attr|
    define_method(attr) do
      value = super()
      value.present? ? value.force_encoding('UTF-8').send(config.convert_method_for_numeric_attribute) : config.default_value_if_numeric_attribute_blank
    end
  end
end