mirror of
https://github.com/wahyd4/gitlabhq.git
synced 2026-08-10 05:06:46 +10:00
82 lines
1.5 KiB
Ruby
82 lines
1.5 KiB
Ruby
module Gitlab
|
|
##
|
|
# Class that represents a path to a file or directory
|
|
#
|
|
# This is IO-operations safe class, that does similar job to
|
|
# Ruby's Pathname but without the risk of accessing filesystem.
|
|
#
|
|
#
|
|
class StringPath
|
|
attr_reader :path, :universe
|
|
|
|
def initialize(path, universe)
|
|
@path = path
|
|
@universe = universe
|
|
end
|
|
|
|
def to_s
|
|
@path
|
|
end
|
|
|
|
def absolute?
|
|
@path.start_with?('/')
|
|
end
|
|
|
|
def relative?
|
|
!absolute?
|
|
end
|
|
|
|
def directory?
|
|
@path.end_with?('/')
|
|
end
|
|
|
|
def file?
|
|
!directory?
|
|
end
|
|
|
|
def has_parent?
|
|
@universe.include?(@path.sub(basename, ''))
|
|
end
|
|
|
|
def parent
|
|
return nil unless has_parent?
|
|
new(@path.sub(basename, ''))
|
|
end
|
|
|
|
def descendants
|
|
return [] unless directory?
|
|
children = @universe.select { |entry| entry =~ /^#{@path}.+/ }
|
|
children.map { |path| new(path) }
|
|
end
|
|
|
|
def children
|
|
descendants.select { |descendant| descendant.parent == self }
|
|
end
|
|
|
|
def directories
|
|
return [] unless directory?
|
|
children.select { |child| child.directory? }
|
|
end
|
|
|
|
def files
|
|
return [] unless directory?
|
|
children.select { |child| child.file? }
|
|
end
|
|
|
|
def basename
|
|
name = @path.split(::File::SEPARATOR).last
|
|
directory? ? name + ::File::SEPARATOR : name
|
|
end
|
|
|
|
def ==(other)
|
|
@path == other.path && @universe == other.universe
|
|
end
|
|
|
|
private
|
|
|
|
def new(path)
|
|
self.class.new(path, @universe)
|
|
end
|
|
end
|
|
end
|