Class: CouchPotato::Extensions::EncryptedProperty

Inherits:
Persistence::SimpleProperty
  • Object
show all
Defined in:
lib/couch_potato/extensions/encrypted_property.rb

Instance Method Summary collapse

Constructor Details

#initialize(owner_clazz, name, options = {}) ⇒ EncryptedProperty

Returns a new instance of EncryptedProperty.



6
7
8
9
# File 'lib/couch_potato/extensions/encrypted_property.rb', line 6

def initialize(owner_clazz, name, options = {})
  super
  @options = options
end

Instance Method Details

#build(object, json) ⇒ Object



11
12
13
14
15
# File 'lib/couch_potato/extensions/encrypted_property.rb', line 11

def build(object, json)
  value = json[name.to_s].nil? ? json[name.to_sym] : json[name.to_s]
  object.send "#{name}=", decrypt(value)
  object.instance_variable_set("@encrypted_#{name}", value)
end

#decrypt(value) ⇒ Object



39
40
41
42
43
# File 'lib/couch_potato/extensions/encrypted_property.rb', line 39

def decrypt(value)
  if not value.nil?
    EzCrypto::Key.decrypt_with_password(@options[:password], @options[:salt], Base64.decode64(value))
  end
end

#dirty?(object) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/couch_potato/extensions/encrypted_property.rb', line 17

def dirty?(object)
  object.send("#{name}_changed?")
end

#encrypt(value) ⇒ Object



33
34
35
36
37
# File 'lib/couch_potato/extensions/encrypted_property.rb', line 33

def encrypt(value)
  if not value.nil?
    value.empty? ? value : Base64.encode64(EzCrypto::Key.encrypt_with_password(@options[:password], @options[:salt], value))
  end
end

#encrypted_value(object) ⇒ Object



45
46
47
# File 'lib/couch_potato/extensions/encrypted_property.rb', line 45

def encrypted_value(object)
  object.instance_variable_get("@encrypted_#{name}")
end

#serialize(json, object) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/couch_potato/extensions/encrypted_property.rb', line 21

def serialize(json, object)
  if dirty?(object) or encrypted_value(object).nil?
    json[name] = encrypt(object.send(name))
  else
    json[name] = encrypted_value(object)
  end
end

#value(result, object) ⇒ Object



29
30
31
# File 'lib/couch_potato/extensions/encrypted_property.rb', line 29

def value(result, object)
  result[name] = object.send(name)
end