Class: Liberator::Directory
- Inherits:
-
Object
- Object
- Liberator::Directory
- Defined in:
- lib/liberator/directory.rb
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#selected_index ⇒ Object
readonly
Returns the value of attribute selected_index.
Instance Method Summary collapse
- #delete_selected_entry ⇒ Object
-
#initialize(path) ⇒ Directory
constructor
A new instance of Directory.
- #parent ⇒ Object
- #refresh ⇒ Object (also: #cache_entries)
- #select_next_entry ⇒ Object
- #select_previous_entry ⇒ Object
- #selected_entry ⇒ Object
Constructor Details
#initialize(path) ⇒ Directory
Returns a new instance of Directory.
4 5 6 7 |
# File 'lib/liberator/directory.rb', line 4 def initialize(path) @path = File. path cache_entries end |
Instance Attribute Details
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
3 4 5 |
# File 'lib/liberator/directory.rb', line 3 def entries @entries end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
3 4 5 |
# File 'lib/liberator/directory.rb', line 3 def path @path end |
#selected_index ⇒ Object (readonly)
Returns the value of attribute selected_index.
3 4 5 |
# File 'lib/liberator/directory.rb', line 3 def selected_index @selected_index end |
Instance Method Details
#delete_selected_entry ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/liberator/directory.rb', line 25 def delete_selected_entry begin FileUtils.rm_r selected_entry[:path] rescue Errno::EACCES return false end true end |
#parent ⇒ Object
21 22 23 |
# File 'lib/liberator/directory.rb', line 21 def parent Directory.new File.(@path + '/..') end |
#refresh ⇒ Object Also known as: cache_entries
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/liberator/directory.rb', line 35 def refresh begin @entries = Dir.real_entries(@path).collect do |entry| absolute_path = @path + '/' + entry if File.file? absolute_path size = File.size absolute_path elsif File.directory? absolute_path size = Dir.size absolute_path else next end { path: absolute_path, size: size } end @entries = @entries.compact.sort_by { |entry| 1.0/entry[:size].to_f } @selected_index = 0 rescue raise IOError.new end end |
#select_next_entry ⇒ Object
17 18 19 |
# File 'lib/liberator/directory.rb', line 17 def select_next_entry @selected_index += 1 unless @selected_index >= @entries.size-1 end |
#select_previous_entry ⇒ Object
13 14 15 |
# File 'lib/liberator/directory.rb', line 13 def select_previous_entry @selected_index -= 1 if @selected_index > 0 end |
#selected_entry ⇒ Object
9 10 11 |
# File 'lib/liberator/directory.rb', line 9 def selected_entry @entries[@selected_index] end |