unit.rb 2.1 KB

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