Class: Vizcore::DSL::ReactionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/vizcore/dsl/reaction_builder.rb

Overview

Collects high-level ‘react_to` DSL entries and converts them to mappings.

Instance Method Summary collapse

Constructor Details

#initialize(mapping_factory:) ⇒ ReactionBuilder

Returns a new instance of ReactionBuilder.

Parameters:

  • mapping_factory (#call)

    builds one normalized mapping hash



8
9
10
11
# File 'lib/vizcore/dsl/reaction_builder.rb', line 8

def initialize(mapping_factory:)
  @mapping_factory = mapping_factory
  @mappings = []
end

Instance Method Details

#change(target, **options) ⇒ void

This method returns an undefined value.

Continuously map the reaction source to a target parameter.

Parameters:

  • target (Symbol, String)

    layer parameter name

  • options (Hash)

    mapping transform options



30
31
32
# File 'lib/vizcore/dsl/reaction_builder.rb', line 30

def change(target, **options)
  @mappings << @mapping_factory.call(target, options)
end

#evaluate { ... } ⇒ Array<Hash>

Evaluate a ‘react_to` block.

Yields:

  • Reaction DSL methods

Returns:

  • (Array<Hash>)

    normalized mapping payloads

Raises:

  • (ArgumentError)

    when the block does not define any reaction



18
19
20
21
22
23
# File 'lib/vizcore/dsl/reaction_builder.rb', line 18

def evaluate(&block)
  instance_eval(&block) if block
  raise ArgumentError, "react_to requires at least one change or trigger" if @mappings.empty?

  @mappings.map(&:dup)
end

#trigger(target, **options) ⇒ void

This method returns an undefined value.

Map the reaction source to an event-like target parameter.

Parameters:

  • target (Symbol, String)

    layer parameter name

  • options (Hash)

    mapping transform options



39
40
41
# File 'lib/vizcore/dsl/reaction_builder.rb', line 39

def trigger(target, **options)
  change(target, **options)
end