Class: JustOneLock::World

Inherits:
Object
  • Object
show all
Defined in:
lib/just_one_lock/world.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWorld

Returns a new instance of World.



4
5
6
7
8
9
# File 'lib/just_one_lock/world.rb', line 4

def initialize
  @files = {}
  @output = $stdout
  @directory = '/tmp'
  @delete_files = true
end

Instance Attribute Details

#delete_filesObject

Returns the value of attribute delete_files.



2
3
4
# File 'lib/just_one_lock/world.rb', line 2

def delete_files
  @delete_files
end

#directoryObject

Returns the value of attribute directory.



2
3
4
# File 'lib/just_one_lock/world.rb', line 2

def directory
  @directory
end

#outputObject

Returns the value of attribute output.



2
3
4
# File 'lib/just_one_lock/world.rb', line 2

def output
  @output
end

Instance Method Details

#after_lock(name, file) ⇒ Object



30
31
32
# File 'lib/just_one_lock/world.rb', line 30

def after_lock(name, file)
  delete_unlocked_files if delete_files
end

#before_lock(name, file) ⇒ Object



26
27
28
# File 'lib/just_one_lock/world.rb', line 26

def before_lock(name, file)
  @files[name] = file
end

#delete_unlocked_filesObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/just_one_lock/world.rb', line 11

def delete_unlocked_files
  paths_to_delete = []

  @files.each do |path, f|
    if File.exists?(path) && f.closed?
      paths_to_delete << path
    end
  end

  paths_to_delete.each do |path|
    File.delete(path)
    @files.delete(path)
  end
end

#lock_pathsObject



34
35
36
# File 'lib/just_one_lock/world.rb', line 34

def lock_paths
  @files.keys
end

#puts(*args) ⇒ Object



38
39
40
# File 'lib/just_one_lock/world.rb', line 38

def puts(*args)
  output.puts(*args)
end