Class: Milton::Storage::StoredFile

Inherits:
Object
  • Object
show all
Defined in:
lib/milton/storage/stored_file.rb

Direct Known Subclasses

DiskFile, S3File

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, id, options) ⇒ StoredFile

Returns a new instance of StoredFile.



32
33
34
35
36
# File 'lib/milton/storage/stored_file.rb', line 32

def initialize(filename, id, options)
  self.filename = filename
  self.id       = id
  self.options  = options
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



30
31
32
# File 'lib/milton/storage/stored_file.rb', line 30

def filename
  @filename
end

#idObject

Returns the value of attribute id.



30
31
32
# File 'lib/milton/storage/stored_file.rb', line 30

def id
  @id
end

#optionsObject

Returns the value of attribute options.



30
31
32
# File 'lib/milton/storage/stored_file.rb', line 30

def options
  @options
end

Class Method Details

.adapter(type) ⇒ Object

Returns the adapter class specified by the given type (by naming convention)

Storage::StoredFile.adapter(:s3) => Storage::S3File
Storage::StoredFile.adapter(:disk) => Storage::DiskFile


25
26
27
# File 'lib/milton/storage/stored_file.rb', line 25

def adapter(type)
  "Milton::Storage::#{type.to_s.classify}File".constantize
end

.create(filename, id, source, options) ⇒ Object



13
14
15
16
17
# File 'lib/milton/storage/stored_file.rb', line 13

def create(filename, id, source, options)
  returning new(filename, id, options) do |file|
    file.store(source)
  end
end

.sanitize_filename(filename, options) ⇒ Object

Sanitizes the given filename, removes pathnames and the special chars needed for options seperation for derivatives



7
8
9
10
11
# File 'lib/milton/storage/stored_file.rb', line 7

def sanitize_filename(filename, options)
  File.basename(filename, File.extname(filename)).gsub(/^.*(\\|\/)/, '').
    gsub(/[^\w]|#{Regexp.escape(options[:separator])}/, options[:replacement]).
    strip + File.extname(filename)
end

Instance Method Details

#clone(filename) ⇒ Object

Creates a clone of this StoredFile of the same type with the same id and options but using the given filename. Doesn’t actually do any copying of the underlying file data.



41
42
43
# File 'lib/milton/storage/stored_file.rb', line 41

def clone(filename)
  self.class.new(filename, self.id, self.options)
end