Module: Crossbeam::ClassMethods

Defined in:
lib/crossbeam.rb

Overview

Methods to load in the service object as class methods

Instance Method Summary collapse

Instance Method Details

#call(*params, **options, &block) ⇒ Struct, Object

Used to initiate and service call



43
44
45
46
47
48
49
50
# File 'lib/crossbeam.rb', line 43

def call(*params, **options, &block)
  @result = Crossbeam::Result.new(called: true)
  @klass = new(*params, **options)
  run_callbacks_and_klass(&block)
  @result
rescue Crossbeam::Failure => e
  process_error(e)
end

#process_error(error) ⇒ Object, Crossbeam::Result

Process the error that was thrown by the object call



72
73
74
75
76
77
# File 'lib/crossbeam.rb', line 72

def process_error(error)
  @result.failure = true
  @result.errors.add(:failure, error.to_s)
  @result.results = nil
  @result
end

#run_callbacks_and_klass(&block) ⇒ Void

Call the klass’s before/after callbacks, process errors, and call @klass



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/crossbeam.rb', line 55

def run_callbacks_and_klass(&block)
  # Call all the classes `before`` callbacks
  run_before_callbacks
  # Initialize and run the classes call method
  @result.results = @klass.call(&block)
  # build @result.errors if errors were added
  build_error_list if @klass.errors.any?
  # re-assign @result if output was set
  set_results_output
  # Call all the classes `after` callbacks
  run_after_callbacks
end