Class: Tango::DatabaseLocker

Inherits:
Object
  • Object
show all
Defined in:
lib/tango/database_locker.rb

Overview

Database locker in form of .lock file

Author:

  • Mckomo

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(candidates = [], lock_path = "./tmp/database.lock") ⇒ Tango::DatabaseLocker

Constructor of DatabaseLocker

Parameters:

  • candidates (Array) (defaults to: [])

    List of databases to be blocked

  • lock_path (String) (defaults to: "./tmp/database.lock")


15
16
17
18
# File 'lib/tango/database_locker.rb', line 15

def initialize( candidates = [], lock_path = "./tmp/database.lock" )
  @candidates = candidates
  @lock_path = lock_path
end

Instance Attribute Details

#lock_pathObject (readonly)

Returns the value of attribute lock_path.



8
9
10
# File 'lib/tango/database_locker.rb', line 8

def lock_path
  @lock_path
end

Instance Method Details

#lock(database) ⇒ Tango::DatabaseLocker

Lock given database

Examples:

unlocked = database_locker.unlocked # Find unlocked database
connect_to_db( unlocked )           # Connect to unlocked database
edit_db_data()                      # Change database data
database_locker.lock( unlocked )    # Lock database to be sure that any changes wont be made

Parameters:

  • database (String)

Returns:



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tango/database_locker.rb', line 36

def lock( database )
  
  @unlocked = nil
  
  File.open( lock_path, "w" ) do |f|
    f.write( database )
  end
  
  self
  
end

#unlockedObject

Return name of unlocked database

return [String]



23
24
25
# File 'lib/tango/database_locker.rb', line 23

def unlocked
  @unlocked ||= find_unlocked
end