Class: ArchivedAttributes::ArchivedAttribute
- Inherits:
-
Object
- Object
- ArchivedAttributes::ArchivedAttribute
- Defined in:
- lib/archived_attributes/archived_attribute.rb
Instance Attribute Summary collapse
-
#instance ⇒ Object
readonly
Returns the value of attribute instance.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #destroy ⇒ Object
- #dirty? ⇒ Boolean
-
#initialize(name, instance, opts = {}) ⇒ ArchivedAttribute
constructor
A new instance of ArchivedAttribute.
-
#instantiate_backend_from(options) ⇒ Object
Returns a backend object based on the options given (e.g., filesystem, s3).
- #options ⇒ Object
-
#save ⇒ Object
First saves this record to the back-end.
- #value ⇒ Object
- #value=(other) ⇒ Object
Constructor Details
#initialize(name, instance, opts = {}) ⇒ ArchivedAttribute
Returns a new instance of ArchivedAttribute.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/archived_attributes/archived_attribute.rb', line 13 def initialize(name, instance, opts = {}) @name = name @instance = instance @dirty = false @_options = ArchivedAttributes::GlobalConfiguration.instance.to_hash. merge(opts) @backend = instantiate_backend_from() end |
Instance Attribute Details
#instance ⇒ Object (readonly)
Returns the value of attribute instance.
11 12 13 |
# File 'lib/archived_attributes/archived_attribute.rb', line 11 def instance @instance end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/archived_attributes/archived_attribute.rb', line 11 def name @name end |
Instance Method Details
#destroy ⇒ Object
47 48 49 |
# File 'lib/archived_attributes/archived_attribute.rb', line 47 def destroy @backend.destroy end |
#dirty? ⇒ Boolean
37 38 39 |
# File 'lib/archived_attributes/archived_attribute.rb', line 37 def dirty? @dirty end |
#instantiate_backend_from(options) ⇒ Object
Returns a backend object based on the options given (e.g., filesystem, s3).
53 54 55 56 |
# File 'lib/archived_attributes/archived_attribute.rb', line 53 def instantiate_backend_from() "ArchivedAttributes::Backends::#{[:storage].to_s.camelize}". constantize.new(self) end |
#options ⇒ Object
58 59 60 |
# File 'lib/archived_attributes/archived_attribute.rb', line 58 def @_options end |
#save ⇒ Object
First saves this record to the back-end. If backend storage raises an error, we capture it and add it to the AR validation errors.
43 44 45 |
# File 'lib/archived_attributes/archived_attribute.rb', line 43 def save @backend.save(@stashed_value) if self.dirty? end |
#value ⇒ Object
24 25 26 27 28 |
# File 'lib/archived_attributes/archived_attribute.rb', line 24 def value val = defined?(@stashed_value) ? @stashed_value : @backend.load val = compress? ? Zlib::Inflate.inflate(val) : val val = marshal? ? Marshal.load(val) : val end |
#value=(other) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/archived_attributes/archived_attribute.rb', line 30 def value=(other) other = marshal? ? Marshal.dump(other) : other other = compress? && !other.nil? ? Zlib::Deflate.deflate(other) : other @stashed_value = other @dirty = true end |