58 lines
983 B
Ruby
Raw Normal View History

2017-04-27 08:48:29 +02:00
class PATH
def initialize(*paths)
@paths = parse(*paths)
end
def prepend(*paths)
@paths.unshift(*parse(*paths))
self
end
def append(*paths)
@paths.concat(parse(*paths))
self
end
def to_ary
@paths
end
alias to_a to_ary
def to_str
@paths.join(File::PATH_SEPARATOR)
end
alias to_s to_str
2017-04-28 11:12:02 +02:00
def ==(other)
2017-04-27 08:48:29 +02:00
if other.respond_to?(:to_ary)
return true if to_ary == other.to_ary
end
if other.respond_to?(:to_str)
return true if to_str == other.to_str
end
false
end
def empty?
@paths.empty?
end
def validate
validated_path = self.class.new(@paths.select(&File.method(:directory?)))
validated_path unless validated_path.empty?
2017-04-27 08:48:29 +02:00
end
private
def parse(*paths)
paths
.flatten
.flat_map { |p| p.respond_to?(:to_str) ? p.to_str.split(File::PATH_SEPARATOR): p }
.compact
.map { |p| p.respond_to?(:to_path) ? p.to_path : p.to_str }
.uniq
end
end