Class: ElFinderS3::Pathname
- Inherits:
-
Object
- Object
- ElFinderS3::Pathname
- Defined in:
- lib/el_finder_s3/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_prefix_s ⇒ Object
- #to_s ⇒ Object (also: #to_str)
- #touch(options = {}) ⇒ Object
-
#type(type) ⇒ Object
of initialize.
- #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_s3/pathname.rb', line 10 def initialize(adapter_or_root, path = '.') @adapter = adapter_or_root.is_a?(ElFinderS3::Pathname) ? adapter_or_root.adapter : adapter_or_root @root = path.is_a?(ElFinderS3::Pathname) ? path.root : S3Pathname.new(@adapter, '/') if path.is_a?(ElFinderS3::Pathname) @path = path.path elsif path.is_a?(ElFinderS3::S3Pathname) @path = path else @path = S3Pathname.new(@adapter, path) end if absolute? if @path.cleanpath.to_s.start_with?(@root.to_s) @path = S3Pathname.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 = S3Pathname.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_s3/pathname.rb', line 7 def adapter @adapter end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/el_finder_s3/pathname.rb', line 7 def path @path end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
7 8 9 |
# File 'lib/el_finder_s3/pathname.rb', line 7 def root @root end |
Instance Method Details
#+(other) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/el_finder_s3/pathname.rb', line 42 def +(other) if other.is_a? ::ElFinderS3::Pathname other = other.path end self.class.new(@adapter, @path + other) end |
#absolute? ⇒ Boolean
57 58 59 |
# File 'lib/el_finder_s3/pathname.rb', line 57 def absolute? @path.absolute? end |
#basename(*args) ⇒ Object
101 102 103 |
# File 'lib/el_finder_s3/pathname.rb', line 101 def basename(*args) @path.basename(*args) end |
#basename_sans_extension ⇒ Object
108 109 110 |
# File 'lib/el_finder_s3/pathname.rb', line 108 def basename_sans_extension @path.basename(@path.extname) end |
#child_directories(with_directory = true) ⇒ Object
158 159 160 |
# File 'lib/el_finder_s3/pathname.rb', line 158 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
169 170 171 |
# File 'lib/el_finder_s3/pathname.rb', line 169 def children(with_directory=true) adapter.children(self, with_directory).map { |e| self.class.new(@adapter, e) } end |
#cleanpath ⇒ Object
87 88 89 |
# File 'lib/el_finder_s3/pathname.rb', line 87 def cleanpath fullpath.cleanpath end |
#dirname ⇒ Object
122 123 124 |
# File 'lib/el_finder_s3/pathname.rb', line 122 def dirname self.class.new(@adapter, @path.dirname) end |
#duplicate ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/el_finder_s3/pathname.rb', line 197 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
129 130 131 |
# File 'lib/el_finder_s3/pathname.rb', line 129 def extname @path.nil? ? '' : @path.extname end |
#files(with_directory = true) ⇒ Object
163 164 165 |
# File 'lib/el_finder_s3/pathname.rb', line 163 def files(with_directory=true) adapter.children(self, with_directory).select { |child| child.file? }.map { |e| self.class.new(@adapter, e) } end |
#fullpath ⇒ Object
78 79 80 81 82 |
# File 'lib/el_finder_s3/pathname.rb', line 78 def fullpath @fullpath ||= (@path.nil? ? @root : @root + @path) b = @path.nil? ? @root : @root + @path return @fullpath end |
#is_root? ⇒ Boolean
52 53 54 |
# File 'lib/el_finder_s3/pathname.rb', line 52 def is_root? @path.to_s == '.' end |
#outside_of_root? ⇒ Boolean
71 72 73 |
# File 'lib/el_finder_s3/pathname.rb', line 71 def outside_of_root? !cleanpath.to_s.start_with?(@root.to_s) end |
#realpath ⇒ Object
94 95 96 |
# File 'lib/el_finder_s3/pathname.rb', line 94 def realpath fullpath.realpath end |
#relative? ⇒ Boolean
64 65 66 |
# File 'lib/el_finder_s3/pathname.rb', line 64 def relative? @path.relative? end |
#relative_to(other) ⇒ Object
179 180 181 |
# File 'lib/el_finder_s3/pathname.rb', line 179 def relative_to(other) @path.relative_path_from(other) end |
#rename(to) ⇒ Object
214 215 216 217 218 |
# File 'lib/el_finder_s3/pathname.rb', line 214 def rename(to) to = self.class.new(@adapter, to) realpath.rename(to.fullpath.to_s) to end |
#to_prefix_s ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/el_finder_s3/pathname.rb', line 140 def to_prefix_s prefix_s = cleanpath.to_s if prefix_s == '/' return '' elsif prefix_s[0] == '/' prefix_s[0] = '' end if prefix_s[prefix_s.size-1] != '/' prefix_s = prefix_s + '/' end return prefix_s end |
#to_s ⇒ Object Also known as: to_str
136 137 138 |
# File 'lib/el_finder_s3/pathname.rb', line 136 def to_s cleanpath.to_s end |
#touch(options = {}) ⇒ Object
174 175 176 |
# File 'lib/el_finder_s3/pathname.rb', line 174 def touch( = {}) adapter.touch(cleanpath, ) end |
#type(type) ⇒ Object
of initialize
37 38 39 |
# File 'lib/el_finder_s3/pathname.rb', line 37 def type(type) @type = type end |
#unique ⇒ Object
184 185 186 187 188 189 190 191 192 |
# File 'lib/el_finder_s3/pathname.rb', line 184 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 |