Module: BusinessFlow::DSL::ClassMethods

Defined in:
lib/business_flow/dsl.rb

Overview

Contains the DSL for BusinessFlow

Instance Method Summary collapse

Instance Method Details

#build(parameter_object) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/business_flow/dsl.rb', line 60

def build(parameter_object)
  finalize_initializer
  allocate.tap do |flow|
    catch(:halt_step) do
      flow.send(:_business_flow_dsl_initialize,
                ParameterObject.new(parameter_object))
    end
  end
end

#call(parameter_object = {}) ⇒ Object



49
50
51
# File 'lib/business_flow/dsl.rb', line 49

def call(parameter_object = {})
  execute(build(parameter_object))
end

#call!(*args) ⇒ Object



70
71
72
73
74
# File 'lib/business_flow/dsl.rb', line 70

def call!(*args)
  flow = call(*args)
  raise FlowFailedException, flow if flow.errors?
  flow
end

#execute(flow) ⇒ Object

:reek:UtilityFunction This is a function on us so that other modules can change execution behavior.



55
56
57
58
# File 'lib/business_flow/dsl.rb', line 55

def execute(flow)
  catch(:halt_step) { flow.call }
  result_from(flow)
end

#finalize_initializerObject



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/business_flow/dsl.rb', line 114

def finalize_initializer
  return if @finalized_initializer
  class_eval %{
    private def _business_flow_dsl_initialize(parameter_object)
      @parameter_object = parameter_object
      #{needs_code}
      initialize
    end
  }, __FILE__, __LINE__ - 6
  @finalized_initializer = true
end

#finalize_result_providerObject



98
99
100
101
102
# File 'lib/business_flow/dsl.rb', line 98

def finalize_result_provider
  return if @finalized_result_provider || !@result_copy
  const_get(:Result).class_eval "#{@result_copy}\nend", __FILE__, __LINE__
  @finalized_result_provider = true
end

#needs(*fields) ⇒ Object

Requires that a field be retrievable from the initialization parameters

This will only require that the field is not nil. The field may still be #empty?

Parameters:

  • fields

    The fields required from the initialization parameters



13
14
15
16
# File 'lib/business_flow/dsl.rb', line 13

def needs(*fields)
  @needs ||= FieldList.new([], ParameterField, self)
  @needs.add_fields(fields)
end

#needs_codeObject



104
105
106
107
108
109
110
111
112
# File 'lib/business_flow/dsl.rb', line 104

def needs_code
  needs.map do |need|
    %(if #{need}.nil?
        errors[:#{need}] << 'must not be nil'
        throw :halt_step
      end
    )
  end.join("\n")
end

#provides(*fields) ⇒ Object

Declares that you will expose a field to the outside world.



26
27
28
29
30
31
32
# File 'lib/business_flow/dsl.rb', line 26

def provides(*fields)
  @provides ||= FieldList.new([], PublicField, [self, const_get(:Result)])
  @result_copy ||= 'def from_flow(flow)'
  @result_copy += fields.map { |field| "\n@#{field} = flow.#{field}" }
                        .join
  @provides.add_fields(fields)
end

#result_from(flow) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/business_flow/dsl.rb', line 88

def result_from(flow)
  finalize_result_provider
  # We use instance_variable_get here instead of making it part of
  # from_flow to ensure that we do not create the errors object unless
  # we need it.
  result = const_get(:Result).new(flow.instance_variable_get(:@errors))
  result.from_flow(flow) if @result_copy
  result
end

#step(klass, opts = {}) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/business_flow/dsl.rb', line 40

def step(klass, opts = {})
  step = Step.new(Callable.new(klass), opts)
  step_queue.push(step)
  step.outputs
      .values
      .select { |field| field.is_a?(Symbol) }
      .each { |field| Field.new(field).add_to(self) }
end

#step_executor(executor_class = nil) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/business_flow/dsl.rb', line 80

def step_executor(executor_class = nil)
  if executor_class
    @executor_class = executor_class
  else
    @executor_class ||= ::BusinessFlow::DefaultStepExecutor
  end
end

#step_queueObject



76
77
78
# File 'lib/business_flow/dsl.rb', line 76

def step_queue
  @step_queue ||= []
end

#uses(field, klass, opts = {}) ⇒ Object



34
35
36
37
38
# File 'lib/business_flow/dsl.rb', line 34

def uses(field, klass, opts = {})
  step = Step.new(Callable.new(klass), opts)
  retriever = proc { step.call(self).output }
  UsesField.new(field, retriever).add_to(self)
end

#wants(field, default = proc { nil }, opts = {}) ⇒ Object

Allows a field to be retrieved from the initialiaztion parameters



19
20
21
22
23
# File 'lib/business_flow/dsl.rb', line 19

def wants(field, default = proc { nil }, opts = {})
  internal_name = "wants_#{field}".to_sym
  uses(internal_name, default, opts)
  ParameterField.new(field, internal_name).add_to(self)
end