Class: Evva::SwiftGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/evva/swift_generator.rb

Constant Summary collapse

SWIFT_EVENT_HEADER =
"import CoreLocation\n"\
"import Foundation\n"\
"import SharedCode\n\n"\
"@objc class MixpanelHelper: NSObject {\n"\
"\tenum Event {\n".freeze
SWIFT_EVENT_DATA_HEADER =
"\t\tprivate var data: EventData {\n"\
"\t\t\tswitch self {\n".freeze
SWIFT_PEOPLE_HEADER =
"fileprivate enum Counter: String {\n".freeze
SWIFT_INCREMENT_FUNCTION =
"\tfunc increment(times: Int = 1) {\n"\
"\t\tMixpanelAPI.instance.incrementCounter(rawValue, times: times)\n"\
'\t}'.freeze
NATIVE_TYPES =
%w[Int String Double Float Bool].freeze

Instance Method Summary collapse

Instance Method Details

#event_enum(enum, file_name) ⇒ Object



60
61
62
# File 'lib/evva/swift_generator.rb', line 60

def event_enum(enum, file_name)
  # empty
end

#events(bundle, file_name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/evva/swift_generator.rb', line 23

def events(bundle, file_name)
  event_file = SWIFT_EVENT_HEADER
  bundle.each do |event|
    event_file += swift_case(event)
  end
  event_file += "\t}\n\n"
  event_file += SWIFT_EVENT_DATA_HEADER
  bundle.each do |event|
    event_file += swift_event_data(event)
  end
  event_file += "\t}\n}\n"
end

#people_properties(people_bundle, file_name) ⇒ Object



64
65
66
67
68
# File 'lib/evva/swift_generator.rb', line 64

def people_properties(people_bundle, file_name)
  properties = SWIFT_PEOPLE_HEADER
  properties += people_bundle.map { |prop| swift_people_const(prop) }.join('')
  properties + "\n" + SWIFT_INCREMENT_FUNCTION + "\n}\n"
end

#process_arguments(props) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/evva/swift_generator.rb', line 77

def process_arguments(props)
  arguments = ''
  props.each do |property|
    val = property.split(':').first
    if is_special_property?(property)
      if is_optional_property?(property)
        val = val.chomp('?')
        arguments += "\"#{val}\": #{val}.rawValue, "
      else
        arguments += "\"#{val}\": #{val}.rawValue, "
      end
    else
      if is_optional_property?(property)
        val = val.chomp('?')
        arguments += "\"#{val}\": #{val}, "
      else
        arguments += "\"#{val}\": #{val}, "
      end
    end
  end
  arguments.chomp(', ')
end

#special_property_enum(enum) ⇒ Object



70
71
72
73
74
75
# File 'lib/evva/swift_generator.rb', line 70

def special_property_enum(enum)
  enum_body = "import Foundation\n\n"
  enum_body += "enum #{enum.enum_name}: String {\n"
  enum_body += enum.values.map { |vals| "\tcase #{vals.tr(' ', '_')} = \"#{vals}\"\n" }.join('')
  enum_body + "} \n"
end

#swift_case(event_data) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/evva/swift_generator.rb', line 36

def swift_case(event_data)
  function_name = 'track' + titleize(event_data.event_name)
  if event_data.properties.empty?
    "\t\tcase #{function_name}\n"
  else
    trimmed_properties = event_data.properties.map { |k, v| k.to_s + ': ' + v.gsub('Boolean','Bool').gsub('Long', 'Int') }.join(", ")
    "\t\tcase #{function_name}(#{trimmed_properties})\n"
  end
end

#swift_event_data(event_data) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/evva/swift_generator.rb', line 46

def swift_event_data(event_data)
  function_name = 'track' + titleize(event_data.event_name)
  if event_data.properties.empty?
    function_body = "\t\t\tcase .#{function_name}:\n" \
                    "\t\t\t\treturn EventData(name: \"#{event_data.event_name}\")\n\n"
  else
    function_header = prepend_let(event_data.properties)
    function_arguments = process_arguments(event_data.properties.map { |k, v| "#{k}: #{v}" })
    function_body = "\t\t\tcase .#{function_name}(#{function_header}):\n" \
                    "\t\t\t\treturn EventData(name: \"#{event_data.event_name}\", properties: [#{function_arguments}])\n\n"
  end
  function_body
end