Class: Alchemy::Custom::Model::ElFinder::PathName

Inherits:
Object
  • Object
show all
Defined in:
lib/alchemy/custom/model/el_finder/path_name.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, path = '.') ⇒ PathName

Returns a new instance of PathName.

Raises:

  • (SecurityError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/alchemy/custom/model/el_finder/path_name.rb', line 7

def path
  @path
end

#rootObject (readonly)

Returns the value of attribute root.



7
8
9
# File 'lib/alchemy/custom/model/el_finder/path_name.rb', line 7

def root
  @root
end

Instance Method Details

#+(other) ⇒ Object



31
32
33
34
35
36
# 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?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/alchemy/custom/model/el_finder/path_name.rb', line 46

def absolute?
  @path.absolute?
end

#basename(*args) ⇒ Object



88
89
90
# File 'lib/alchemy/custom/model/el_finder/path_name.rb', line 88

def basename(*args)
  @path.basename(*args)
end

#basename_sans_extensionObject



95
96
97
# 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) ⇒ Object



131
132
133
# 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) ⇒ Object



136
137
138
# 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

#cleanpathObject



74
75
76
# File 'lib/alchemy/custom/model/el_finder/path_name.rb', line 74

def cleanpath
  fullpath.cleanpath
end

#dirnameObject



109
110
111
# File 'lib/alchemy/custom/model/el_finder/path_name.rb', line 109

def dirname
  self.class.new(@root, @path.dirname)
end

#duplicateObject



164
165
166
167
168
169
170
171
172
173
174
175
176
# 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

#extnameObject



116
117
118
# File 'lib/alchemy/custom/model/el_finder/path_name.rb', line 116

def extname
  @path.nil? ? '' : @path.extname
end

#fullpathObject



67
68
69
# File 'lib/alchemy/custom/model/el_finder/path_name.rb', line 67

def fullpath
  @path.nil? ? @root : @root + @path
end

#is_root?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/alchemy/custom/model/el_finder/path_name.rb', line 41

def is_root?
  @path.to_s == '.'
end

#outside_of_root?Boolean

Returns:

  • (Boolean)


60
61
62
# 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

#realpathObject



81
82
83
# File 'lib/alchemy/custom/model/el_finder/path_name.rb', line 81

def realpath
  fullpath.realpath
end

#relative?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/alchemy/custom/model/el_finder/path_name.rb', line 53

def relative?
  @path.relative?
end

#relative_to(other) ⇒ Object



146
147
148
# File 'lib/alchemy/custom/model/el_finder/path_name.rb', line 146

def relative_to(other)
  @path.relative_path_from(other)
end

#rename(to) ⇒ Object



181
182
183
184
185
186
187
188
# 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_sObject Also known as: to_str



123
124
125
# File 'lib/alchemy/custom/model/el_finder/path_name.rb', line 123

def to_s
  cleanpath.to_s
end

#touch(options = {}) ⇒ Object



141
142
143
# File 'lib/alchemy/custom/model/el_finder/path_name.rb', line 141

def touch(options = {})
  FileUtils.touch(cleanpath, options)
end

#uniqueObject



151
152
153
154
155
156
157
158
159
# 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