Class: SgtnClient::SingleOperation
- Inherits:
-
Object
- Object
- SgtnClient::SingleOperation
- Defined in:
- lib/sgtn-client/common/single_operation.rb
Instance Method Summary collapse
-
#initialize(*conditions, &creator) ⇒ SingleOperation
constructor
A new instance of SingleOperation.
-
#operate(id, &block) ⇒ Object
return new created object (return nil possibly).
- #remove_object(id) ⇒ Object
Constructor Details
#initialize(*conditions, &creator) ⇒ SingleOperation
Returns a new instance of SingleOperation.
6 7 8 9 10 11 12 13 14 |
# File 'lib/sgtn-client/common/single_operation.rb', line 6 def initialize(*conditions, &creator) raise 'no way to create a new obj' unless creator @lock = Mutex.new @hash = {} @conditions = conditions @creator = creator end |
Instance Method Details
#operate(id, &block) ⇒ Object
return new created object (return nil possibly)
17 18 19 20 21 22 23 24 25 |
# File 'lib/sgtn-client/common/single_operation.rb', line 17 def operate(id, &block) @lock.synchronize do obj = @hash[id] @conditions.each do |con| return obj unless con.call(id, obj) end @hash[id] = @creator.call(id, &block) end end |
#remove_object(id) ⇒ Object
27 28 29 30 31 |
# File 'lib/sgtn-client/common/single_operation.rb', line 27 def remove_object(id) @lock.synchronize do @hash.delete(id) end end |