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