Class: ElFinderFtp::Pathname
- Inherits:
-
Object
- Object
- ElFinderFtp::Pathname
- Defined in:
- lib/el_finder_ftp/pathname.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #absolute? ⇒ Boolean
- #basename(*args) ⇒ Object
- #basename_sans_extension ⇒ Object
- #child_directories(with_directory = true) ⇒ Object
- #children(with_directory = true) ⇒ Object
- #cleanpath ⇒ Object
- #dirname ⇒ Object
- #duplicate ⇒ Object
- #extname ⇒ Object
- #files(with_directory = true) ⇒ Object
- #fullpath ⇒ Object
-
#initialize(adapter_or_root, path = '.') ⇒ Pathname
constructor
A new instance of Pathname.
- #is_root? ⇒ Boolean
- #outside_of_root? ⇒ Boolean
- #realpath ⇒ Object
- #relative? ⇒ Boolean
- #relative_to(other) ⇒ Object
- #rename(to) ⇒ Object
- #to_s ⇒ Object (also: #to_str)
- #touch(options = {}) ⇒ Object
- #unique ⇒ Object
Constructor Details
#initialize(adapter_or_root, path = '.') ⇒ Pathname
Returns a new instance of Pathname.
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/el_finder_ftp/pathname.rb', line 10 def initialize(adapter_or_root, path = '.') @adapter = adapter_or_root.is_a?(ElFinderFtp::Pathname) ? adapter_or_root.adapter : adapter_or_root @root = path.is_a?(ElFinderFtp::Pathname) ? path.root : FtpPathname.new(@adapter, '/') if path.is_a?(ElFinderFtp::Pathname) @path = path.path elsif path.is_a?(ElFinderFtp::FtpPathname) @path = path else @path = FtpPathname.new(@adapter, path) end if absolute? if @path.cleanpath.to_s.start_with?(@root.to_s) @path = FtpPathname.new( @adapter, @path.to_s.slice((@root.to_s.length)..-1), @path.attrs) elsif @path.cleanpath.to_s.start_with?(@root.realpath.to_s) @path = FtpPathname.new( @adapter, @path.to_s.slice((@root.realpath.to_s.length)..-1), @path.attrs) else raise SecurityError, "Absolute paths are not allowed" end end raise SecurityError, "Paths outside the root are not allowed" if outside_of_root? end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
7 8 9 |
# File 'lib/el_finder_ftp/pathname.rb', line 7 def adapter @adapter end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/el_finder_ftp/pathname.rb', line 7 def path @path end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
7 8 9 |
# File 'lib/el_finder_ftp/pathname.rb', line 7 def root @root end |
Instance Method Details
#+(other) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/el_finder_ftp/pathname.rb', line 36 def +(other) if other.is_a? ::ElFinderFtp::Pathname other = other.path end self.class.new(@adapter, @path + other) end |
#absolute? ⇒ Boolean
49 50 51 |
# File 'lib/el_finder_ftp/pathname.rb', line 49 def absolute? @path.absolute? end |
#basename(*args) ⇒ Object
79 80 81 |
# File 'lib/el_finder_ftp/pathname.rb', line 79 def basename(*args) @path.basename(*args) end |
#basename_sans_extension ⇒ Object
84 85 86 |
# File 'lib/el_finder_ftp/pathname.rb', line 84 def basename_sans_extension @path.basename(@path.extname) end |
#child_directories(with_directory = true) ⇒ Object
110 111 112 |
# File 'lib/el_finder_ftp/pathname.rb', line 110 def child_directories(with_directory=true) adapter.children(self, with_directory).select{ |child| child.directory? }.map{|e| self.class.new(@adapter, e)} end |
#children(with_directory = true) ⇒ Object
121 122 123 |
# File 'lib/el_finder_ftp/pathname.rb', line 121 def children(with_directory=true) adapter.children(self, with_directory).map{|e| self.class.new(@adapter, e)} end |
#cleanpath ⇒ Object
69 70 71 |
# File 'lib/el_finder_ftp/pathname.rb', line 69 def cleanpath fullpath.cleanpath end |
#dirname ⇒ Object
94 95 96 |
# File 'lib/el_finder_ftp/pathname.rb', line 94 def dirname self.class.new(@adapter, @path.dirname) end |
#duplicate ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/el_finder_ftp/pathname.rb', line 147 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(@adapter, dirname + "#{_basename} copy #{copy}#{extname}") copy += 1 end while new_file.exist? new_file end |
#extname ⇒ Object
99 100 101 |
# File 'lib/el_finder_ftp/pathname.rb', line 99 def extname @path.nil? ? '' : @path.extname end |
#files(with_directory = true) ⇒ Object
115 116 117 |
# File 'lib/el_finder_ftp/pathname.rb', line 115 def files(with_directory=true) adapter.children(self, with_directory).select{ |child| child.file? }.map{|e| self.class.new(@adapter, e)} end |
#fullpath ⇒ Object
64 65 66 |
# File 'lib/el_finder_ftp/pathname.rb', line 64 def fullpath @fullpath ||= (@path.nil? ? @root : @root + @path) end |
#is_root? ⇒ Boolean
44 45 46 |
# File 'lib/el_finder_ftp/pathname.rb', line 44 def is_root? @path.to_s == '.' end |
#outside_of_root? ⇒ Boolean
59 60 61 |
# File 'lib/el_finder_ftp/pathname.rb', line 59 def outside_of_root? !cleanpath.to_s.start_with?(@root.to_s) end |
#realpath ⇒ Object
74 75 76 |
# File 'lib/el_finder_ftp/pathname.rb', line 74 def realpath fullpath.realpath end |
#relative? ⇒ Boolean
54 55 56 |
# File 'lib/el_finder_ftp/pathname.rb', line 54 def relative? @path.relative? end |
#relative_to(other) ⇒ Object
131 132 133 |
# File 'lib/el_finder_ftp/pathname.rb', line 131 def relative_to(other) @path.relative_path_from(other) end |
#rename(to) ⇒ Object
162 163 164 165 166 |
# File 'lib/el_finder_ftp/pathname.rb', line 162 def rename(to) to = self.class.new(@adapter, to) realpath.rename(to.fullpath.to_s) to end |
#to_s ⇒ Object Also known as: to_str
104 105 106 |
# File 'lib/el_finder_ftp/pathname.rb', line 104 def to_s cleanpath.to_s end |
#touch(options = {}) ⇒ Object
126 127 128 |
# File 'lib/el_finder_ftp/pathname.rb', line 126 def touch( = {}) adapter.touch(cleanpath, ) end |
#unique ⇒ Object
136 137 138 139 140 141 142 143 144 |
# File 'lib/el_finder_ftp/pathname.rb', line 136 def unique return self.dup unless fullpath.file? copy = 1 begin new_file = self.class.new(@adapter, dirname + "#{basename_sans_extension} #{copy}#{extname}") copy += 1 end while new_file.exist? new_file end |