Class: Jim::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/jim/index.rb

Overview

Index managages a list of directories which are searched to find requirements

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*directories) ⇒ Index

Initialize an Index with a list of directories. The firse directory is assumed to be the JIMHOME



8
9
10
11
# File 'lib/jim/index.rb', line 8

def initialize(*directories)
  @directories = [directories].flatten.compact
  @jimhome_re  = /#{Pathname.new(@directories.first).expand_path.to_s}/
end

Instance Attribute Details

#directoriesObject (readonly)

Returns the value of attribute directories.



4
5
6
# File 'lib/jim/index.rb', line 4

def directories
  @directories
end

Instance Method Details

#add(directory) ⇒ Object

Add a directory to the index



14
15
16
# File 'lib/jim/index.rb', line 14

def add(directory)
  @directories.unshift directory
end

#each_file_in_index(ext, &block) ⇒ Object

Iterate through every file in the index yielding the path to the block



85
86
87
# File 'lib/jim/index.rb', line 85

def each_file_in_index(ext, &block)
  Jim.each_path_in_directories(@directories, ext, [], &block)
end

#find(name, version = nil) ⇒ Object

Find a file in the index by ‘name` and an optional `version`. If found, returns a `Pathname` where the file can be retrieved.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/jim/index.rb', line 43

def find(name, version = nil)
  name     = Pathname.new(name)
  stem     = name.basename
  version  = version && version.strip != '' ? version.strip : nil
  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
        block_given? ? yield(final) : break
      end
    end
    break if final && !block_given?
  end
  final
end

#find_all(name, version = nil) ⇒ Object

Find all paths matching ‘name` and `version`. Returning an array.



73
74
75
76
77
# File 'lib/jim/index.rb', line 73

def find_all(name, version = nil)
  matched = []
  find(name, version) {|p| matched << p }
  matched
end

#in_jimhome?(path) ⇒ Boolean

Is this path in the JIMHOME

Returns:

  • (Boolean)


80
81
82
# File 'lib/jim/index.rb', line 80

def in_jimhome?(path)
  !!(path.to_s =~ @jimhome_re)
end

#list(search = nil) ⇒ Object

List all available files in the directories or only those matching ‘search`. Returns a sorted array of arrays.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jim/index.rb', line 20

def list(search = nil)
  list = {}
  each_file_in_index('.js') do |filename|
    if /lib\/([^\/\-]+)-([\d\w\.\-]+)\/.+/.match filename
      name    = $1
      version = $2
    else
      name, version = Jim::VersionParser.parse_filename(filename)
    end
    if name && version
      list[name] ||= []
      list[name] << [version, filename]
    end
  end
  if search
    search = /#{search}/i
    list = list.find_all {|lib| lib[0] =~ search }
  end
  list.sort
end