Class: Herpes::Callback

Inherits:
Object show all
Defined in:
lib/herpes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time, discriminator, one_shot = false, &block) ⇒ Callback

Returns a new instance of Callback.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/herpes.rb', line 20

def initialize (time, discriminator, one_shot = false, &block)
	raise ArgumentError, 'no block has been passed' unless block

	@time          = time
	@discriminator = discriminator
	@one_shot      = one_shot
	@block         = block

	if !one_shot?
		@last = Time.at 0
	else
		called!
	end
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



18
19
20
# File 'lib/herpes.rb', line 18

def block
  @block
end

#discriminatorObject (readonly)

Returns the value of attribute discriminator.



18
19
20
# File 'lib/herpes.rb', line 18

def discriminator
  @discriminator
end

#lastObject (readonly)

Returns the value of attribute last.



18
19
20
# File 'lib/herpes.rb', line 18

def last
  @last
end

#timeObject (readonly)

Returns the value of attribute time.



18
19
20
# File 'lib/herpes.rb', line 18

def time
  @time
end

Instance Method Details

#call(herpes, &block) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/herpes.rb', line 66

def call (herpes, &block)
	return if calling?

	calling!
	@block.call(&block)
ensure
	called!
	herpes.wake_up
end

#called!Object



57
58
59
60
# File 'lib/herpes.rb', line 57

def called!
	@last    = Time.now
	@calling = false
end

#calling!Object



53
54
55
# File 'lib/herpes.rb', line 53

def calling!
	@calling = true
end

#calling?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/herpes.rb', line 62

def calling?
	@calling == true
end

#gonna_call!Object



43
44
45
46
47
# File 'lib/herpes.rb', line 43

def gonna_call!
	return if calling?

	@calling = :gonna
end

#gonna_call?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/herpes.rb', line 49

def gonna_call?
	@calling == :gonna
end

#next_inObject



39
40
41
# File 'lib/herpes.rb', line 39

def next_in
	time - (Time.now - last)
end

#one_shot?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/herpes.rb', line 35

def one_shot?
	@one_shot
end