Class: Liberator::Directory

Inherits:
Object
  • Object
show all
Defined in:
lib/liberator/directory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.expand_path path
  cache_entries
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



3
4
5
# File 'lib/liberator/directory.rb', line 3

def entries
  @entries
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/liberator/directory.rb', line 3

def path
  @path
end

#selected_indexObject (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_entryObject



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

#parentObject



21
22
23
# File 'lib/liberator/directory.rb', line 21

def parent
  Directory.new File.expand_path(@path + '/..')
end

#refreshObject 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_entryObject



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_entryObject



13
14
15
# File 'lib/liberator/directory.rb', line 13

def select_previous_entry
  @selected_index -= 1 if @selected_index > 0
end

#selected_entryObject



9
10
11
# File 'lib/liberator/directory.rb', line 9

def selected_entry
  @entries[@selected_index]
end