Class: PolyglotIos::Serializer::Language::ObjC

Inherits:
Base
  • Object
show all
Defined in:
lib/ios_polyglot_cli/serializers/languages/languages_serializer_objc.rb

Instance Attribute Summary

Attributes inherited from Base

#languages

Instance Method Summary collapse

Methods inherited from Base

#initialize, #template

Constructor Details

This class inherits a constructor from PolyglotIos::Serializer::Language::Base

Instance Method Details

#h_templateObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ios_polyglot_cli/serializers/languages/languages_serializer_objc.rb', line 21

def h_template()
<<-H_TEMPLATE
#import <Foundation/Foundation.h>

@interface Language : NSObject

@property (nonatomic, strong, readonly) NSString *name;
@property (nonatomic, strong, readonly) NSString *localName;
@property (nonatomic, strong, readonly) NSString *locale;
@property (nonatomic, strong, readonly) NSString *languageCode;

@property (readonly, class) NSArray<Language *> *allLanguages;
<% @languages.each do |language| -%>
@property (readonly, class) Language *<%= language.clean_name %>;
<% end -%>

@end
H_TEMPLATE
end

#m_templateObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ios_polyglot_cli/serializers/languages/languages_serializer_objc.rb', line 41

def m_template()
<<-M_TEMPLATE
#import "Language.h"

@interface Language()

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *localName;
@property (nonatomic, strong) NSString *locale;
@property (nonatomic, strong) NSString *languageCode;

@end

@implementation Language

- (instancetype)initWithName:(NSString *)name localName:(NSString *)localName locale:(NSString *)locale languageCode:(NSString *)languageCode
{
    if (self = [super init]) {
_name = name;
_localName = localName;
_locale = locale;
_languageCode = languageCode;
    }
    return self;
}

<% @languages.each do |language| -%>
+ (Language *)<%= language.clean_name %>
{
    return [[Language alloc] initWithName:@"<%= language.name %>" localName:@"<%= language.local_name %>" locale:@"<%= language.locale %>" languageCode:@"<%= language.code %>"];
}

<% end -%>
+ (NSArray<Language *> *)allLanguages 
{
    return @[
<% @languages.each_with_index do |language, i| -%>
Language.<%= language.clean_name %><%= i == (@languages.size - 1) ? "" : "," %>
<% end -%>
    ];
}

@end
M_TEMPLATE
end

#renderObject



8
9
10
11
12
# File 'lib/ios_polyglot_cli/serializers/languages/languages_serializer_objc.rb', line 8

def render()
  h_file = ERB.new(h_template, trim_mode: '-').result(binding)
  m_file = ERB.new(m_template, trim_mode: '-').result(binding)
  return h_file, m_file
end

#save(sources_path) ⇒ Object



14
15
16
17
18
19
# File 'lib/ios_polyglot_cli/serializers/languages/languages_serializer_objc.rb', line 14

def save(sources_path)
  FileUtils.mkdir_p sources_path unless File.exist? sources_path
  h_file, m_file = render()
  File.write(File.join(sources_path, "Language.h"), h_file)
  File.write(File.join(sources_path, "Language.m"), m_file)
end