Class: SgtnClient::SingleOperation

Inherits:
Object
  • Object
show all
Defined in:
lib/sgtn-client/common/single_operation.rb

Instance Method Summary collapse

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