sanitized_name.rb 1.5 KB

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