Class: ElFinder::Pathname
- Inherits:
-
Object
- Object
- ElFinder::Pathname
- Defined in:
- lib/el_finder/pathname.rb
Instance Attribute Summary collapse
-
#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
- #fullpath ⇒ Object
-
#initialize(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(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 |
# 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 |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/el_finder/pathname.rb', line 7 def path @path end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
7 8 9 |
# File 'lib/el_finder/pathname.rb', line 7 def root @root end |
Instance Method Details
#+(other) ⇒ Object
29 30 31 32 33 34 |
# 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? ⇒ Boolean
42 43 44 |
# File 'lib/el_finder/pathname.rb', line 42 def absolute? @path.absolute? end |
#basename(*args) ⇒ Object
72 73 74 |
# File 'lib/el_finder/pathname.rb', line 72 def basename(*args) @path.basename(*args) end |
#basename_sans_extension ⇒ Object
77 78 79 |
# File 'lib/el_finder/pathname.rb', line 77 def basename_sans_extension @path.basename(@path.extname) end |
#child_directories(with_directory = true) ⇒ Object
103 104 105 |
# 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) ⇒ Object
108 109 110 |
# 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 ⇒ Object
62 63 64 |
# File 'lib/el_finder/pathname.rb', line 62 def cleanpath fullpath.cleanpath end |
#dirname ⇒ Object
87 88 89 |
# File 'lib/el_finder/pathname.rb', line 87 def dirname self.class.new(@root, @path.dirname) end |
#duplicate ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 |
# 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 ⇒ Object
92 93 94 |
# File 'lib/el_finder/pathname.rb', line 92 def extname @path.nil? ? '' : @path.extname end |
#fullpath ⇒ Object
57 58 59 |
# File 'lib/el_finder/pathname.rb', line 57 def fullpath @path.nil? ? @root : @root + @path end |
#is_root? ⇒ Boolean
37 38 39 |
# File 'lib/el_finder/pathname.rb', line 37 def is_root? @path.to_s == '.' end |
#outside_of_root? ⇒ Boolean
52 53 54 |
# File 'lib/el_finder/pathname.rb', line 52 def outside_of_root? !cleanpath.to_s.start_with?(@root.to_s) end |
#realpath ⇒ Object
67 68 69 |
# File 'lib/el_finder/pathname.rb', line 67 def realpath fullpath.realpath end |
#relative? ⇒ Boolean
47 48 49 |
# File 'lib/el_finder/pathname.rb', line 47 def relative? @path.relative? end |
#relative_to(other) ⇒ Object
118 119 120 |
# File 'lib/el_finder/pathname.rb', line 118 def relative_to(other) @path.relative_path_from(other) end |
#rename(to) ⇒ Object
149 150 151 152 153 154 155 156 |
# 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 ⇒ Object Also known as: to_str
97 98 99 |
# File 'lib/el_finder/pathname.rb', line 97 def to_s cleanpath.to_s end |
#touch(options = {}) ⇒ Object
113 114 115 |
# File 'lib/el_finder/pathname.rb', line 113 def touch( = {}) FileUtils.touch(cleanpath)#, options) end |
#unique ⇒ Object
123 124 125 126 127 128 129 130 131 |
# 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 |