Class: Evva::SwiftGenerator
- Inherits:
-
Object
- Object
- Evva::SwiftGenerator
- Defined in:
- lib/evva/swift_generator.rb
Constant Summary collapse
- SWIFT_EVENT_HEADER =
"import CoreLocation\n"\ "import Foundation\n"\ "import SharedCode\n\n"\ "class MixpanelHelper: NSObject {\n"\ "enum Event {\n".freeze
- SWIFT_EVENT_DATA_HEADER =
"private var data: EventData {\n"\ "switch self {\n\n\n".freeze
- SWIFT_PEOPLE_HEADER =
"fileprivate enum Counter: String {\n".freeze
- SWIFT_INCREMENT_FUNCTION =
"func increment(times: Int = 1) {\n"\ "MixpanelAPI.instance.incrementCounter(rawValue, times: times)\n"\ '}'.freeze
Instance Method Summary collapse
- #event_enum(enum) ⇒ Object
- #events(bundle) ⇒ Object
- #people_properties(people_bundle) ⇒ Object
- #process_arguments(props) ⇒ Object
- #special_property_enum(enum) ⇒ Object
- #swift_case(event_data) ⇒ Object
- #swift_event_data(event_data) ⇒ Object
Instance Method Details
#event_enum(enum) ⇒ Object
60 61 62 |
# File 'lib/evva/swift_generator.rb', line 60 def event_enum(enum) # empty end |
#events(bundle) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/evva/swift_generator.rb', line 21 def events(bundle) event_file = SWIFT_EVENT_HEADER bundle.each do |event| event_file += swift_case(event) end event_file += "}\n" event_file += "private var data: EventData {\n"\ "switch self {\n\n" bundle.each do |event| event_file += swift_event_data(event) end event_file += "}\n}\n" end |
#people_properties(people_bundle) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/evva/swift_generator.rb', line 64 def people_properties(people_bundle) properties = SWIFT_PEOPLE_HEADER people_bundle.each do |prop| properties += swift_people_const(prop) end properties += SWIFT_INCREMENT_FUNCTION + "\n}" end |
#process_arguments(props) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/evva/swift_generator.rb', line 82 def process_arguments(props) arguments = '' props.split(',').each do |property| if is_special_property(property) if is_optional_property(property) else arguments += %("#{property.split(':').first}") + ':' + property.split(':').first + '.rawValue, ' end else arguments += %("#{property.split(':').first}") + ':' + property.split(':').first + ', ' end end arguments.chomp(', ') end |
#special_property_enum(enum) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/evva/swift_generator.rb', line 72 def special_property_enum(enum) enum_body = "import Foundation\n\n" enum_values = enum.values.split(',') enum_body += "enum #{enum.enum_name}: String {\n" enum_values.each do |vals| enum_body += "\tcase #{vals.tr(' ', '_')} = " + %("#{vals}") + "\n" end enum_body += "} \n" end |
#swift_case(event_data) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/evva/swift_generator.rb', line 35 def swift_case(event_data) function_name = 'track' + titleize(event_data.event_name) if event_data.properties.nil? case_body = "\t\tcase #{function_name}\n" else trimmed_properties = event_data.properties.gsub('Boolean', 'Bool') case_body = "\t\tcase #{function_name}(#{trimmed_properties})\n" end end |
#swift_event_data(event_data) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/evva/swift_generator.rb', line 45 def swift_event_data(event_data) function_name = 'track' + titleize(event_data.event_name) if event_data.properties.nil? function_body = "case .#{function_name} \n" \ "\treturn EventData(name:" + %("#{event_data.event_name}") + ")\n\n" else function_header = prepend_let(event_data.properties) function_arguments = process_arguments(event_data.properties.gsub('Boolean', 'Bool')) function_body = "case .#{function_name}(#{function_header}):\n" \ "\treturn EventData(name:" + %("#{event_data.event_name}") + ", properties: [#{function_arguments}])\n\n" end function_body end |