attributes.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Sequencer::Units < SimpleDelegator
  3. # Initializes the lifespan store for the attributes
  4. # of the given Units declarations.
  5. #
  6. # @param [Array<Hash{Symbol => Array<:Symbol>}>] declarations the list of Unit declarations.
  7. #
  8. # @example
  9. # declarations = [{uses: [:attribute1, ...], provides: [:result], ...}]
  10. # attributes = Sequencer::Units::Attributes(declarations)
  11. class Attributes < Delegator
  12. # Lists all `provides` declarations of the Units the instance was initialized with.
  13. #
  14. # @example
  15. # attributes.provided
  16. # # => [:result, ...]
  17. #
  18. # @return [Array<Symbol>] the list of all `provides` declarations
  19. def provided
  20. select do |_attribute, instance|
  21. instance.will_be_provided?
  22. end.keys
  23. end
  24. # Lists all `uses` declarations of the Units the instance was initialized with.
  25. #
  26. # @example
  27. # attributes.used
  28. # # => [:attribute1, ...]
  29. #
  30. # @return [Array<Symbol>] the list of all `uses` declarations
  31. def used
  32. select do |_attribute, instance|
  33. instance.will_be_used?
  34. end.keys
  35. end
  36. # Checks if the given attribute is known in the list of Unit declarations.
  37. #
  38. # @example
  39. # attributes.known?(:attribute2)
  40. # # => false
  41. #
  42. # @return [Boolean]
  43. def known?(attribute)
  44. key?(attribute)
  45. end
  46. def __getobj__
  47. @attributes
  48. end
  49. def __setobj__(declarations)
  50. @attributes ||= begin # rubocop:disable Naming/MemoizedInstanceVariableName
  51. attributes = Hash.new do |hash, key|
  52. hash[key] = Sequencer::Units::Attribute.new
  53. end
  54. attributes.tap do |result|
  55. declarations.each_with_index do |unit, index|
  56. unit[:uses].try(:each) do |attribute|
  57. result[attribute].to = index
  58. end
  59. unit[:provides].try(:each) do |attribute|
  60. next if result[attribute].will_be_provided?
  61. result[attribute].from = index
  62. end
  63. unit[:optional].try(:each) do |attribute|
  64. result[attribute].optional = index
  65. end
  66. end
  67. end
  68. end
  69. end
  70. end
  71. end