Class: S3Direct::File
- Inherits:
-
Object
- Object
- S3Direct::File
- Defined in:
- lib/s3direct/file.rb
Instance Attribute Summary collapse
-
#identifier ⇒ Object
readonly
Returns the value of attribute identifier.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
Class Method Summary collapse
Instance Method Summary collapse
- #exists? ⇒ Boolean
-
#initialize(model, identifier, pattern, opts = {}) ⇒ File
constructor
A new instance of File.
- #key ⇒ Object
- #max_upload_size ⇒ Object
- #name ⇒ Object
- #s3_path ⇒ Object
- #upload_request(filename = name, opts = {}) ⇒ Object
- #url ⇒ Object
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 = .merge(opts) end |
Instance Attribute Details
#identifier ⇒ Object (readonly)
Returns the value of attribute identifier.
4 5 6 |
# File 'lib/s3direct/file.rb', line 4 def identifier @identifier end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
4 5 6 |
# File 'lib/s3direct/file.rb', line 4 def model @model end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/s3direct/file.rb', line 4 def end |
#pattern ⇒ Object (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
48 49 50 |
# File 'lib/s3direct/file.rb', line 48 def exists? name.present? end |
#key ⇒ Object
44 45 46 |
# File 'lib/s3direct/file.rb', line 44 def key ::File.join(s3_path, name) end |
#max_upload_size ⇒ Object
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 |
#name ⇒ Object
20 21 22 |
# File 'lib/s3direct/file.rb', line 20 def name @model.send "#{identifier}_file" end |
#s3_path ⇒ Object
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), .merge(opts) end |
#url ⇒ Object
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 |