require_relative '../base' module Analytics module Serializer class ObjC < Base class UserProperty def initialize(src) @src = src end def save(path) @class_name = 'AnalyticsUserProperty' @file_name = @class_name + '.h' output_path = File.join(path, @file_name) File.write(output_path, render_h) @file_name = @class_name + '.m' output_path = File.join(path, @file_name) File.write(output_path, render_m) end private def render_h ERB.new(template_h, nil, '-').result(binding) end def template_h <<~TEMPLATE <%= ERB.new(Analytics::Templates.tmpl_at('header.erb')).result(binding) %> #import #import "AnalyticsEnums.h" NS_ASSUME_NONNULL_BEGIN __swift_unavailable("This class is only available in ObjC.") @interface <%= @class_name %> : NSObject @property (nonatomic, strong, readonly) NSString *name; @property (nonatomic, strong, readonly, nullable) NSString *value; - (instancetype)init NS_UNAVAILABLE; + (instancetype)new NS_UNAVAILABLE; #pragma mark - User properties <% @src["userProperties"].each do |user_property| %> /// <%= user_property['description'] %> <% if user_property['values'].nil? -%> /// @param value The value of the user property, or `nil` if You want to remove the user property. <% else -%> /// @param value The value of the user property, or use `<%= 'AUP' + user_property['name'].pascal_case + 'Nil' %>` if You want to remove the user property. <% end -%> + (instancetype)<%= user_property['name'].camel_case %>WithValue:(<%= format_method_parameter(user_property) %>)value; <% end %> @end NS_ASSUME_NONNULL_END TEMPLATE end def render_m ERB.new(template_m, nil, '-').result(binding) end def template_m <<~TEMPLATE <%= ERB.new(Analytics::Templates.tmpl_at('header.erb')).result(binding) %> #import "<%= @class_name + '.h' %>" @implementation <%= @class_name %> - (instancetype)initWithName:(NSString *)name value:(NSString * _Nullable)value { if (self = [super init]) { _name = name; _value = value; } return self; } #pragma mark - User properties <% @src["userProperties"].each do |user_property| %> + (instancetype)<%= user_property['name'].camel_case %>WithValue:(<%= format_method_parameter(user_property) %>)value { <% unless user_property['values'].nil? -%> NSString *rawValue; switch (value) { <% user_property['values'].each do |value| -%> case <%= 'AUP' + user_property['name'].pascal_case + value.pascal_case %>: rawValue = @"<%= value %>"; break; <% end -%> case <%= 'AUP' + user_property['name'].pascal_case + 'Nil' %>: rawValue = nil; break; } return [[self alloc] initWithName:@"<%= user_property['name'] %>" value:rawValue]; <% else -%> return [[self alloc] initWithName:@"<%= user_property['name'] %>" value:value]; <% end -%> } <% end %> @end TEMPLATE end def format_method_parameter(user_property) if user_property['values'].nil? "NSString * _Nullable" else 'AUP' + user_property['name'].pascal_case end end end end end end