Class: CouchPotato::Extensions::EncryptedProperty
- Inherits:
-
Persistence::SimpleProperty
- Object
- Persistence::SimpleProperty
- CouchPotato::Extensions::EncryptedProperty
- Defined in:
- lib/couch_potato/extensions/encrypted_property.rb
Instance Method Summary collapse
- #build(object, json) ⇒ Object
- #decrypt(value) ⇒ Object
- #dirty?(object) ⇒ Boolean
- #encrypt(value) ⇒ Object
- #encrypted_value(object) ⇒ Object
-
#initialize(owner_clazz, name, options = {}) ⇒ EncryptedProperty
constructor
A new instance of EncryptedProperty.
- #serialize(json, object) ⇒ Object
- #value(result, object) ⇒ Object
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, = {}) super @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
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 |