Sha256: eb04be384fe7312870b9f151b634332f75c9f740439f32b555c3d2eb1580645b

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 KB

Contents

require 'pathname'

module Codependency
  class Path < Array

    def initialize( extensions=%w| .rb | )
      @extensions = Array(extensions)
    end
    attr_reader :extensions

    ##
    # Appends a path to this path set. If the path exists, it
    # will be expanded. Raises Errno::ENOENT if the path
    # does not exist. Raises Errno::ENOTDIR if the path
    # is not a directory.
    def <<( str )
      path = Pathname( str )

      case
      when !path.exist?
        raise Errno::ENOENT, path.to_path
      when !path.directory?
        raise Errno::ENOTDIR, path.to_path
      else
        super path.expand_path
      end
    end

    ##
    # Sugar syntax for calling #find if given a string.
    # Otherwise, behaves like normal array access.
    def []( path_or_index )
      if path_or_index.is_a? String
        find path_or_index
      else
        super
      end
    end

    ##
    # Attempts to find the given file in this path set.
    # Raises Errno::ENOENT if the file cannot be found.
    def find( str )
      super lambda { raise Errno::ENOENT, str } do |dir|
        path = extensions.find do |ext|
          file = dir.join str + ext
          break file if file.exist?
        end
        break path if path
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
codependency-2.3.2 lib/codependency/path.rb
codependency-2.3.1 lib/codependency/path.rb
codependency-2.3.0 lib/codependency/path.rb
codependency-2.2.0 lib/codependency/path.rb
codependency-2.1.0 lib/codependency/path.rb
codependency-2.0.0 lib/codependency/path.rb