Module: ActiveRecord::Sentry::ClassMethods

Defined in:
lib/active_record/sentry.rb

Instance Method Summary collapse

Instance Method Details

#asymmetrically_encrypts(attr_name, options = {}) ⇒ Object

def generates_crypted_hash_of(attribute)

before_validation ::Sentry::ShaSentry.new(attribute)
attr_accessor attribute

end



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/active_record/sentry.rb', line 25

def asymmetrically_encrypts(attr_name, options = {})
  #temp_sentry = ::Sentry::AsymmetricSentryCallback.new(attr_name)
  #before_validation temp_sentry
  #after_save temp_sentry
  unless instance_methods.include?("#{attr_name}_with_decryption")
    define_read_methods

    define_method("#{attr_name}_with_decryption") do |*optional|
      begin
        crypted_value = self.send("#{attr_name}_without_decryption")
        #puts "crypted value: #{crypted_value}"
        return nil if crypted_value.nil?
        key = optional.shift || (options[:key].is_a?(Proc) ? options[:key].call : options[:key]) || ::Sentry.default_key
        decrypted_value = ::Sentry::AsymmetricSentry.decrypt_large_from_base64(crypted_value, key)
        return decrypted_value
      rescue Exception => e
        nil
      end
    end

    alias_method_chain attr_name, :decryption
    alias_method "crypted_#{attr_name}", "#{attr_name}_without_decryption"
    alias_method "#{attr_name}_before_type_cast", "#{attr_name}_with_decryption"

    define_method("#{attr_name}_with_encryption=") do |value|
      encrypted_value = self.class.encrypt_for_sentry(value)
      self.send("#{attr_name}_without_encryption=", encrypted_value)
      nil
    end

    alias_method_chain "#{attr_name}=", :encryption
  end

end

#encrypt_for_sentry(string) ⇒ Object



60
61
62
63
# File 'lib/active_record/sentry.rb', line 60

def encrypt_for_sentry(string)
  return nil if string.nil?
  return ::Sentry::AsymmetricSentry.encrypt_large_to_base64(string)
end

#generates_crypted(attr_name, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/active_record/sentry.rb', line 8

def generates_crypted(attr_name, options = {})
  mode = options[:mode] || :asymmetric
  case mode
    #when :sha
    #  generates_crypted_hash_of(attr_name)
    when :asymmetric, :asymmetrical
      asymmetrically_encrypts(attr_name)
    #when :symmetric, :symmetrical
    #  symmetrically_encrypts(attr_name)
  end
end