Class: Redbreast::Serializer::Swift

Inherits:
Base
  • Object
show all
Includes:
Helper::General
Defined in:
lib/redbreast/serializers/swift_serializer.rb

Overview

Used to save swift files

Constant Summary collapse

SPACER =
"    "

Constants included from Helper::General

Helper::General::ESCAPE_KEYWORDS

Instance Attribute Summary

Attributes inherited from Base

#asset_names, #bundle

Instance Method Summary collapse

Methods included from Helper::General

#app_name, #bundles, #clean_case_name, #clean_enum_name, #clean_variable_name, #config, #escape_keyword_if_needed, #escape_with_underscore_if_needed, #indent, #programming_language, #upper_camel_case

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Redbreast::Serializer::Base

Instance Method Details

#create_swift_test_cases(names:, declaration:, app_name:) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/redbreast/serializers/swift_serializer.rb', line 61

def create_swift_test_cases(names:, declaration:, app_name:)
  text = ''
  app_name_text = app_name.nil? || app_name.empty? ? '' : app_name + '.'

  names.each do |name|
    temp_array = name.split('/')
    variable = temp_array.pop
    additional_text = temp_array.count.zero? ? '' : '.'

    text += SPACER + SPACER + declaration + app_name_text + temp_array.map { |enum| upper_camel_case(enum) }.join('.') + additional_text + clean_variable_name(variable)
    text += name == names.last ? '' : "\n"
  end

  text
end

#generate_extension(extended_class, app_name) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/redbreast/serializers/swift_serializer.rb', line 52

def generate_extension(extended_class, app_name)
  text = 'extension ' + extended_class + " {"
  return text if app_name.nil? || app_name.empty?

  text = 'public extension ' + extended_class + " {\n"
  text + SPACER + "enum " + app_name + " {}\n}\n\nextension " + extended_class + '.' + app_name + " {"

end

#generate_file_swift(names:, spacing: SPACER, indentation: '', variable:, bundle:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/redbreast/serializers/swift_serializer.rb', line 18

def generate_file_swift(names:, spacing: SPACER, indentation: '', variable:, bundle:)
  return if names.empty?

  text = ''
  arr = []

  vars, arr = generate_variables(names: names, spacing: spacing, indentation: indentation, variable: variable, bundle: bundle, text: text, array: arr)

  arr = arr.uniq.sort_by(&:downcase)

  arr.each do |enum_name|
    names_new = []
    names_new_enum = []
    new_enum_name = enum_name

    text += "\n" + spacing + 'public enum ' + upper_camel_case(enum_name) + ' {'
    names_new, names_new_enum = separate_variables_from_folders(names: names, enum_name: enum_name, new_enum_name: new_enum_name, names_new_enum: names_new_enum, names_new: names_new)

    if !names_new_enum.empty? && new_enum_name == enum_name
      indentation += indentation.empty? || indentation[-1] == '/' ? '' : '/'
      text += "\n" + generate_file_swift(names: names_new_enum, spacing: spacing + SPACER, indentation: indentation + enum_name, variable: variable, bundle: bundle)
    end

    unless names_new.empty?

      indentation += indentation.empty? || indentation[-1] == '/' ? '' : '/'
      text += generate_file_swift(names: names_new, spacing: spacing + SPACER, indentation: indentation + enum_name, variable: variable, bundle: bundle)
    end

    text += "\n" + spacing + '}' + "\n"
  end
  text + vars
end

#generate_variables(names:, spacing:, indentation:, bundle:, variable:, text:, array:) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/redbreast/serializers/swift_serializer.rb', line 77

def generate_variables(names:, spacing:, indentation:, bundle:, variable:, text:, array:)
  names.sort_by(&:downcase).each do |name|
    temp_arr = name.split('/')

    if temp_arr.length != 1
      array.push(temp_arr.first)
    else
      name_prefix = indentation.empty? ? '' : '/'
      text += "\n" + spacing + variable % [clean_variable_name(name), indentation + name_prefix + name, bundle[:reference]]
    end
  end

  [text, array]
end

#save(output_source_path:, template_generator:, generate_colors:) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/redbreast/serializers/swift_serializer.rb', line 10

def save(output_source_path:, template_generator:, generate_colors:)
  directory = File.dirname(output_source_path)
  FileUtils.mkdir_p directory unless File.exist? directory

  file = ERB.new(template_generator.template, trim_mode: '-').result(binding)
  File.write(output_source_path, file)
end

#separate_variables_from_folders(names:, enum_name:, new_enum_name:, names_new_enum:, names_new:) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/redbreast/serializers/swift_serializer.rb', line 92

def separate_variables_from_folders(names:, enum_name:, new_enum_name:, names_new_enum:, names_new:)
  names.each do |name|
    temp_arr = name.split('/')

    next if temp_arr.length == 1

    if temp_arr.length > 2
      names_new_enum.push(temp_arr.drop(1).join('/')) if temp_arr.first == new_enum_name
      next
    end

    names_new.push(temp_arr.drop(1).join('/')) if temp_arr[0] == enum_name
  end

  [names_new, names_new_enum]
end