Sha256: ce45a795faf05843bd7d65d903cbd8c6a606f3ed985edecc21e66650f092388f

Contents?: true

Size: 712 Bytes

Versions: 1

Compression:

Stored size: 712 Bytes

Contents

require "pathname"

module MVCLI
  class Path
    def initialize(base)
      @base = Pathname(base.to_s)
    end

    def exists?(path)
      @base.join(path).exist?
    end

    def read(path)
      @base.join(path).read
    end

    def join(path)
      self.class.new @base.join path
    end

    def nearest(pattern)
      ancestors.each do |dir|
        if entry = dir.entries.find { |e| e.to_s.match pattern }
          return dir.join entry
        end
      end
    end

    def ancestors(dir = @base)
      if dir == dir.parent
        []
      else
        [dir] + ancestors(dir.parent)
      end
    end

    def to_s(path = nil)
      path.nil? ? @base.to_s : @base.join(path).to_s
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mvcli-0.1.0 lib/mvcli/path.rb