unit.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Sequencer::Unit
  3. extend ::Sequencer::Mixin::PrefixedConstantize
  4. PREFIX = 'Sequencer::Unit::'.freeze
  5. # Convenience wrapper for processing a single Unit.
  6. #
  7. # ATTENTION: This should only be used for development, testing or debugging purposes.
  8. # There might be a check in the future to prevent using this method in other scopes.
  9. #
  10. # @see #initialize
  11. # @see #process
  12. def self.process(unit, parameters, &)
  13. new(unit).process(parameters, &)
  14. end
  15. # Initializes a new Sequencer::Unit for processing it.
  16. #
  17. # ATTENTION: This should only be used for development, testing or debugging purposes.
  18. # There might be a check in the future to prevent using this method in other scopes.
  19. #
  20. # @param [String] unit the name String for the Unit that should get processed
  21. def initialize(unit)
  22. @unit = self.class.constantize(unit)
  23. end
  24. # Processes the Sequencer::Unit that the instance was initialized with.
  25. #
  26. # ATTENTION: This should only be used for development, testing or debugging purposes.
  27. # There might be a check in the future to prevent using this method in other scopes.
  28. #
  29. # @param [Hash{Symbol => Object}] parameters the parameters for initializing the Sequencer::State
  30. # @yield [instance] optional block to access the Unit instance
  31. # @yieldparam instance [Object] the Unit instance for e.g. adding expectations
  32. def process(parameters)
  33. @parameters = parameters
  34. instance = @unit.new(state)
  35. # yield instance to apply expectations
  36. yield instance if block_given?
  37. state.process do
  38. instance.process
  39. end
  40. state.to_h
  41. end
  42. private
  43. def state
  44. @state ||= begin
  45. units = Sequencer::Units.new(
  46. @unit.name
  47. )
  48. sequence = Sequencer::Sequence.new(
  49. units: units,
  50. expecting: @unit.provides,
  51. )
  52. Sequencer::State.new(sequence,
  53. parameters: @parameters)
  54. end
  55. end
  56. end