Class: Handsoap::Deferred

Inherits:
Object
  • Object
show all
Defined in:
lib/handsoap/deferred.rb

Instance Method Summary collapse

Constructor Details

#initializeDeferred



5
6
7
8
9
10
# File 'lib/handsoap/deferred.rb', line 5

def initialize
  @callback = nil
  @callback_cache = nil
  @errback = nil
  @errback_cache = nil
end

Instance Method Details

#callback(&block) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/handsoap/deferred.rb', line 17

def callback(&block)
  raise "Already assigned a block for callback" if @callback
  @callback = block
  if @callback_cache
    payload = @callback_cache
    trigger_callback(*payload)
  end
  self
end

#errback(&block) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/handsoap/deferred.rb', line 26

def errback(&block)
  raise "Already assigned a block for errback" if @errback
  @errback = block
  if @errback_cache
    payload = @errback_cache
    trigger_errback(*payload)
  end
  self
end

#has_callback?Boolean



11
12
13
# File 'lib/handsoap/deferred.rb', line 11

def has_callback?
  !! @callback
end

#has_errback?Boolean



14
15
16
# File 'lib/handsoap/deferred.rb', line 14

def has_errback?
  !! @errback
end

#trigger_callback(*args) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/handsoap/deferred.rb', line 35

def trigger_callback(*args)
  if @callback
    @callback.call(*args)
  else
    @callback_cache = args
  end
end

#trigger_errback(*args) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/handsoap/deferred.rb', line 42

def trigger_errback(*args)
  if @errback
    @errback.call(*args)
  else
    @errback_cache = args
  end
end