Class: Doubleshot::Lockfile

Inherits:
Object show all
Defined in:
lib/doubleshot/lockfile.rb

Defined Under Namespace

Classes: UnknownDependencyTypeError, UnlockedDependencyError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = "Doubleshot.lock") ⇒ Lockfile

Returns a new instance of Lockfile.



18
19
20
21
22
# File 'lib/doubleshot/lockfile.rb', line 18

def initialize(path = "Doubleshot.lock")
  @path = Pathname(path.to_s)
  @gems = Dependencies::GemDependencyList.new
  @jars = Dependencies::JarDependencyList.new
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



16
17
18
# File 'lib/doubleshot/lockfile.rb', line 16

def path
  @path
end

Instance Method Details

#add(dependency) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/doubleshot/lockfile.rb', line 50

def add(dependency)
  if dependency.class == Dependencies::Dependency
    raise ArgumentError.new("+dependency+ must be a concrete type (JarDependency or GemDependency).")
  elsif dependency.class < Dependencies::Dependency
    if dependency.locked?
      case dependency
        when Dependencies::JarDependency then @jars.add(dependency)
        when Dependencies::GemDependency then @gems.add(dependency)
        else raise UnknownDependencyTypeError.new(dependency)
      end
    else
      raise UnlockedDependencyError.new(dependency)
    end
  else
    raise ArgumentError.new("+dependency+ must be a type of Doubleshot::Dependencies::Dependency.")
  end

  self
end

#deleteObject



24
25
26
# File 'lib/doubleshot/lockfile.rb', line 24

def delete
  @path.delete
end

#empty?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/doubleshot/lockfile.rb', line 46

def empty?
  @gems.empty? && @jars.empty?
end

#exist?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/doubleshot/lockfile.rb', line 28

def exist?
  @path.exist?
end

#flush!Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/doubleshot/lockfile.rb', line 88

def flush!
  output = { "JARS" => [], "GEMS" => [] }

  jars.each do |jar|
    output["JARS"] << jar.to_s(true)
  end

  gems.each do |gem|
    output["GEMS"] << gem.to_s(true)
  end

  @path.open("w+") do |file|
    file << output.to_yaml
  end
end

#gemsObject



36
37
38
39
# File 'lib/doubleshot/lockfile.rb', line 36

def gems
  load
  ReadonlyCollection.new @gems
end

#jarsObject



41
42
43
44
# File 'lib/doubleshot/lockfile.rb', line 41

def jars
  load
  ReadonlyCollection.new @jars
end

#loadObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/doubleshot/lockfile.rb', line 70

def load
  unless @loaded
    (data["JARS"] || []).each do |coordinate|
      @jars.add Dependencies::JarDependency.new(coordinate)
    end

    (data["GEMS"] || []).each do |spec|
      name, version = *spec.split(":")
      dependency = Dependencies::GemDependency.new(name)
      dependency.lock(version)

      @gems.add dependency
    end
  end

  @loaded = true
end

#mtimeObject



32
33
34
# File 'lib/doubleshot/lockfile.rb', line 32

def mtime
  @path.mtime
end