base.rb 2.2 KB

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