base.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Sequencer::Unit::Import::Common::SubSequence::Mixin::Base
  3. private
  4. def sequence
  5. raise "Missing implementation of '#{__method__}' method for '#{self.class.name}'"
  6. end
  7. def sequence_name
  8. @sequence_name ||= sequence
  9. end
  10. def default_params
  11. {}
  12. end
  13. def default_parameter
  14. @default_parameter ||= default_params
  15. end
  16. def sequence_resource(resource = nil, &)
  17. parameters = sanitized_sequence_parameters!(resource, &)
  18. return if parameters.blank?
  19. ::Sequencer.process(sequence_name,
  20. parameters: parameters)
  21. end
  22. def sanitized_sequence_parameters!(resource, &)
  23. parameters = sequence_parameters(resource, &)
  24. if parameters.nil?
  25. logger.debug { "Skipping processing of Sub-Sequence '#{sequence_name}'. `sequence_resource` block returned `nil` in '#{self.class.name}'." }
  26. return
  27. end
  28. if parameters[:resource].blank?
  29. raise '`resource` parameter missing. It is required as an argument to `sequence_resource` or as `:resource` key value of the block result.'
  30. end
  31. parameters.tap do |result|
  32. result[:resource] = result[:resource].to_h.with_indifferent_access
  33. end
  34. end
  35. def sequence_parameters(resource)
  36. # creates a dup/copy of `default_parameter`
  37. parameters = default_parameter.merge(resource: resource)
  38. return parameters if !block_given?
  39. yield(parameters)
  40. end
  41. def sequence_resources(resources, &)
  42. resources.each do |resource|
  43. sequence_resource(resource, &)
  44. end
  45. end
  46. end