Class: Arkaan::Campaigns::File
- Includes:
- Arkaan::Concerns::MimeTypable
- Defined in:
- lib/arkaan/campaigns/file.rb
Overview
Representation of a file, allowing us to retrieve it on AWS by its filename and linked campaign ID.
Instance Attribute Summary collapse
-
#filename ⇒ String
The name of the file, corresponding to the AWS name.
-
#size ⇒ Integer
The size of the file in bytes.
Attributes inherited from Document
Instance Method Summary collapse
-
#exists_in_campaign(tmp_name) ⇒ Boolean
Checks if the given name already exists for a file in the campaign.
-
#extension ⇒ String
Returns the extension of the file, without the dot.
-
#merge_suffix(suffix) ⇒ String
Merges the given suffix with the name correctly.
-
#name_unicity ⇒ Object
Checks if the name is unique in the campaign, and if it is not then modifies it.
-
#raw_name ⇒ String
Returns the file name without the extension for the current file.
Methods inherited from Document
#creator, #creator=, #has_permission?, #is_allowed?
Instance Attribute Details
#filename ⇒ String
Returns the name of the file, corresponding to the AWS name.
10 |
# File 'lib/arkaan/campaigns/file.rb', line 10 field :name, type: String |
#size ⇒ Integer
Returns the size of the file in bytes.
13 |
# File 'lib/arkaan/campaigns/file.rb', line 13 field :size, type: Integer, default: 0 |
Instance Method Details
#exists_in_campaign(tmp_name) ⇒ Boolean
Checks if the given name already exists for a file in the campaign.
48 49 50 |
# File 'lib/arkaan/campaigns/file.rb', line 48 def exists_in_campaign(tmp_name) campaign.files.where(name: tmp_name, :id.ne => id).exists? end |
#extension ⇒ String
Returns the extension of the file, without the dot.
60 61 62 |
# File 'lib/arkaan/campaigns/file.rb', line 60 def extension return self[:name].split('.').last end |
#merge_suffix(suffix) ⇒ String
Merges the given suffix with the name correctly.
41 42 43 |
# File 'lib/arkaan/campaigns/file.rb', line 41 def merge_suffix(suffix) return "#{raw_name} (#{suffix}).#{extension}" end |
#name_unicity ⇒ Object
Checks if the name is unique in the campaign, and if it is not then modifies it. The modifications are the same than in a windows or UNIX system :
-
if the name exists as it is given, the system adds the suffix “(1)” to it.
-
if the name with the suffix still exists, the system tries to increment it until an available name is found.
-
the found name is then afected to the name of the current file.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/arkaan/campaigns/file.rb', line 26 def name_unicity if !campaign.nil? && !self[:name].nil? if exists_in_campaign(self[:name]) suffix = 1 while exists_in_campaign(merge_suffix(suffix)) suffix = suffix + 1 end self[:name] = merge_suffix(suffix) end end end |
#raw_name ⇒ String
Returns the file name without the extension for the current file.
54 55 56 |
# File 'lib/arkaan/campaigns/file.rb', line 54 def raw_name return self[:name].gsub(".#{extension}", '') end |