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
# 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.

Parameters:

  • hash (String)

    the content hash to expire



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

#openFile

Returns the data as an open file. Any temporary copy is unlinked first, leaving the kernel to reclaim it on close.

Returns:

  • (File)

    an open file, positioned at the start



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

#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, 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.

Returns:

  • (String)

    the SHA1 content hash

Raises:



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.

Yield Parameters:

  • path (Pathname)

    path to the data

Returns:

  • (Object)

    the return value of the block



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