sanitized_name.rb 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 ObjectAttribute
  7. class SanitizedName < Sequencer::Unit::Common::Provider::Named
  8. prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action
  9. skip_action :skipped, :failed
  10. private
  11. def sanitized_name
  12. # model_no
  13. # model_nos
  14. # model_name
  15. # model_name
  16. # model_name
  17. # model_name_
  18. # model_name
  19. without_double_underscores.gsub(%r{_id(s?)$}, '_no\1')
  20. end
  21. def without_double_underscores
  22. # model_id
  23. # model_ids
  24. # model_name
  25. # model_name
  26. # model_name
  27. # model_name_
  28. # model_name
  29. only_supported_chars.gsub(%r{_{2,}}, '_')
  30. end
  31. def only_supported_chars
  32. # model_id
  33. # model_ids
  34. # model___name
  35. # model_name
  36. # model__name
  37. # model_name_
  38. # model_name
  39. downcased.chars.map { |char| char.match?(%r{[a-z0-9_]}) ? char : '_' }.join
  40. end
  41. def downcased
  42. # model id
  43. # model ids
  44. # model / name
  45. # model name
  46. # model::name
  47. # model name?
  48. # model name
  49. transliterated.downcase
  50. end
  51. def transliterated
  52. # Model ID
  53. # Model IDs
  54. # Model / Name
  55. # Model Name
  56. # Model::Name
  57. # Model Name?
  58. # Model Name
  59. ::ActiveSupport::Inflector.transliterate(unsanitized_name, '_'.freeze)
  60. end
  61. def unsanitized_name
  62. # Model ID
  63. # Model IDs
  64. # Model / Name
  65. # Model Name
  66. # Model::Name
  67. # Model Name?
  68. # Mödel Nâmé
  69. raise 'Missing implementation for unsanitized_name method'
  70. end
  71. end
  72. end
  73. end
  74. end
  75. end
  76. end