Class: Alki::Loader::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/alki/loader/registry.rb

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



6
7
8
9
# File 'lib/alki/loader/registry.rb', line 6

def initialize
  @lock = Monitor.new
  clear
end

Instance Method Details

#add(entry) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/alki/loader/registry.rb', line 24

def add(entry)
  @lock.synchronize do
    @paths.delete_if{|e| e.path == entry.path }
    @paths << entry
    @names.sort!{|a,b| b.path <=> a.path}
    if entry.name
      @names.delete_if{|e| e.name == entry.name }
      @names << entry
      @names.sort!{|a,b| b.name <=> a.name}
    end
  end
end

#clearObject



11
12
13
14
15
16
17
# File 'lib/alki/loader/registry.rb', line 11

def clear
  @lock.synchronize do
    @paths = []
    @names = []
    @configs_loaded = false
  end
end

#load_alki_loader_filesObject



62
63
64
65
66
# File 'lib/alki/loader/registry.rb', line 62

def load_alki_loader_files
  Gem.find_latest_files('alki_loader.rb').each do |config_path|
    require config_path
  end
end

#load_configs!Object



53
54
55
56
57
58
59
60
# File 'lib/alki/loader/registry.rb', line 53

def load_configs!
  @lock.synchronize do
    path_hash = $LOAD_PATH.hash
    return if @configs_loaded == path_hash
    @configs_loaded = path_hash
    load_alki_loader_files
  end
end

#lookup_name(name) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/alki/loader/registry.rb', line 37

def lookup_name(name)
  load_configs!
  @names.find do |e|
    name.start_with?(e.name) &&
      (name.size == e.name.size || name[e.name.size] == '/')
  end
end

#lookup_path(path) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/alki/loader/registry.rb', line 45

def lookup_path(path)
  load_configs!
  @paths.find do |e|
    path.start_with?(e.path) &&
      (path.size == e.path.size || path[e.path.size] == '/')
  end
end

#pathsObject



19
20
21
22
# File 'lib/alki/loader/registry.rb', line 19

def paths
  load_configs!
  @paths.map{|e| e.path }
end