Class: Dis::Model::Data
- Inherits:
-
Object
- Object
- Dis::Model::Data
- Defined in:
- lib/dis/model/data.rb
Overview
Dis Model Data
Facilitates communication between the model and the storage, and holds any newly assigned data before the record is saved.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Returns true if two Data objects represent the same data.
-
#any? ⇒ Boolean
Returns true if data exists either in memory or in storage.
-
#changed? ⇒ Boolean
Will be true if data has been explicitly set.
-
#content_length ⇒ Integer
Returns the length of the data in bytes.
-
#expire(hash) ⇒ void
Expires a data object from the storage if it’s no longer being used by existing records.
-
#file_path ⇒ String
Returns the file path to the data.
-
#initialize(record, raw = nil) ⇒ Data
constructor
A new instance of Data.
-
#read ⇒ String?
Returns the data as a binary string.
-
#reset_read_cache! ⇒ void
Clears cached data and tempfiles, allowing them to be garbage collected.
-
#store! ⇒ String
Stores the data and returns the content hash.
-
#tempfile ⇒ Tempfile
Writes the data to a temporary file.
Constructor Details
#initialize(record, raw = nil) ⇒ Data
Returns a new instance of Data.
12 13 14 15 |
# File 'lib/dis/model/data.rb', line 12 def initialize(record, raw = nil) @record = record @raw = raw end |
Instance Method Details
#==(other) ⇒ Boolean
Returns true if two Data objects represent the same data.
22 23 24 25 26 27 28 29 30 |
# File 'lib/dis/model/data.rb', line 22 def ==(other) if !raw? && other.is_a?(self.class) && !other.changed? content_hash == other.content_hash elsif other.respond_to?(:read) other.read == read else false end end |
#any? ⇒ Boolean
Returns true if data exists either in memory or in storage.
35 36 37 |
# File 'lib/dis/model/data.rb', line 35 def any? raw? || stored? end |
#changed? ⇒ Boolean
Will be true if data has been explicitly set.
53 54 55 |
# File 'lib/dis/model/data.rb', line 53 def changed? raw? end |
#content_length ⇒ Integer
Returns the length of the data in bytes.
60 61 62 63 64 65 66 |
# File 'lib/dis/model/data.rb', line 60 def content_length if raw? && raw.respond_to?(:length) raw.length else read.try(&:length).to_i end end |
#expire(hash) ⇒ void
This method returns an undefined value.
Expires a data object from the storage if it’s no longer being used by existing records. This is triggered from callbacks on the record whenever they are changed or destroyed.
75 76 77 78 79 80 81 82 83 |
# File 'lib/dis/model/data.rb', line 75 def expire(hash) return if hash.blank? unless @record.class.where( @record.class.dis_attributes[:content_hash] => hash ).any? Dis::Storage.delete(storage_type, hash) end end |
#file_path ⇒ String
Returns the file path to the data. Prefers a local storage path to avoid unnecessary copies, falls back to a tempfile.
113 114 115 |
# File 'lib/dis/model/data.rb', line 113 def file_path local_path || tempfile.path end |
#read ⇒ String?
Returns the data as a binary string.
42 43 44 |
# File 'lib/dis/model/data.rb', line 42 def read @read ||= read_from(closest) end |
#reset_read_cache! ⇒ void
This method returns an undefined value.
Clears cached data and tempfiles, allowing them to be garbage collected. Subsequent calls to read or tempfile will re-fetch from storage.
101 102 103 104 105 106 107 |
# File 'lib/dis/model/data.rb', line 101 def reset_read_cache! @read = nil return unless @tempfile @tempfile.close! @tempfile = nil end |
#store! ⇒ String
Stores the data and returns the content hash.
90 91 92 93 94 |
# File 'lib/dis/model/data.rb', line 90 def store! raise Dis::Errors::NoDataError unless raw? Dis::Storage.store(storage_type, raw) end |
#tempfile ⇒ Tempfile
Writes the data to a temporary file.
120 121 122 123 124 125 126 127 |
# File 'lib/dis/model/data.rb', line 120 def tempfile unless @tempfile @tempfile = Tempfile.new(binmode: true) @tempfile.write(@read || read_from(closest)) @tempfile.open end @tempfile end |