class Alchemy::Custom::Model::ElFinder::PathName

Attributes

path[R]
root[R]

Public Class Methods

new(root, path = '.') click to toggle source
# File lib/alchemy/custom/model/el_finder/path_name.rb, line 10
def initialize(root, path = '.')
  @root = root.is_a?(PathName) ? root.root : ::Pathname.new(root)

  @path = ::Pathname.new(path)
  @path = path.is_a?(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/alchemy/custom/model/el_finder/path_name.rb, line 31
def +(other)
  if other.is_a? PathN  ame
    other = other.path
  end
  self.class.new(@root, (@path + other).to_s)
end
absolute?() click to toggle source
# File lib/alchemy/custom/model/el_finder/path_name.rb, line 46
def absolute?
  @path.absolute?
end
basename(*args) click to toggle source
# File lib/alchemy/custom/model/el_finder/path_name.rb, line 88
def basename(*args)
  @path.basename(*args)
end
basename_sans_extension() click to toggle source
# File lib/alchemy/custom/model/el_finder/path_name.rb, line 95
def basename_sans_extension
  @path.basename(@path.extname)
end
child_directories(with_directory = true) click to toggle source
# File lib/alchemy/custom/model/el_finder/path_name.rb, line 131
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/alchemy/custom/model/el_finder/path_name.rb, line 136
def children(with_directory = true)
  realpath.children(with_directory).map {|e| self.class.new(@root, e)}
end
cleanpath() click to toggle source
# File lib/alchemy/custom/model/el_finder/path_name.rb, line 74
def cleanpath
  fullpath.cleanpath
end
dirname() click to toggle source
# File lib/alchemy/custom/model/el_finder/path_name.rb, line 109
def dirname
  self.class.new(@root, @path.dirname)
end
duplicate() click to toggle source
# File lib/alchemy/custom/model/el_finder/path_name.rb, line 164
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/alchemy/custom/model/el_finder/path_name.rb, line 116
def extname
  @path.nil? ? '' : @path.extname
end
fullpath() click to toggle source
# File lib/alchemy/custom/model/el_finder/path_name.rb, line 67
def fullpath
  @path.nil? ? @root : @root + @path
end
is_root?() click to toggle source
# File lib/alchemy/custom/model/el_finder/path_name.rb, line 41
def is_root?
  @path.to_s == '.'
end
outside_of_root?() click to toggle source
# File lib/alchemy/custom/model/el_finder/path_name.rb, line 60
def outside_of_root?
  !cleanpath.to_s.start_with?(@root.to_s)
end
realpath() click to toggle source
# File lib/alchemy/custom/model/el_finder/path_name.rb, line 81
def realpath
  fullpath.realpath
end
relative?() click to toggle source
# File lib/alchemy/custom/model/el_finder/path_name.rb, line 53
def relative?
  @path.relative?
end
relative_to(other) click to toggle source
# File lib/alchemy/custom/model/el_finder/path_name.rb, line 146
def relative_to(other)
  @path.relative_path_from(other)
end
rename(to) click to toggle source
# File lib/alchemy/custom/model/el_finder/path_name.rb, line 181
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/alchemy/custom/model/el_finder/path_name.rb, line 123
def to_s
  cleanpath.to_s
end
Also aliased as: to_str
to_str()

of to_s

Alias for: to_s
touch(options = {}) click to toggle source
# File lib/alchemy/custom/model/el_finder/path_name.rb, line 141
def touch(options = {})
  FileUtils.touch(cleanpath, options)
end
unique() click to toggle source
# File lib/alchemy/custom/model/el_finder/path_name.rb, line 151
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