Module: BusinessFlow::DSL::ClassMethods
- Defined in:
- lib/business_flow/dsl.rb
Overview
Contains the DSL for BusinessFlow
Instance Method Summary collapse
- #build(parameter_object) ⇒ Object
- #call(parameter_object = {}) ⇒ Object
- #call!(*args) ⇒ Object
-
#execute(flow) ⇒ Object
:reek:UtilityFunction This is a function on us so that other modules can change execution behavior.
- #finalize_initializer ⇒ Object
- #finalize_result_provider ⇒ Object
- #instrument(_name, _flow) {|nil| ... } ⇒ Object
-
#needs(*fields) ⇒ Object
Requires that a field be retrievable from the initialization parameters.
- #needs_code ⇒ Object
-
#optional(*fields) ⇒ Object
Allows a field to be retieved form the initialization paramters, but does not require it to be non-nil.
-
#provides(*fields) ⇒ Object
Declares that you will expose a field to the outside world.
- #result_from(flow) ⇒ Object
- #step(klass, opts = {}) ⇒ Object
- #step_executor(executor_class = nil) ⇒ Object
- #step_queue ⇒ Object
- #uses(field, klass, opts = {}) ⇒ Object
-
#wants(field, default = proc { nil }, opts = {}) ⇒ Object
Allows a field to be retrieved from the initialiaztion parameters.
Instance Method Details
#build(parameter_object) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/business_flow/dsl.rb', line 67 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
56 57 58 |
# File 'lib/business_flow/dsl.rb', line 56 def call(parameter_object = {}) execute(build(parameter_object)) end |
#call!(*args) ⇒ Object
77 78 79 80 81 |
# File 'lib/business_flow/dsl.rb', line 77 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.
62 63 64 65 |
# File 'lib/business_flow/dsl.rb', line 62 def execute(flow) catch(:halt_step) { flow.call } unless flow.errors? result_from(flow) end |
#finalize_initializer ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/business_flow/dsl.rb', line 125 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_provider ⇒ Object
109 110 111 112 113 |
# File 'lib/business_flow/dsl.rb', line 109 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 |
#instrument(_name, _flow) {|nil| ... } ⇒ Object
95 96 97 |
# File 'lib/business_flow/dsl.rb', line 95 def instrument(_name, _flow) yield nil 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?
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_code ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/business_flow/dsl.rb', line 115 def needs_code needs.map do |need| %(if #{need}.nil? errors[:#{need}] << 'must not be nil' throw :halt_step end ) end.join("\n") end |
#optional(*fields) ⇒ Object
Allows a field to be retieved form the initialization paramters, but does not require it to be non-nil
20 21 22 23 |
# File 'lib/business_flow/dsl.rb', line 20 def optional(*fields) @optionals ||= FieldList.new([], ParameterField, self) @optionals.add_fields(fields) end |
#provides(*fields) ⇒ Object
Declares that you will expose a field to the outside world.
33 34 35 36 37 38 39 |
# File 'lib/business_flow/dsl.rb', line 33 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
99 100 101 102 103 104 105 106 107 |
# File 'lib/business_flow/dsl.rb', line 99 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
47 48 49 50 51 52 53 54 |
# File 'lib/business_flow/dsl.rb', line 47 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
87 88 89 90 91 92 93 |
# File 'lib/business_flow/dsl.rb', line 87 def step_executor(executor_class = nil) if executor_class @executor_class = executor_class else @executor_class ||= ::BusinessFlow::DefaultStepExecutor end end |
#step_queue ⇒ Object
83 84 85 |
# File 'lib/business_flow/dsl.rb', line 83 def step_queue @step_queue ||= [] end |
#uses(field, klass, opts = {}) ⇒ Object
41 42 43 44 45 |
# File 'lib/business_flow/dsl.rb', line 41 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
26 27 28 29 30 |
# File 'lib/business_flow/dsl.rb', line 26 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 |