Class: RubyReactor::Dsl::MapBuilder

Inherits:
Object
  • Object
show all
Includes:
TemplateHelpers
Defined in:
lib/ruby_reactor/dsl/map_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TemplateHelpers

#Failure, #Success, #element, #input, #result, #value

Constructor Details

#initialize(name, mapped_reactor_class = nil, reactor = nil, &block) ⇒ MapBuilder

Returns a new instance of MapBuilder.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ruby_reactor/dsl/map_builder.rb', line 10

def initialize(name, mapped_reactor_class = nil, reactor = nil, &block)
  @name = name
  @mapped_reactor_class = mapped_reactor_class || (block ? Class.new(RubyReactor::Reactor) : nil)
  if @mapped_reactor_class && @mapped_reactor_class.name.nil? && reactor
    map_name_camel = name.to_s.split("_").map(&:capitalize).join
    parent_name = reactor.name # Assuming reactor has a name method or is a class with a name.

    full_name = "#{parent_name}::#{map_name_camel}"
    @mapped_reactor_class.define_singleton_method(:name) { full_name }
    RubyReactor::Registry.register(full_name, @mapped_reactor_class)
  end
  @reactor = reactor
  @argument_mappings = {}
  @async = false
  @strict_ordering = true
  @batch_size = nil
  @source_enumerable = nil
  @collect_block = nil
  @fail_fast = true # Default: stop on first error
end

Instance Attribute Details

#argument_mappingsObject

Returns the value of attribute argument_mappings.



8
9
10
# File 'lib/ruby_reactor/dsl/map_builder.rb', line 8

def argument_mappings
  @argument_mappings
end

#mapped_reactor_classObject

Returns the value of attribute mapped_reactor_class.



8
9
10
# File 'lib/ruby_reactor/dsl/map_builder.rb', line 8

def mapped_reactor_class
  @mapped_reactor_class
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/ruby_reactor/dsl/map_builder.rb', line 8

def name
  @name
end

#source_enumerableObject

Returns the value of attribute source_enumerable.



8
9
10
# File 'lib/ruby_reactor/dsl/map_builder.rb', line 8

def source_enumerable
  @source_enumerable
end

Instance Method Details

#argument(mapped_input_name, source) ⇒ Object



31
32
33
# File 'lib/ruby_reactor/dsl/map_builder.rb', line 31

def argument(mapped_input_name, source)
  @argument_mappings[mapped_input_name] = source
end

#async(async = true, batch_size: nil) ⇒ Object



43
44
45
46
# File 'lib/ruby_reactor/dsl/map_builder.rb', line 43

def async(async = true, batch_size: nil)
  @async = async
  @batch_size = batch_size if batch_size
end

#batch_size(size) ⇒ Object



52
53
54
# File 'lib/ruby_reactor/dsl/map_builder.rb', line 52

def batch_size(size)
  @batch_size = size
end

#buildObject



64
65
66
67
68
69
# File 'lib/ruby_reactor/dsl/map_builder.rb', line 64

def build
  dependencies = extract_dependencies_from_mappings
  dependencies << @source_enumerable.step_name if @source_enumerable.is_a?(RubyReactor::Template::Result)

  RubyReactor::Dsl::StepConfig.new(build_step_config(dependencies))
end

#collect(&block) ⇒ Object



56
57
58
# File 'lib/ruby_reactor/dsl/map_builder.rb', line 56

def collect(&block)
  @collect_block = block
end

#fail_fast(enabled = true) ⇒ Object



60
61
62
# File 'lib/ruby_reactor/dsl/map_builder.rb', line 60

def fail_fast(enabled = true)
  @fail_fast = enabled
end

#returns(step_name) ⇒ Object Also known as: return



77
78
79
80
# File 'lib/ruby_reactor/dsl/map_builder.rb', line 77

def returns(step_name)
  ensure_mapped_reactor_class!
  @mapped_reactor_class.returns(step_name)
end

#source(enumerable = nil, &block) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/ruby_reactor/dsl/map_builder.rb', line 35

def source(enumerable = nil, &block)
  @source_enumerable = if block
                         RubyReactor::Template::DynamicSource.new(@argument_mappings, &block)
                       else
                         enumerable
                       end
end

#step(name, &block) ⇒ Object

Delegate step definition methods to the mapped reactor class



72
73
74
75
# File 'lib/ruby_reactor/dsl/map_builder.rb', line 72

def step(name, &block)
  ensure_mapped_reactor_class!
  @mapped_reactor_class.step(name, &block)
end

#strict_ordering(enabled = true) ⇒ Object



48
49
50
# File 'lib/ruby_reactor/dsl/map_builder.rb', line 48

def strict_ordering(enabled = true)
  @strict_ordering = enabled
end