mapped.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. module Sequencer::Unit::Import::Zendesk::SubSequence::Mapped
  3. module ClassMethods
  4. def resource_map
  5. :"#{resource_klass.underscore}_map"
  6. end
  7. def inherited(base)
  8. super
  9. base.provides(base.resource_map)
  10. base.extend(Forwardable)
  11. base.instance_delegate [:resource_map] => base
  12. end
  13. end
  14. def self.included(base)
  15. base.uses :client
  16. base.extend(ClassMethods)
  17. end
  18. def process
  19. state.provide(resource_map) do
  20. process_sub_sequence
  21. mapping
  22. end
  23. end
  24. private
  25. def expecting
  26. raise 'Missing implementation of expecting method'
  27. end
  28. def collection_provider
  29. client
  30. end
  31. def process_sub_sequence
  32. resource_iteration do |resource|
  33. expected_value = expected(resource)
  34. next if expected_value.blank?
  35. mapping[resource.id] = mapping_value(expected_value)
  36. end
  37. end
  38. def expected(resource)
  39. result = sub_sequence(resource)
  40. result[expecting]
  41. end
  42. def sub_sequence(resource)
  43. ::Sequencer.process(sequence_name,
  44. parameters: default_params.merge(
  45. resource: resource
  46. ),
  47. expecting: [expecting])
  48. end
  49. def mapping_value(expected_value)
  50. expected_value
  51. end
  52. def mapping
  53. @mapping ||= {}
  54. end
  55. end