Class: GitCompound::Lock

Inherits:
Object
  • Object
show all
Defined in:
lib/git_compound/lock.rb

Overview

Class that represents lock file

Constant Summary collapse

FILENAME =
'.gitcompound.lock'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = FILENAME) ⇒ Lock

Returns a new instance of Lock.



13
14
15
16
17
# File 'lib/git_compound/lock.rb', line 13

def initialize(file = FILENAME)
  @file   = file
  @locked = YAML.load(File.read(file)) if File.exist?(file)
  clean unless @locked.is_a? Hash
end

Class Method Details

.exist?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/git_compound/lock.rb', line 9

def self.exist?
  File.exist?(FILENAME)
end

Instance Method Details

#cleanObject



19
20
21
22
# File 'lib/git_compound/lock.rb', line 19

def clean
  @locked = { manifest: '', components: [] }
  self
end

#componentsObject



36
37
38
39
40
41
42
43
44
# File 'lib/git_compound/lock.rb', line 36

def components
  @locked[:components].to_a.map do |locked|
    Component.new(locked[:name].to_sym) do
      sha locked[:sha]
      source locked[:source]
      destination locked[:destination]
    end
  end
end

#contentsObject



46
47
48
# File 'lib/git_compound/lock.rb', line 46

def contents
  @locked
end

#find(component) ⇒ Object



50
51
52
53
54
# File 'lib/git_compound/lock.rb', line 50

def find(component)
  components.find do |locked_component|
    locked_component.path == component.path
  end
end

#lock_component(component) ⇒ Object



28
29
30
# File 'lib/git_compound/lock.rb', line 28

def lock_component(component)
  @locked[:components] << component.to_hash
end

#lock_manifest(manifest) ⇒ Object



24
25
26
# File 'lib/git_compound/lock.rb', line 24

def lock_manifest(manifest)
  @locked[:manifest] = manifest.md5sum
end

#manifestObject



32
33
34
# File 'lib/git_compound/lock.rb', line 32

def manifest
  @locked[:manifest]
end

#process(worker) ⇒ Object



56
57
58
# File 'lib/git_compound/lock.rb', line 56

def process(worker)
  components.each { |component| worker.visit_component(component) }
end

#writeObject



60
61
62
# File 'lib/git_compound/lock.rb', line 60

def write
  File.open(@file, 'w') { |f| f.puts @locked.to_yaml }
end