Class: Subfolder
- Inherits:
-
Object
- Object
- Subfolder
- Defined in:
- lib/subfolder.rb
Instance Attribute Summary collapse
-
#directory ⇒ Object
readonly
Returns the value of attribute directory.
Class Method Summary collapse
- .remove_size_from_string(name) ⇒ Object
- .renamed_eof_regex ⇒ Object
- .size_human_readable(number_of_bytes) ⇒ Object
Instance Method Summary collapse
- #add_path_to_name! ⇒ Object
- #calc_size! ⇒ Object
-
#initialize(directory) ⇒ Subfolder
constructor
A new instance of Subfolder.
- #name_contains_size? ⇒ Boolean
- #name_matches_size? ⇒ Boolean
- #remove_size_from_name! ⇒ Object
- #size ⇒ Object
- #size_human_readable ⇒ Object
Constructor Details
#initialize(directory) ⇒ Subfolder
Returns a new instance of Subfolder.
11 12 13 14 15 |
# File 'lib/subfolder.rb', line 11 def initialize(directory) # An existing directory should be provided raise "Invalid path given: #{directory}" if not Dir.exists?(directory) @directory = directory end |
Instance Attribute Details
#directory ⇒ Object (readonly)
Returns the value of attribute directory.
5 6 7 |
# File 'lib/subfolder.rb', line 5 def directory @directory end |
Class Method Details
.remove_size_from_string(name) ⇒ Object
67 68 69 |
# File 'lib/subfolder.rb', line 67 def self.remove_size_from_string(name) name.gsub(Subfolder.renamed_eof_regex,"") end |
.renamed_eof_regex ⇒ Object
7 8 9 |
# File 'lib/subfolder.rb', line 7 def self.renamed_eof_regex / \[\d+(\.\d{1,3})?[KMGT]?B\]$/ end |
.size_human_readable(number_of_bytes) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/subfolder.rb', line 56 def self.size_human_readable(number_of_bytes) raise "Tried to convert invalid value to human-readable file size: #{number_of_bytes}, #{number_of_bytes.class}" if (number_of_bytes.class != Fixnum \ and number_of_bytes.class != Bignum) \ or number_of_bytes < 0 return "0B" if number_of_bytes == 0 units = %w{B KB MB GB TB} e = (Math.log(number_of_bytes)/Math.log(1024)).floor s = "%.3f" % (number_of_bytes.to_f / 1024**e) s.sub(/\.?0*$/, units[e]) end |
Instance Method Details
#add_path_to_name! ⇒ Object
39 40 41 42 43 |
# File 'lib/subfolder.rb', line 39 def add_path_to_name! new_name = name_with_size File.rename(@directory, new_name) @directory = new_name end |
#calc_size! ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/subfolder.rb', line 17 def calc_size! dirsize = 0 Find.find(@directory) do |f| dirsize += File.stat(f).size if File.exists?(f) and not File.symlink?(f) end @size = dirsize end |
#name_contains_size? ⇒ Boolean
31 32 33 |
# File 'lib/subfolder.rb', line 31 def name_contains_size? @directory =~ Subfolder.renamed_eof_regex end |
#name_matches_size? ⇒ Boolean
35 36 37 |
# File 'lib/subfolder.rb', line 35 def name_matches_size? name_contains_size? and (@directory =~ / \[#{size_human_readable}\]/) end |
#remove_size_from_name! ⇒ Object
45 46 47 48 49 50 |
# File 'lib/subfolder.rb', line 45 def remove_size_from_name! return if not name_contains_size? new_name = name_without_size File.rename(@directory, new_name) @directory = new_name end |
#size ⇒ Object
27 28 29 |
# File 'lib/subfolder.rb', line 27 def size @size or calc_size! end |
#size_human_readable ⇒ Object
52 53 54 |
# File 'lib/subfolder.rb', line 52 def size_human_readable Subfolder.size_human_readable(size) end |