Module: Fiber::Annotation

Included in:
Fiber
Defined in:
lib/fiber/annotation.rb,
lib/fiber/annotation/version.rb

Overview

A mechanism for annotating fibers.

Constant Summary collapse

VERSION =
"0.2.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#annotationObject

Get the current annotation.



20
21
22
# File 'lib/fiber/annotation.rb', line 20

def annotation
  @annotation
end

Instance Method Details

#annotate(annotation) ⇒ Object

Annotate the current fiber with the given annotation.

If a block is given, the annotation is set for the duration of the block and then restored to the previous value.

The block form of this method should only be invoked on the current fiber.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fiber/annotation.rb', line 31

def annotate(annotation)
	if block_given?
		raise "Cannot annotation a different fiber!" unless Fiber.current == self
		
		begin
			current_annotation = @annotation
			@annotation = annotation
			return yield
		ensure
			@annotation = current_annotation
		end
	else
		@annotation = annotation
	end
end

#initialize(annotation: nil, **options, &block) ⇒ Object

Annotate the current fiber with the given annotation.



13
14
15
16
# File 'lib/fiber/annotation.rb', line 13

def initialize(annotation: nil, **options, &block)
	@annotation = annotation
	super(**options, &block)
end