Class: Quick::Sampler::DSL

Inherits:
Object
  • Object
show all
Includes:
SimpleCombinators, SimpleValues
Defined in:
lib/quick/sampler/dsl.rb

Overview

A domain specific method for sampler definitions.

Instance methods of this class are available as barewords inside a sampler definition supplied as a block to compile.

Methods that produce a sampler actually wrap it in a Fluidiom instance that adds a fluid API to the sampler. This wrapping is stripped off from the sampler returned by compile - although I'm still undesided if that's the right thing to do.

(Incidentally, Fluidiom instances for deeper nested sub-samplers get leaked from compile at the moment)

Defined Under Namespace

Modules: SimpleCombinators, SimpleValues Classes: Fluidiom

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SimpleCombinators

#combine, #hash_like, #list_like, #list_of, #merge, #object_like, #one_of, #one_of_weighted, #pick_from, #send_to

Methods included from SimpleValues

#boolean, #const, #fixnum, #integer, #negative_fixnum, #positive_fixnum, #probability, #string, #weighted_truth, #zero

Constructor Details

#initialize(binding = nil) ⇒ DSL

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of DSL.

Parameters:

  • binding (Binding) (defaults to: nil)

    a context to lookup unknown methods



21
22
23
# File 'lib/quick/sampler/dsl.rb', line 21

def initialize binding = nil
  setup_delegation(binding) if binding
end

Class Method Details

.compile(description: nil, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

(see Quick::Sampler.compile)



27
28
29
30
31
# File 'lib/quick/sampler/dsl.rb', line 27

def self.compile description: nil, &block
  new(block.binding).instance_eval(&block).unwrap.tap do |sampler|
    sampler.description = description
  end
end

Instance Method Details

#feed(enum = nil, &block) ⇒ Object

Sample an enumerable or block

The block is ignored if enum parameter isn't nil

Parameters:

  • enum (Enumerable) (defaults to: nil)

    an enumerable to sample values from

Yield Returns:

  • (<Sample>)

    a sampled value



45
46
47
48
# File 'lib/quick/sampler/dsl.rb', line 45

def feed enum = nil, &block
  enum ||= block if block_given?
  Fluidiom.new(Base.new(enum))
end

#inspectObject

overloaded to display human readable text in tests output



34
35
36
# File 'lib/quick/sampler/dsl.rb', line 34

def inspect
  "Quick Sampler DSL"
end