Class: S3Direct::File

Inherits:
Object
  • Object
show all
Defined in:
lib/s3direct/file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, identifier, pattern, opts = {}) ⇒ File

Returns a new instance of File.



12
13
14
15
16
17
18
# File 'lib/s3direct/file.rb', line 12

def initialize(model, identifier, pattern, opts={})
  @model = model
  @identifier = identifier
  @pattern = pattern

  @options = default_options.merge(opts)
end

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



4
5
6
# File 'lib/s3direct/file.rb', line 4

def identifier
  @identifier
end

#modelObject (readonly)

Returns the value of attribute model.



4
5
6
# File 'lib/s3direct/file.rb', line 4

def model
  @model
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/s3direct/file.rb', line 4

def options
  @options
end

#patternObject (readonly)

Returns the value of attribute pattern.



4
5
6
# File 'lib/s3direct/file.rb', line 4

def pattern
  @pattern
end

Class Method Details

.sanitize_filename(name) ⇒ Object



6
7
8
9
10
# File 'lib/s3direct/file.rb', line 6

def self.sanitize_filename(name)
  unless name.nil?
    name.strip
  end
end

Instance Method Details

#exists?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/s3direct/file.rb', line 48

def exists?
  name.present?
end

#keyObject



44
45
46
# File 'lib/s3direct/file.rb', line 44

def key
  ::File.join(s3_path, name)
end

#max_upload_sizeObject



52
53
54
55
56
57
58
# File 'lib/s3direct/file.rb', line 52

def max_upload_size
  max_method = "#{identifier}_max_upload_size"

  if model.respond_to?(max_method)
    model.public_send(max_method)
  end
end

#nameObject



20
21
22
# File 'lib/s3direct/file.rb', line 20

def name
  @model.send "#{identifier}_file"
end

#s3_pathObject



24
25
26
# File 'lib/s3direct/file.rb', line 24

def s3_path
  StringInterpolator.new(model, pattern).to_s
end

#upload_request(filename = name, opts = {}) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/s3direct/file.rb', line 36

def upload_request(filename = name, opts = {})
  if filename.blank?
    raise "Can't create an upload request without a filename - " +
      "provide it as an argument or set #{identifier}_file on the model"
  end
  UploadRequest.new s3_path, self.class.sanitize_filename(filename), options.merge(opts)
end

#urlObject



28
29
30
31
32
33
34
# File 'lib/s3direct/file.rb', line 28

def url
  if exists?
    ::File.join(config.bucket_url, key)
  else
    default_url
  end
end