module SecureAttribute::ClassMethods

Public Instance Methods

attr_secure(name, options = {}) click to toggle source
# File lib/secure_attribute.rb, line 47
def attr_secure(name, options = {})
  ensure_attribute_accessors_are_defined(name)
  alias_method(attr_reader = "#{name}_without_secure_attribute", "#{name}")
  alias_method(attr_writer = "#{name}_without_secure_attribute=", "#{name}=")

  define_method("#{name}=") do |data|
    if data && !data.empty?
      send(attr_writer, SecureAttribute.encipher(options[:algorithm], data, options[:key]))
    else
      send(attr_writer, data)
    end
  end

  define_method(name) do
    if (data = send(attr_reader)) && !data.empty?
      SecureAttribute.decipher(data, options[:key])
    else
      data
    end
  end
end
ensure_attribute_accessors_are_defined(name) click to toggle source
# File lib/secure_attribute.rb, line 69
def ensure_attribute_accessors_are_defined(name)
  if defined?(ActiveRecord::Base) && self < ActiveRecord::Base
    define_attribute_method(name)
  else
    attr_writer(name) unless respond_to?("#{name}=")
    attr_reader(name) unless respond_to?(name)
  end
end