class PolyglotIos::Serializer::Source::TranslationEnum

Attributes

child_enums[RW]
name[RW]
translations[RW]

Public Class Methods

new(name) click to toggle source
# File lib/ios_polyglot_cli/serializers/sources/swift_helpers/translation_enum.rb, line 12
def initialize(name)
  @name = name
  @translations = []
  @child_enums = []
end

Public Instance Methods

insert(components, key_name) click to toggle source
# File lib/ios_polyglot_cli/serializers/sources/swift_helpers/translation_enum.rb, line 18
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
serialized(indent_level = 0) click to toggle source
# File lib/ios_polyglot_cli/serializers/sources/swift_helpers/translation_enum.rb, line 27
def serialized(indent_level = 0)
  output = indent(indent_level)
  clean_name = clean_enum_name(name)

  if translations.count == 0
    # enum without any case statement cannot be RawRepresentable
    output.concat("public enum #{clean_name} {\n")
  else
    output.concat("public enum #{clean_name}: String, StringsProtocol {\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 Instance Methods

insert_to_nested_enum(components, key_name) click to toggle source
# File lib/ios_polyglot_cli/serializers/sources/swift_helpers/translation_enum.rb, line 62
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
nested_enum_with_name(name) click to toggle source
# File lib/ios_polyglot_cli/serializers/sources/swift_helpers/translation_enum.rb, line 77
def nested_enum_with_name(name)
  child_enums.find { |enum| enum.name == name }
end