Module: ActiveRecord::Sentry::ClassMethods

Defined in:
lib/active_record/sentry.rb

Instance Method Summary collapse

Instance Method Details

#asymmetrically_encrypts(attr_name) ⇒ Object



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
# File 'lib/active_record/sentry.rb', line 25

def asymmetrically_encrypts(attr_name)
  temp_sentry = ::Sentry::AsymmetricSentryCallback.new(attr_name)
  before_validation temp_sentry
  after_save temp_sentry

  define_method(attr_name) do |*optional|
    send("#{attr_name}!", *optional) rescue nil
  end

  define_method("#{attr_name}!") do |*optional|
    return decrypted_values[attr_name] unless decrypted_values[attr_name].nil?
    return nil if send("crypted_#{attr_name}").nil?
    key = optional.shift
    ::Sentry::AsymmetricSentry.decrypt_from_base64(send("crypted_#{attr_name}"), key)
  end

  define_method("#{attr_name}=") do |value|
    decrypted_values[attr_name] = value
    nil
  end

  private
  define_method(:decrypted_values) do
    @decrypted_values ||= {}
  end
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] || :sha
  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

#generates_crypted_hash_of(attribute) ⇒ Object



20
21
22
23
# File 'lib/active_record/sentry.rb', line 20

def generates_crypted_hash_of(attribute)
  before_validation ::Sentry::ShaSentry.new(attribute)
  attr_accessor attribute
end

#symmetrically_encrypts(attr_name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/active_record/sentry.rb', line 52

def symmetrically_encrypts(attr_name)
  temp_sentry = ::Sentry::SymmetricSentryCallback.new(attr_name)
  before_validation temp_sentry
  after_save temp_sentry

  define_method(attr_name) do
    send("#{attr_name}!") rescue nil
  end

  define_method("#{attr_name}!") do
    return decrypted_values[attr_name] unless decrypted_values[attr_name].nil?
    return nil if send("crypted_#{attr_name}").nil?
    ::Sentry::SymmetricSentry.decrypt_from_base64(send("crypted_#{attr_name}"))
  end

  define_method("#{attr_name}=") do |value|
    decrypted_values[attr_name] = value
    nil
  end

  private
  define_method(:decrypted_values) do
    @decrypted_values ||= {}
  end
end