lib/huberry/attr_encrypted/object.rb in shuber-attr_encrypted-1.0.3 vs lib/huberry/attr_encrypted/object.rb in shuber-attr_encrypted-1.0.4
- old
+ new
@@ -1,47 +1,51 @@
module Huberry
module AttrEncrypted
module Object
- def self.included(base)
- base.class_eval do
- extend ClassMethods
- eattr_accessor :attr_encrypted_options, :encrypted_attributes
- attr_encrypted_options = {}
- encrypted_attributes = {}
- end
+ def self.extended(base)
+ base.attr_encrypted_options = {}
+ base.instance_variable_set('@encrypted_attributes', {})
end
-
- # Wraps instance_variable_get
- def read_attribute(attribute)
- instance_variable_get("@#{attribute}")
+
+ # Default options to use with calls to <tt>attr_encrypted</tt>.
+ #
+ # It will inherit existing options from its parent class
+ def attr_encrypted_options
+ @attr_encrypted_options ||= superclass.attr_encrypted_options.nil? ? {} : superclass.attr_encrypted_options.dup
end
-
- # Wraps instance_variable_set
- def write_attribute(attribute, value)
- instance_variable_set("@#{attribute}", value)
+
+ # Sets default options to use with calls to <tt>attr_encrypted</tt>.
+ def attr_encrypted_options=(options)
+ @attr_encrypted_options = options
end
-
- module ClassMethods
- # Checks if an attribute has been configured to be encrypted
- #
- # Example
- #
- # class User
- # attr_accessor :name
- # attr_encrypted :email
- # end
- #
- # User.attr_encrypted?(:name) # false
- # User.attr_encrypted?(:email) # true
- def attr_encrypted?(attribute)
- encrypted_attributes.keys.include?(attribute.to_s)
- end
- # Copies existing encrypted attributes and options to the derived class
- def inherited(base)
- base.attr_encrypted_options = self.attr_encrypted_options.nil? ? {} : self.attr_encrypted_options.dup
- base.encrypted_attributes = self.encrypted_attributes.nil? ? {} : self.encrypted_attributes.dup
- end
+ # Contains a hash of encrypted attributes with virtual attribute names as keys and real attribute
+ # names as values
+ #
+ # Example
+ #
+ # class User
+ # attr_encrypted :email
+ # end
+ #
+ # User.encrypted_attributes # { 'email' => 'encrypted_email' }
+ def encrypted_attributes
+ @encrypted_attributes ||= superclass.encrypted_attributes.nil? ? {} : superclass.encrypted_attributes.dup
+ end
+
+ # Checks if an attribute has been configured to be encrypted
+ #
+ # Example
+ #
+ # class User
+ # attr_accessor :name
+ # attr_encrypted :email
+ # end
+ #
+ # User.attr_encrypted?(:name) # false
+ # User.attr_encrypted?(:email) # true
+ def attr_encrypted?(attribute)
+ encrypted_attributes.keys.include?(attribute.to_s)
end
end
end
end
\ No newline at end of file