Sha256: 008c3ce0c520387248eaa706480e9dd4301e291cc18be647b5221d6437848427

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

module Jim
  class Index
    attr_reader :directories
    
    def initialize(*directories)
      @directories = directories.flatten
    end

    def add(directory)
      @directories.unshift directory
    end
    
    def list
      list = {}
      each_file_in_index('.js') do |filename|
        if version = filename.version
          name = filename.stem.gsub(/(\-[^\-]+)$/, '')
        elsif /lib\/([^\/]+)-([\d\w\.\-]+)\/.+/.match filename
          name    = $1
          version = $2
        else
          name = filename.stem
          version = '0'
        end
        if name && version
          list[name] ||= []
          list[name] << version unless list[name].include?(version)
        end
      end
      list.sort
    end

    def find(name, version = nil)
      name     = Pathname.new(name)
      stem     = name.basename
      ext      = '.js'
      possible_paths = if version
        [
          /#{stem}-#{version}\/#{name}#{ext}$/,
         /#{name}-#{version}#{ext}$/
        ]
      else
        [
          /#{name}#{ext}/,
          /#{name}-[\d\w\.\-]+#{ext}/
        ]
      end
      final = false
      each_file_in_index(ext) do |filename|
        possible_paths.each do |p|
          if File.file?(filename) && p.match(filename)
            final = Pathname.new(filename).expand_path
            break
          end
        end
        break if final
      end
      final
    end
    
    def each_file_in_index(ext, &block)
      @directories.each do |dir|
        Dir.glob(Pathname.new(dir) + '**' + "*#{ext}") do |filename|
          yield Pathname.new(filename)
        end
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jim-0.1.0 lib/jim/index.rb