Class: Going::SelectStatement
- Inherits:
-
Object
- Object
- Going::SelectStatement
- Extended by:
- BooleanAttrReader
- Defined in:
- lib/going/select_statement.rb
Class Method Summary collapse
Instance Method Summary collapse
- #complete(*args, &on_complete) ⇒ Object
-
#initialize ⇒ SelectStatement
constructor
A new instance of SelectStatement.
- #once(*args, &blk) ⇒ Object
- #secondary_complete(*args, &on_complete) ⇒ Object
- #select(&blk) ⇒ Object
- #when_complete(*args, &callback) ⇒ Object
Methods included from BooleanAttrReader
Constructor Details
#initialize ⇒ SelectStatement
Returns a new instance of SelectStatement.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/going/select_statement.rb', line 34 def initialize @completed = false @once_mutex = Mutex.new @complete_mutex = Mutex.new @semaphore = ConditionVariable.new @when_completes = [] @args = nil @on_complete = nil end |
Class Method Details
.instance ⇒ Object
7 8 9 |
# File 'lib/going/select_statement.rb', line 7 def instance Thread.current[global_key] end |
.instance? ⇒ Boolean
11 12 13 |
# File 'lib/going/select_statement.rb', line 11 def instance? !instance.nil? end |
.new_instance ⇒ Object
15 16 17 |
# File 'lib/going/select_statement.rb', line 15 def new_instance self.instance = new end |
.reset ⇒ Object
19 20 21 |
# File 'lib/going/select_statement.rb', line 19 def reset self.instance = nil end |
Instance Method Details
#complete(*args, &on_complete) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/going/select_statement.rb', line 62 def complete(*args, &on_complete) complete_mutex.synchronize do if !completed? @args = args @on_complete = on_complete @completed = true @secondary_completed = true semaphore.signal end end end |
#once(*args, &blk) ⇒ Object
85 86 87 88 89 |
# File 'lib/going/select_statement.rb', line 85 def once(*args, &blk) once_mutex.synchronize do yield(*args) if block_given? && incomplete? end end |
#secondary_complete(*args, &on_complete) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/going/select_statement.rb', line 74 def secondary_complete(*args, &on_complete) complete_mutex.synchronize do if !secondary_completed? @args = args @on_complete = on_complete @secondary_completed = true semaphore.signal end end end |
#select(&blk) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/going/select_statement.rb', line 45 def select(&blk) select_helper = SelectHelper.instance if blk.arity == 1 yield select_helper else select_helper.instance_eval(&blk) end wait cleanup call_completion_block end |
#when_complete(*args, &callback) ⇒ Object
58 59 60 |
# File 'lib/going/select_statement.rb', line 58 def when_complete(*args, &callback) when_completes << proc { callback.call(*args) } end |