class ElFinder::Pathname
Attributes
path[R]
root[R]
Public Class Methods
new(root, path = '.')
click to toggle source
# File lib/el_finder/pathname.rb, line 10 def initialize(root, path = '.') @root = root.is_a?(ElFinder::Pathname) ? root.root : ::Pathname.new(root) @path = ::Pathname.new(path) @path = path.is_a?(ElFinder::Pathname) ? path.path : ::Pathname.new(path) if absolute? if @path.cleanpath.to_s.start_with?(@root.to_s) @path = ::Pathname.new @path.to_s.slice((@root.to_s.length + 1)..-1) elsif @path.cleanpath.to_s.start_with?(@root.realpath.to_s) @path = ::Pathname.new @path.to_s.slice((@root.realpath.to_s.length + 1)..-1) else raise SecurityError, "Absolute paths are not allowed" end end raise SecurityError, "Paths outside the root are not allowed" if outside_of_root? end
Public Instance Methods
+(other)
click to toggle source
# File lib/el_finder/pathname.rb, line 29 def +(other) if other.is_a? ::ElFinder::Pathname other = other.path end self.class.new(@root, (@path + other).to_s) end
absolute?()
click to toggle source
# File lib/el_finder/pathname.rb, line 42 def absolute? @path.absolute? end
basename(*args)
click to toggle source
# File lib/el_finder/pathname.rb, line 72 def basename(*args) @path.basename(*args) end
basename_sans_extension()
click to toggle source
# File lib/el_finder/pathname.rb, line 77 def basename_sans_extension @path.basename(@path.extname) end
child_directories(with_directory=true)
click to toggle source
# File lib/el_finder/pathname.rb, line 103 def child_directories(with_directory=true) realpath.children(with_directory).select{ |child| child.directory? }.map{|e| self.class.new(@root, e)} end
children(with_directory=true)
click to toggle source
# File lib/el_finder/pathname.rb, line 108 def children(with_directory=true) realpath.children(with_directory).map{|e| self.class.new(@root, e)} end
cleanpath()
click to toggle source
# File lib/el_finder/pathname.rb, line 62 def cleanpath fullpath.cleanpath end
dirname()
click to toggle source
# File lib/el_finder/pathname.rb, line 87 def dirname self.class.new(@root, @path.dirname) end
duplicate()
click to toggle source
# File lib/el_finder/pathname.rb, line 134 def duplicate _basename = basename_sans_extension copy = 1 if _basename.to_s =~ /^(.*) copy (\d+)$/ _basename = $1 copy = $2.to_i end begin new_file = self.class.new(@root, dirname + "#{_basename} copy #{copy}#{extname}") copy += 1 end while new_file.exist? new_file end
extname()
click to toggle source
# File lib/el_finder/pathname.rb, line 92 def extname @path.nil? ? '' : @path.extname end
fullpath()
click to toggle source
# File lib/el_finder/pathname.rb, line 57 def fullpath @path.nil? ? @root : @root + @path end
is_root?()
click to toggle source
# File lib/el_finder/pathname.rb, line 37 def is_root? @path.to_s == '.' end
outside_of_root?()
click to toggle source
# File lib/el_finder/pathname.rb, line 52 def outside_of_root? !cleanpath.to_s.start_with?(@root.to_s) end
realpath()
click to toggle source
# File lib/el_finder/pathname.rb, line 67 def realpath fullpath.realpath end
relative?()
click to toggle source
# File lib/el_finder/pathname.rb, line 47 def relative? @path.relative? end
relative_to(other)
click to toggle source
# File lib/el_finder/pathname.rb, line 118 def relative_to(other) @path.relative_path_from(other) end
rename(to)
click to toggle source
# File lib/el_finder/pathname.rb, line 149 def rename(to) to = self.class.new(@root, to.to_s) realpath.rename(to.fullpath.to_s) rescue Errno::EXDEV FileUtils.move(realpath.to_s, to.fullpath.to_s) ensure @path = to.path end
to_s()
click to toggle source
# File lib/el_finder/pathname.rb, line 97 def to_s cleanpath.to_s end
Also aliased as: to_str
touch(options = {})
click to toggle source
# File lib/el_finder/pathname.rb, line 113 def touch(options = {}) FileUtils.touch(cleanpath, options) end
unique()
click to toggle source
# File lib/el_finder/pathname.rb, line 123 def unique return self.dup unless self.file? copy = 1 begin new_file = self.class.new(@root, dirname + "#{basename_sans_extension} #{copy}#{extname}") copy += 1 end while new_file.exist? new_file end