units.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Sequencer::Units < SimpleDelegator
  3. include ::Enumerable
  4. # Initializes a new Sequencer::Units instance with the given Units Array.
  5. #
  6. # @param [*Array<String>] *units a list of Units with or without the Sequencer::Unit prefix.
  7. #
  8. # @example
  9. # Sequencer::Units.new('Example::Unit', 'Sequencer::Unit::Second::Unit')
  10. def initialize(*units)
  11. super(units)
  12. end
  13. # Required #each implementation for ::Enumerable functionality. Constantizes
  14. # the list of units given when initialized.
  15. #
  16. # @example
  17. # units.each do |unit|
  18. # unit.process(sequencer)
  19. # end
  20. #
  21. # @return [nil]
  22. def each
  23. __getobj__.each do |unit|
  24. yield constantize(unit)
  25. end
  26. end
  27. # Provides an Array of :uses, :provides and :optional declarations for each Unit.
  28. #
  29. # @example
  30. # units.declarations
  31. # #=> [{uses: [:question], provides: [:answer], optional: [:facts], ...}]
  32. #
  33. # @return [Array<Hash{Symbol => Array<Symbol>}>] the declarations of the Units
  34. def declarations
  35. collect do |unit|
  36. {
  37. uses: unit.uses,
  38. provides: unit.provides,
  39. optional: unit.optional,
  40. }
  41. end
  42. end
  43. # Enables the access to an Unit class via index.
  44. #
  45. # @param [Integer] index the index for the requested Unit class.
  46. #
  47. # @example
  48. # units[1]
  49. # #=> Sequencer::Unit::Example
  50. #
  51. # @return [Object] the Unit class for the requested index
  52. def [](index)
  53. constantize(__getobj__[index])
  54. end
  55. private
  56. def constantize(unit)
  57. Sequencer::Unit.constantize(unit)
  58. end
  59. end