Class: Treeview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Treeview

Returns a new instance of Treeview.



8
9
10
# File 'lib/treeview.rb', line 8

def initialize(path)
  @path = path == "" ? "/" : path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/treeview.rb', line 6

def path
  @path
end

#root_dirObject (readonly)

Returns the value of attribute root_dir.



6
7
8
# File 'lib/treeview.rb', line 6

def root_dir
  @root_dir
end

Class Method Details

.tree(dir) ⇒ Object



2
3
4
# File 'lib/treeview.rb', line 2

def self.tree(dir)
  new(dir).tree
end

Instance Method Details

#parentsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/treeview.rb', line 29

def parents
  paths = []
  current = root_dir
  until current == "/"
    paths << {
      :path => current,
      :is_dir => true,
    }
    current = File.dirname(current)
  end
  paths << {
    :path => "/",
    :is_dir => true,
  }
  paths.reverse
end

#treeObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/treeview.rb', line 17

def tree
  Dir.glob("#{root_dir == "/" ? "/" : "#{root_dir}/"}*").map do |file|
    {
      :path => file,
      :is_dir => File.directory?(file),
    }
  end.sort_by do |ent|
    # first order is directory or not, second order is alphabetical
    [ent[:is_dir] ? 0 : 1, ent[:path]]
  end
end