Class: Mack::Request::UploadedFile
- Defined in:
- lib/mack/controller/uploaded_file.rb
Overview
Wraps the Hash that Rack creates when you post a file.
Instance Attribute Summary collapse
-
#destination_path ⇒ Object
The destination path you want the file to be saved to.
-
#file_name ⇒ Object
readonly
The name of the file uploaded.
-
#file_type ⇒ Object
readonly
The type of the file uploaded.
-
#head ⇒ Object
readonly
Returns the value of attribute head.
-
#param_name ⇒ Object
readonly
The name of the parameter used to upload the file.
-
#temp_file ⇒ Object
readonly
A File object representing the uploaded file.
Instance Method Summary collapse
-
#initialize(param, destination_path = nil) ⇒ UploadedFile
constructor
A new instance of UploadedFile.
-
#save ⇒ Object
Saves the temp_file to the destination_path.
-
#save_to(*destination_path) ⇒ Object
Takes/sets the destination_path and then calls the save method.
- #valid? ⇒ Boolean
Constructor Details
#initialize(param, destination_path = nil) ⇒ UploadedFile
Returns a new instance of UploadedFile.
18 19 20 21 22 23 24 25 |
# File 'lib/mack/controller/uploaded_file.rb', line 18 def initialize(param, destination_path = nil) @file_name = param[:filename] @file_type = param[:type] @param_name = param[:name] @temp_file = param[:tempfile] @head = param[:head] @destination_path = nil end |
Instance Attribute Details
#destination_path ⇒ Object
The destination path you want the file to be saved to.
16 17 18 |
# File 'lib/mack/controller/uploaded_file.rb', line 16 def destination_path @destination_path end |
#file_name ⇒ Object (readonly)
The name of the file uploaded
7 8 9 |
# File 'lib/mack/controller/uploaded_file.rb', line 7 def file_name @file_name end |
#file_type ⇒ Object (readonly)
The type of the file uploaded
9 10 11 |
# File 'lib/mack/controller/uploaded_file.rb', line 9 def file_type @file_type end |
#head ⇒ Object (readonly)
Returns the value of attribute head.
14 15 16 |
# File 'lib/mack/controller/uploaded_file.rb', line 14 def head @head end |
#param_name ⇒ Object (readonly)
The name of the parameter used to upload the file
11 12 13 |
# File 'lib/mack/controller/uploaded_file.rb', line 11 def param_name @param_name end |
#temp_file ⇒ Object (readonly)
A File object representing the uploaded file.
13 14 15 |
# File 'lib/mack/controller/uploaded_file.rb', line 13 def temp_file @temp_file end |
Instance Method Details
#save ⇒ Object
Saves the temp_file to the destination_path. This method will create the directory tree of the destination_path if it does not exists. Raises ArgumentError if the destination_path has not been set.
50 51 52 53 54 |
# File 'lib/mack/controller/uploaded_file.rb', line 50 def save raise ArgumentError.new("Destination path must be set!") if self.destination_path.blank? FileUtils.mkdir_p(File.dirname(self.destination_path)) FileUtils.mv(self.temp_file.path, self.destination_path) end |
#save_to(*destination_path) ⇒ Object
Takes/sets the destination_path and then calls the save method.
42 43 44 45 |
# File 'lib/mack/controller/uploaded_file.rb', line 42 def save_to(*destination_path) self.destination_path = *destination_path save end |
#valid? ⇒ Boolean
56 57 58 |
# File 'lib/mack/controller/uploaded_file.rb', line 56 def valid? !self.file_name.blank? && !self.temp_file.nil? end |