Class: Dis::Model::Data

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(record, raw = nil) ⇒ Data

Returns a new instance of Data.

Parameters:

  • record (ActiveRecord::Base)

    the model instance

  • raw (File, IO, String, nil) (defaults to: nil)

    newly assigned 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.

Parameters:

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Examples:

Dis::Model::Data.new(record).changed? # => false
Dis::Model::Data.new(record, file).changed? # => true

Returns:

  • (Boolean)


53
54
55
# File 'lib/dis/model/data.rb', line 53

def changed?
  raw?
end

#content_lengthInteger

Returns the length of the data in bytes.

Returns:

  • (Integer)


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.

Parameters:

  • hash (String)

    the content hash to expire



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_pathString

Returns the file path to the data. Prefers a local storage path to avoid unnecessary copies, falls back to a tempfile.

Returns:

  • (String)


113
114
115
# File 'lib/dis/model/data.rb', line 113

def file_path
  local_path || tempfile.path
end

#readString?

Returns the data as a binary string.

Returns:

  • (String, nil)


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.

Returns:

  • (String)

    the SHA1 content hash

Raises:



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

#tempfileTempfile

Writes the data to a temporary file.

Returns:

  • (Tempfile)


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