Class: OsCtld::Devices::ChangeSet

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/osctld/devices/change_set.rb

Overview

Track changed devices manager to reconfigure them just once

Instance Method Summary collapse

Constructor Details

#initializeChangeSet

Returns a new instance of ChangeSet.



14
15
16
17
# File 'lib/osctld/devices/change_set.rb', line 14

def initialize
  @mutex = Mutex.new
  @pools = {}
end

Instance Method Details

#add(pool, manager, sort_key) ⇒ Object

Add manager to the changeset

Parameters:



31
32
33
34
35
# File 'lib/osctld/devices/change_set.rb', line 31

def add(pool, manager, sort_key)
  sync do
    @pools[pool.name][sort_key] = manager
  end
end

#close(pool) ⇒ Object

Close the changeset, applying all changes

Parameters:



39
40
41
42
43
44
45
46
47
# File 'lib/osctld/devices/change_set.rb', line 39

def close(pool)
  changes = sync { @pools.delete(pool.name) }

  changes.sort do |a, b|
    a[0] <=> b[0]
  end.each do |_, manager|
    manager.apply
  end
end

#open(pool) ⇒ Object

Open new changeset on pool

Parameters:



21
22
23
24
25
# File 'lib/osctld/devices/change_set.rb', line 21

def open(pool)
  sync do
    @pools[pool.name] = {}
  end
end

#syncObject (protected)



51
52
53
# File 'lib/osctld/devices/change_set.rb', line 51

def sync(&)
  @mutex.synchronize(&)
end