Class: FilesystemExplorer::FilesystemItem

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, relative_path, options = {}) ⇒ FilesystemItem

Returns a new instance of FilesystemItem.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/filesystem_explorer/filesystem_item.rb', line 6

def initialize(root, relative_path, options = {})
  options ||= {}
  root = [root].flatten

  if root.count > 1
    relative_path = nil if File.expand_path(relative_path).gsub(/^#{Dir.pwd}\/?/, '').gsub(/^\d+\/?/, '').strip =~ /^\/?$/

    if relative_path.blank?
      initialize_for_multi_root({ :roots => root })
    else
      root.each { |r| relative_path = relative_path.gsub(/^#{r}\/?/, '') }
      initialize_for_multi_root_child({
        :roots => root,
        :index => options[:index] || (relative_path =~ /^(\d+)/ && $1.to_i),
        :relative_path => relative_path
      })
    end
  else
    if relative_path.blank?
      initialize_for_single_root({ :root => root.first })
    else
      initialize_for_single_root_child({
        :root => root.first,
        :relative_path => relative_path
      })
    end
  end
end

Instance Attribute Details

#full_pathObject (readonly)

Returns the value of attribute full_path.



4
5
6
# File 'lib/filesystem_explorer/filesystem_item.rb', line 4

def full_path
  @full_path
end

#rootObject (readonly)

Returns the value of attribute root.



3
4
5
# File 'lib/filesystem_explorer/filesystem_item.rb', line 3

def root
  @root
end

Instance Method Details

#childrenObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/filesystem_explorer/filesystem_item.rb', line 107

def children
  return nil unless is_directory?
  if @children.blank?
    if @is_multi_root
      if @is_root
        load_multi_root_children
      else
        load_multi_root_child_children
      end
    else
      load_single_root_children
    end

    sort_children
  end

  @children
end

#exists?Boolean

Returns:

  • (Boolean)


94
# File 'lib/filesystem_explorer/filesystem_item.rb', line 94

def exists? ; return @exists ; end

#initialize_for_multi_root(options) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/filesystem_explorer/filesystem_item.rb', line 35

def initialize_for_multi_root(options)
  @root = options[:roots]
  @full_paths = @root.map { |r| File.expand_path(r) }
  @is_directory = @full_paths.all? { |p| File.directory?(p) }
  @modified_at = nil
  @size = nil
  @is_root = true
  @exists = @full_paths.all? { |p| File.exists?(p) }
  @is_multi_root = true
end

#initialize_for_multi_root_child(options) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/filesystem_explorer/filesystem_item.rb', line 46

def initialize_for_multi_root_child(options)
  @root = options[:roots]
  @index = options[:index]
  @path = options[:relative_path]
  @full_path = File.expand_path(File.join(@root[@index], options[:relative_path].gsub(/^#{options[:index]}\/?/, '')))
  @exists = File.exists?(@full_path)
  @name = File.basename(@full_path)
  @is_directory = @exists && File.directory?(@full_path)
  @modified_at = @exists && File.mtime(@full_path)
  @size = @exists && File.size(@full_path)
  @is_multi_root = true
  @is_root = false
  @parent = FilesystemItem.new(@root, build_path(@path, '..'), { :index => options[:index] })
end

#initialize_for_single_root(options) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/filesystem_explorer/filesystem_item.rb', line 61

def initialize_for_single_root(options)
  @root = options[:root]
  @full_path = File.expand_path(@root)
  @path = '/'
  @is_directory = File.directory?(@full_path)
  @modified_at = File.mtime(@full_path)
  @size = File.size(@full_path)
  @is_root = true
  @exists = File.exists?(@full_path)
  @is_multi_root = false
end

#initialize_for_single_root_child(options) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/filesystem_explorer/filesystem_item.rb', line 73

def initialize_for_single_root_child(options)
  @root = options[:root]
  @path = options[:relative_path]
  @full_path = File.expand_path(File.join(@root, @path))
  @name = File.basename(@full_path)
  @is_directory = File.directory?(@full_path)
  @modified_at = File.mtime(@full_path)
  @size = File.size(@full_path)
  @is_root = false
  @exists = File.exists?(@full_path)
  @is_multi_root = false
  @parent = FilesystemItem.new(@root, build_path(@path, '..'))
end

#inspectObject



126
# File 'lib/filesystem_explorer/filesystem_item.rb', line 126

def inspect ; return "[#{is_directory? ? 'D' : 'F'}] #{@full_path}" ; end

#is_directory?Boolean

Returns:

  • (Boolean)


87
# File 'lib/filesystem_explorer/filesystem_item.rb', line 87

def is_directory? ; return @is_directory ; end

#is_parent?Boolean

Returns:

  • (Boolean)


93
# File 'lib/filesystem_explorer/filesystem_item.rb', line 93

def is_parent? ; return @is_parent || false ; end

#is_root?Boolean

Returns:

  • (Boolean)


92
# File 'lib/filesystem_explorer/filesystem_item.rb', line 92

def is_root? ; return @is_root ; end

#modified_at(format = nil) ⇒ Object



96
97
98
99
# File 'lib/filesystem_explorer/filesystem_item.rb', line 96

def modified_at(format = nil)
  return @modified_at if format.blank? || @modified_at.blank?
  return @modified_at.strftime(format)
end

#nameObject



95
# File 'lib/filesystem_explorer/filesystem_item.rb', line 95

def name ; return @name ; end

#parentObject



102
103
104
105
# File 'lib/filesystem_explorer/filesystem_item.rb', line 102

def parent
  return nil if is_root?
  @parent ||= FilesystemItem.new(@root, File.join(@full_path, '..'), { root: @root })
end

#pathObject



89
# File 'lib/filesystem_explorer/filesystem_item.rb', line 89

def path ; return @path ||= @full_path || (@full_paths && "") ; end

#sizeObject



100
# File 'lib/filesystem_explorer/filesystem_item.rb', line 100

def size ; return @size ; end

#to_partial_pathObject



128
# File 'lib/filesystem_explorer/filesystem_item.rb', line 128

def to_partial_path ; is_directory? ? 'directory' : 'file' ; end

#urlObject



90
# File 'lib/filesystem_explorer/filesystem_item.rb', line 90

def url ; return @path.split('/').map { |p| URI::escape(p) }.join('/') ; end