require_relative 'translation_case' module PolyglotIos module Serializer module Source class TranslationEnum include Helper::General attr_accessor :name attr_accessor :translations attr_accessor :child_enums def initialize(name) @name = name @translations = [] @child_enums = [] end def insert(components, key_name) if components.count > 1 insert_to_nested_enum(components, key_name) elsif components.count == 1 translation_case = TranslationCase.new(components.first, key_name) translations.push(translation_case) end end def serialized(indent_level = 0) output = indent(indent_level) clean_name = clean_enum_name(name) if indent_level == 0 # the root object should be an extension and not be public to avoid warnings output.concat("extension #{clean_name} {\n") else output.concat("public enum #{clean_name} {\n") end translations .sort_by { |translation| translation.name.downcase } .each do |translation| output .concat(indent(indent_level + 1, translation.serialized())) .concat("\n") end if translations.count > 0 && child_enums.count > 0 output.concat("\n") end serialized_enums = child_enums .sort_by { |child_enum| child_enum.name.downcase } .map { |child_enum| child_enum.serialized(indent_level + 1) } .join("\n") output.concat(serialized_enums) output.concat(indent(indent_level, "}\n")) return output end private def insert_to_nested_enum(components, key_name) _components = components.clone enum_name = _components.shift nested_enum = nested_enum_with_name(enum_name) if !nested_enum.nil? nested_enum.insert(_components, key_name) else child = TranslationEnum.new(enum_name) child.insert(_components, key_name) @child_enums.push(child) end end def nested_enum_with_name(name) child_enums.find { |enum| enum.name == name } end end end end end