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.
-
#initialize(record, raw = nil) ⇒ Data
constructor
A new instance of Data.
-
#open ⇒ File
Returns the data as an open file.
-
#read ⇒ String?
Returns the data as a binary string.
-
#reset_read_cache! ⇒ void
Clears cached data, allowing it to be garbage collected.
-
#store! ⇒ String
Stores the data and returns the content hash.
-
#with_file {|path| ... } ⇒ Object
Yields a path to the data, removing any temporary copy afterwards.
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 |
# File 'lib/dis/model/data.rb', line 60 def content_length return raw.bytesize if raw? && raw.respond_to?(:bytesize) return raw.size if raw? && raw.respond_to?(:size) read.try(&:bytesize).to_i 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.
74 75 76 77 78 79 80 81 82 |
# File 'lib/dis/model/data.rb', line 74 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 |
#open ⇒ File
Returns the data as an open file. Any temporary copy is unlinked first, leaving the kernel to reclaim it on close.
124 125 126 127 128 129 130 131 |
# File 'lib/dis/model/data.rb', line 124 def open path = local_path return File.open(path, "rb") if path file = materialize File.unlink(file.path) file 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, allowing it to be garbage collected.
Subsequent calls to read will re-fetch from storage.
99 100 101 |
# File 'lib/dis/model/data.rb', line 99 def reset_read_cache! @read = nil end |
#store! ⇒ String
Stores the data and returns the content hash.
89 90 91 92 93 |
# File 'lib/dis/model/data.rb', line 89 def store! raise Dis::Errors::NoDataError unless raw? Dis::Storage.store(storage_type, raw) end |
#with_file {|path| ... } ⇒ Object
Yields a path to the data, removing any temporary copy afterwards.
108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/dis/model/data.rb', line 108 def with_file path = local_path return yield(Pathname.new(path)) if path file = materialize begin yield(Pathname.new(file.path)) ensure close_and_unlink(file) end end |