Class: Hegel::Generators::CompositeGenerator

Inherits:
Hegel::Generator show all
Defined in:
lib/hegel/generators.rb,
sig/hegel.rbs

Overview

Hegel::Syntax::Methods#composite. Builds a generator from imperative code, the way Hypothesis's @composite decorator and hegel-rust's compose!/hegel-go's Composite let a caller assemble one value out of several draws without writing a Generator subclass: block receives a draw surface (BlockTestCase below) and can call #draw on it any number of times.

Spanned with HEGEL_LABEL_FLAT_MAP, not a label of its own: the header documents that label as the span around "a flat_map / monadic dependent draw", and a composite block is exactly that shape -- one or more draws, each free to depend on values the block already built, folded into a single result. No composite-specific label exists in the table this binding draws from (see lib_hegel.rb); if a true flat_map combinator is added later it can share this label, or the table can grow a dedicated one (hegel.h documents that a library may mint its own stable u64).

Defined Under Namespace

Classes: BlockTestCase

Instance Method Summary collapse

Methods inherited from Hegel::Generator

#filter, #map

Constructor Details

#initialize {|arg0| ... } ⇒ CompositeGenerator

Returns a new instance of CompositeGenerator.

Yields:

Yield Parameters:

Yield Returns:

  • (Object)


761
762
763
764
# File 'lib/hegel/generators.rb', line 761

def initialize(&block)
  super()
  @block = block
end

Instance Method Details

#do_draw(tc) ⇒ Object

Parameters:

  • tc (Object)

Returns:

  • (Object)

Raises:



766
767
768
769
770
771
772
773
774
775
# File 'lib/hegel/generators.rb', line 766

def do_draw(tc)
  raise Hegel::Error, "composite: block is required" unless @block

  tc.start_span(LibHegel::HEGEL_LABEL_FLAT_MAP)
  begin
    @block.call(BlockTestCase.new(tc))
  ensure
    tc.stop_span(discard: false)
  end
end