Class: FilesystemExplorer::FilesystemItem
- Inherits:
-
Object
- Object
- FilesystemExplorer::FilesystemItem
- Defined in:
- lib/filesystem_explorer/filesystem_item.rb
Instance Attribute Summary collapse
-
#full_path ⇒ Object
readonly
Returns the value of attribute full_path.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #children ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(root, relative_path, options = {}) ⇒ FilesystemItem
constructor
A new instance of FilesystemItem.
- #initialize_for_multi_root(options) ⇒ Object
- #initialize_for_multi_root_child(options) ⇒ Object
- #initialize_for_single_root(options) ⇒ Object
- #initialize_for_single_root_child(options) ⇒ Object
- #inspect ⇒ Object
- #is_directory? ⇒ Boolean
- #is_parent? ⇒ Boolean
- #is_root? ⇒ Boolean
- #modified_at(format = nil) ⇒ Object
- #name ⇒ Object
- #parent ⇒ Object
- #path ⇒ Object
- #size ⇒ Object
- #to_partial_path ⇒ Object
- #url ⇒ Object
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, = {}) ||= {} root = [root].flatten if root.count > 1 relative_path = nil if File.(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 => [: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_path ⇒ Object (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 |
#root ⇒ Object (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
#children ⇒ Object
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
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() @root = [:roots] @full_paths = @root.map { |r| File.(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() @root = [:roots] @index = [:index] @path = [:relative_path] @full_path = File.(File.join(@root[@index], [:relative_path].gsub(/^#{[: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 => [: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() @root = [:root] @full_path = File.(@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() @root = [:root] @path = [:relative_path] @full_path = File.(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 |
#inspect ⇒ Object
126 |
# File 'lib/filesystem_explorer/filesystem_item.rb', line 126 def inspect ; return "[#{is_directory? ? 'D' : 'F'}] #{@full_path}" ; end |
#is_directory? ⇒ Boolean
87 |
# File 'lib/filesystem_explorer/filesystem_item.rb', line 87 def is_directory? ; return @is_directory ; end |
#is_parent? ⇒ Boolean
93 |
# File 'lib/filesystem_explorer/filesystem_item.rb', line 93 def is_parent? ; return @is_parent || false ; end |
#is_root? ⇒ 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 |
#name ⇒ Object
95 |
# File 'lib/filesystem_explorer/filesystem_item.rb', line 95 def name ; return @name ; end |
#parent ⇒ Object
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 |
#path ⇒ Object
89 |
# File 'lib/filesystem_explorer/filesystem_item.rb', line 89 def path ; return @path ||= @full_path || (@full_paths && "") ; end |
#size ⇒ Object
100 |
# File 'lib/filesystem_explorer/filesystem_item.rb', line 100 def size ; return @size ; end |
#to_partial_path ⇒ Object
128 |
# File 'lib/filesystem_explorer/filesystem_item.rb', line 128 def to_partial_path ; is_directory? ? 'directory' : 'file' ; end |
#url ⇒ Object
90 |
# File 'lib/filesystem_explorer/filesystem_item.rb', line 90 def url ; return @path.split('/').map { |p| URI::escape(p) }.join('/') ; end |