class Dis::Model::Data

Dis Model Data

Facilitates communication between the model and the storage, and holds any newly assigned data before the record is saved.

Attributes

raw[R]

Public Class Methods

new(record, raw = nil) click to toggle source
# File lib/dis/model/data.rb, line 10
def initialize(record, raw = nil)
  @record = record
  @raw = raw
end

Public Instance Methods

==(other) click to toggle source

Returns true if two Data objects represent the same data.

# File lib/dis/model/data.rb, line 16
def ==(other)
  # TODO: This can be made faster by
  # comparing hashes for stored objects.
  other.read == read
end
any?() click to toggle source

Returns true if data exists either in memory or in storage.

# File lib/dis/model/data.rb, line 23
def any?
  raw? || stored?
end
changed?() click to toggle source

Will be true if data has been explicitely set.

Dis::Model::Data.new(record).changed? # => false
Dis::Model::Data.new(record, new_file).changed? # => true
# File lib/dis/model/data.rb, line 36
def changed?
  raw?
end
content_length() click to toggle source

Returns the length of the data.

# File lib/dis/model/data.rb, line 41
def content_length
  if raw? && raw.respond_to?(:length)
    raw.length
  else
    read.try(&:length).to_i
  end
end
expire(hash) click to toggle source

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.

# File lib/dis/model/data.rb, line 52
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
read() click to toggle source

Returns the data as a binary string.

# File lib/dis/model/data.rb, line 28
def read
  @read ||= read_from(closest)
end
store!() click to toggle source

Stores the data. Returns a hash of the content for reference.

# File lib/dis/model/data.rb, line 63
def store!
  raise Dis::Errors::NoDataError unless raw?

  Dis::Storage.store(storage_type, raw)
end
tempfile() click to toggle source

Writes the data to a temporary file.

# File lib/dis/model/data.rb, line 70
def tempfile
  unless @tempfile
    @tempfile = Tempfile.new(binmode: true)
    @tempfile.write(@read || read_from(closest))
    @tempfile.open
  end
  @tempfile
end

Private Instance Methods

closest() click to toggle source
# File lib/dis/model/data.rb, line 81
def closest
  if raw?
    raw
  elsif stored?
    stored
  end
end
content_hash() click to toggle source
# File lib/dis/model/data.rb, line 89
def content_hash
  @record[@record.class.dis_attributes[:content_hash]]
end
raw?() click to toggle source
# File lib/dis/model/data.rb, line 93
def raw?
  raw ? true : false
end
read_from(object) click to toggle source
# File lib/dis/model/data.rb, line 97
def read_from(object)
  return nil unless object

  if object.respond_to?(:body)
    object.body
  elsif object.respond_to?(:read)
    rewind_and_read(object)
  else
    object
  end
end
rewind_and_read(object) click to toggle source
# File lib/dis/model/data.rb, line 109
def rewind_and_read(object)
  object.rewind
  response = object.read
  object.rewind
  response
end
storage_type() click to toggle source
# File lib/dis/model/data.rb, line 116
def storage_type
  @record.class.dis_type
end
stored() click to toggle source
# File lib/dis/model/data.rb, line 126
def stored
  Dis::Storage.get(storage_type, content_hash)
end
stored?() click to toggle source
# File lib/dis/model/data.rb, line 120
def stored?
  content_hash.present? &&
    (@record.dis_stored? ||
     Dis::Storage.exists?(storage_type, content_hash))
end