Module: BusinessFlow::DSL

Defined in:
lib/business_flow/dsl.rb

Overview

Core DSL for BusinessFlow. The relevant methods are all in ClassMethods.

Defined Under Namespace

Modules: ClassMethods, ErrorSupport Classes: Field, FieldList, MemoizedField, ParameterField, PublicField, UsesField

Constant Summary collapse

FROM_FLOW_PREAMBLE =
%(
  def from_flow(flow)
    return if errors?
)
RESULT_DEF =
%(
  class Result
    attr_reader :exception

    def initialize(errors, exception)
      @errors = errors
      @exception = exception
    end

    def errors
      @errors ||= ActiveModel::Errors.new(self)
    end

    def errors?
      # We're explicitly using the instance variable here so that if no
      # errors have been created, we don't initialize the error object.
      (!!@errors && @errors.present?) || !exception.nil?
    end

    def valid?(_context = nil)
      # We're explicitly using the instance variable here so that if no
      # errors have been created, we don't initialize the error object.
      @errors.blank?
    end

    def invalid?(context = nil)
      !valid?(context)
    end
  end
)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#failure_exception=(value) ⇒ Object

Sets the attribute failure_exception

Parameters:

  • value

    the value to set the attribute failure_exception to.



270
271
272
# File 'lib/business_flow/dsl.rb', line 270

def failure_exception=(value)
  @failure_exception = value
end

Class Method Details

.included(klass) ⇒ Object

:reek:ManualDispatch I have no need to actually call human_attribute_name, :reek:TooManyStatements Breaking this up would add complexity. I just need to know if I have to provide my own.



257
258
259
260
261
262
263
264
265
# File 'lib/business_flow/dsl.rb', line 257

def self.included(klass)
  klass.extend(ClassMethods)
  klass.class_eval RESULT_DEF, __FILE__, __LINE__
  klass.extend(ErrorSupport) unless klass.respond_to?(:human_attribute_name)
  klass.extend(ActiveModel::Naming)
  return if klass.respond_to?(:read_attribute_for_validation)

  klass.include(ErrorSupport::InstanceMethods)
end

Instance Method Details

#callObject



273
274
275
276
277
278
# File 'lib/business_flow/dsl.rb', line 273

def call
  return if invalid?

  klass = self.class
  klass.step_executor.new(klass.step_queue, self).call
end

#errorsObject



280
281
282
# File 'lib/business_flow/dsl.rb', line 280

def errors
  @errors ||= ActiveModel::Errors.new(self)
end

#errors?Boolean

Returns:

  • (Boolean)


284
285
286
287
288
# File 'lib/business_flow/dsl.rb', line 284

def errors?
  # We're explicitly using the instance variable here so that if no
  # errors have been created, we don't initialize the error object.
  @errors&.present?
end

#invalid?(context = nil) ⇒ Boolean

Returns:

  • (Boolean)


296
297
298
# File 'lib/business_flow/dsl.rb', line 296

def invalid?(context = nil)
  !valid?(context)
end

#valid?(_context = nil) ⇒ Boolean

Returns:

  • (Boolean)


290
291
292
293
294
# File 'lib/business_flow/dsl.rb', line 290

def valid?(_context = nil)
  # We're explicitly using the instance variable here so that if no
  # errors have been created, we don't initialize the error object.
  @errors.blank?
end