Class: Gel::StubSet

Inherits:
Object
  • Object
show all
Defined in:
lib/gel/stub_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ StubSet

Returns a new instance of StubSet.



8
9
10
11
12
# File 'lib/gel/stub_set.rb', line 8

def initialize(root)
  @root = File.realpath(File.expand_path(root))
  @db = Gel::DB.new(root, "stubs")
  @dir = File.join(@root, "bin")
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



6
7
8
# File 'lib/gel/stub_set.rb', line 6

def root
  @root
end

Instance Method Details

#add(store, executables) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/gel/stub_set.rb', line 14

def add(store, executables)
  @db.writing do
    executables.each do |exe|
      create_stub(exe) unless File.exist?(bin(exe))
      @db[exe] = (@db[exe] || []) + [store]
    end
  end
end

#bin(exe) ⇒ Object



48
49
50
# File 'lib/gel/stub_set.rb', line 48

def bin(exe)
  File.join(@dir, exe)
end

#create_stub(exe) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/gel/stub_set.rb', line 37

def create_stub(exe)
  Dir.mkdir(@dir) unless Dir.exist?(@dir)

  File.open(bin(exe), "w", 0755) do |f|
    f.write(<<STUB)
#!/usr/bin/env gel stub #{exe}
# This file is generated and managed by Gel.
STUB
  end
end

#remove(store, executables) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gel/stub_set.rb', line 23

def remove(store, executables)
  @db.writing do
    executables.each do |exe|
      remaining_stores = (@db[exe] || []) - [store]
      if remaining_stores.empty?
        File.unlink(bin(exe))
        @db.delete(exe)
      else
        @db[exe] = remaining_stores
      end
    end
  end
end