jaspion-kilza 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a005f85f95e01c815452123d02f78882c2e6963e
4
- data.tar.gz: d948698396da25947d4df8ce25d5f3a19fd90765
3
+ metadata.gz: f4bd19ba66c3c9b74c745750c4cb82ae424d9de9
4
+ data.tar.gz: e25b99e560d34b8abdba7777c89a576c4ab24e95
5
5
  SHA512:
6
- metadata.gz: 62e6310c809fc212626296a14e2dacc8b58e185e2049e2777642bfa4abe50faf5ff726fbf8089ce6dde4e9ae3b5019f09f1d662e3b715bc1e61d4f604f00dcbd
7
- data.tar.gz: 237145797b973ce882e5b7fa2699133c266ecb57f0ac3f878cf3beceb9862dc4b28b8f21e327fda0f46f64019184403c551fdc71217ccf4b4576e749c2ea14d4
6
+ metadata.gz: 6b4e6b21fd2eb25ef7e5f618a997a785a96155d5f7b662f89c2192276fcb5845e4df0f6687b5253a1096d9868836f00575ea843e8f3419d414ab09eeb228282e
7
+ data.tar.gz: 2f9497d13dce60eeb46b740c007257b31bae8d7e4f50c65c590a7875d469171c0f2a6d2fce5057f19db5b3b0a6d3349a1c7901914653eab1899f4691fcaa2fdb
data/lib/jaspion/kilza.rb CHANGED
@@ -7,6 +7,8 @@ require 'jaspion/kilza/language'
7
7
  require 'jaspion/kilza/language/objc'
8
8
  require 'jaspion/kilza/language/java'
9
9
  require 'jaspion/kilza/language/swift'
10
+ require 'jaspion/kilza/language/swift/class'
11
+ require 'jaspion/kilza/language/swift/property'
10
12
 
11
13
  # Ruby class
12
14
  class String
@@ -16,7 +16,8 @@ module Jaspion
16
16
  #
17
17
  # @param name [String] Class Name
18
18
  def initialize(name)
19
- @name = Kilza.normalize(name).capitalize
19
+ @name = Kilza.clean(name)
20
+ @name[0] = @name[0].capitalize
20
21
  @properties = []
21
22
  @imports = []
22
23
  end
@@ -53,7 +54,7 @@ module Jaspion
53
54
 
54
55
  s = Kilza::Source.new
55
56
  s.source = eruby.result(binding)
56
- s.file_name = @name.capitalize + '.' + file_name
57
+ s.file_name = @name + '.' + file_name
57
58
  s
58
59
  end
59
60
 
@@ -14,8 +14,11 @@ module Jaspion
14
14
  # JSON that will be used to generate objects
15
15
  attr_accessor :json_string
16
16
 
17
- # String that will be used to prefix reserved words
18
- attr_accessor :reserved_delimiter
17
+ # String that will be used to posfix reserved words for classes
18
+ attr_accessor :reserved_class_posfix
19
+
20
+ # String that will be used to prefix reserved words for properties
21
+ attr_accessor :reserved_property_prefix
19
22
 
20
23
  # Words that will receive an undescore before property name
21
24
  attr_accessor :reserved_words
@@ -37,7 +40,8 @@ module Jaspion
37
40
  @classes = []
38
41
  @types = {}
39
42
  @reserved_words = []
40
- @reserved_delimiter = '_'
43
+ @reserved_class_posfix = 'Class'
44
+ @reserved_property_prefix = '_'
41
45
  @equal_keys = []
42
46
  end
43
47
 
@@ -63,7 +67,7 @@ module Jaspion
63
67
  #
64
68
  # @return [Kilza::Class] new class
65
69
  def clazz(name)
66
- name = @reserved_delimiter + name unless @reserved_words.index(name).nil?
70
+ name = name + @reserved_class_posfix unless @reserved_words.index(name.downcase).nil?
67
71
  Class.new(name)
68
72
  end
69
73
 
@@ -78,7 +82,7 @@ module Jaspion
78
82
  # @return [Kilza::Property] new property
79
83
  def property(name, type, array, key)
80
84
  original_name = name
81
- name = @reserved_delimiter + name unless @reserved_words.index(name).nil?
85
+ name = @reserved_property_prefix + name unless @reserved_words.index(name.downcase).nil?
82
86
  prop = Property.new(name , type, array, key)
83
87
  prop.original_name = original_name
84
88
  prop
@@ -91,7 +95,10 @@ module Jaspion
91
95
  #
92
96
  # @return [Kilza::Class] class with the specified name
93
97
  def find(name)
94
- name = Kilza.normalize(name).capitalize
98
+ name = name + @reserved_class_posfix unless @reserved_words.index(name.downcase).nil?
99
+ name = Kilza.clean(name)
100
+ name[0] = name[0].capitalize
101
+
95
102
  @classes.each { |cl| return cl if cl.name == name }
96
103
  @classes.push(clazz(name))
97
104
  @classes.last
@@ -11,6 +11,7 @@ module Jaspion
11
11
 
12
12
  def initialize(name, package = nil)
13
13
  super(name)
14
+ @name = @name + RESERVED_CLASS_POSFIX unless RESERVED_WORDS.index(name.downcase).nil?
14
15
  @package = package
15
16
  end
16
17
 
@@ -27,7 +28,7 @@ module Jaspion
27
28
  class Java
28
29
  class Property < Jaspion::Kilza::Property
29
30
 
30
- def json_key
31
+ def constants
31
32
  return ' private static final String FIELD_' +
32
33
  @name.upcase + ' = "' + @original_name + '";'
33
34
  end
@@ -81,6 +82,32 @@ module Jaspion
81
82
  parse_element
82
83
  end
83
84
  end
85
+
86
+ def setter
87
+ newname = @name.gsub(/_*(.+)/) { $1 }.capitalize
88
+ r = StringIO.new
89
+ if array?
90
+ r << "public void set#{newname}(ArrayList<#{@type}> value) {"
91
+ else
92
+ r << "public void set#{newname}(#{@type} value) {"
93
+ end
94
+ r << "\n this.#{@name} = value;\n }"
95
+ r.string
96
+ end
97
+
98
+ def getter
99
+ newname = @name.gsub(/_*(.+)/) { $1 }.capitalize
100
+ r = StringIO.new
101
+ if array?
102
+ r << "public ArrayList<#{@type}> get#{@name.capitalize}() {"
103
+ elsif boolean?
104
+ r << "public #{@type} is#{newname}() {"
105
+ else
106
+ r << "public #{@type} get#{newname}() {"
107
+ end
108
+ r << "\n return this.#{@name};\n }"
109
+ r.string
110
+ end
84
111
  end
85
112
  end
86
113
  end
@@ -92,17 +119,19 @@ module Jaspion
92
119
  class Java
93
120
  include Jaspion::Kilza::Language
94
121
 
122
+ RESERVED_PROPERTY_PREFIX = '_'
123
+ RESERVED_CLASS_POSFIX = 'Class'
124
+ RESERVED_WORDS = %w(
125
+ abstract continue for new switch assert default goto,
126
+ package synchronized boolean do if private this break double implements,
127
+ protected throw byte else import public throws case enum instanceof,
128
+ null return transient catch extends int short try char final interface static,
129
+ void class finally long strictfp volatile const float native super while
130
+ )
131
+
95
132
  def initialize(json_string)
96
133
  super(json_string)
97
134
 
98
- @reserved_words = %w(
99
- abstract continue for new switch assert default goto,
100
- package synchronized boolean do if private this break double implements,
101
- protected throw byte else import public throws case enum instanceof,
102
- null return transient catch extends int short try char final interface static,
103
- void class finally long strictfp volatile const float native super while
104
- )
105
-
106
135
  @types = {
107
136
  'nilclass' => 'Object',
108
137
  'string' => 'String',
@@ -122,7 +151,7 @@ module Jaspion
122
151
 
123
152
  def property(name, type, array, key)
124
153
  original_name = name
125
- name = @reserved_delimiter + name unless @reserved_words.index(name).nil?
154
+ name = RESERVED_PROPERTY_PREFIX + name unless RESERVED_WORDS.index(name).nil?
126
155
  prop = Jaspion::Kilza::Java::Property.new(name , type, array, key)
127
156
  prop.original_name = original_name
128
157
  prop
@@ -133,7 +162,10 @@ module Jaspion
133
162
 
134
163
  @classes.each do |cl|
135
164
  cl.properties.each do |pr|
136
- pr.type = pr.name.capitalize if pr.object? || (pr.array? && pr.null?)
165
+ name = Kilza.clean(pr.original_name)
166
+ name[0] = name[0].capitalize
167
+ name = name + RESERVED_CLASS_POSFIX unless RESERVED_WORDS.index(name.downcase).nil?
168
+ pr.type = name if pr.object? || (pr.array? && pr.null?)
137
169
 
138
170
  cl.imports.push('import java.util.ArrayList;') if pr.array? &&
139
171
  cl.imports.index('import java.util.ArrayList;').nil?
@@ -1,3 +1,7 @@
1
+ /**
2
+ * Created on <%= Time.now.strftime("%Y-%m-%d") %>
3
+ * Generated by Kilza https://github.com/Jaspion/Kilza
4
+ */
1
5
  package <%= @package %>;
2
6
 
3
7
  import org.json.*;
@@ -14,13 +18,13 @@ import com.google.gson.annotations.Expose;
14
18
  public class <%= @name %> implements Serializable
15
19
  {
16
20
  <% for @property in @properties %>
17
- <%= @property.json_key %>
21
+ <%= @property.constants %>
18
22
  <% end %>
19
23
 
20
24
  <% for @property in @properties %>
21
25
  <%= @property.declaration %>
22
- <% end %>
23
26
 
27
+ <% end %>
24
28
  public <%= @name %>() {
25
29
 
26
30
  }
@@ -54,29 +58,20 @@ public class <%= @name %> implements Serializable
54
58
  }
55
59
 
56
60
  <% for @property in @properties %>
57
- public void set<%= @property.name.capitalize %>(<%= @property.array? ? "ArrayList<" + @property.type + ">" : @property.type %> value) {
58
- this.<%= @property.name %> = value;
59
- }
61
+ <%= @property.setter %>
60
62
 
61
- <% if @property.array? %>
62
- public ArrayList<<%= @property.type %>> get<%= @property.name.capitalize %>() {
63
- <% elsif @property.boolean? %>
64
- public <%= @property.type %> is<%= @property.name.capitalize %>() {
65
- <% else %>
66
- public <%= @property.type %> get<%= @property.name.capitalize %>() {
67
- <% end %>
68
- return this.<%= @property.name %>;
69
- }
63
+ <%= @property.getter %>
70
64
 
71
65
  <% end %>
72
66
  <%
73
67
  @eq = []
74
68
  @hs = []
75
69
  for @property in @properties
70
+ newname = @property.name.gsub(/_*(.+)/) { $1 }.capitalize
76
71
  if @property.boolean?
77
- @eq.push("((#{@name}) obj).is#{@property.name.capitalize}().equals(#{@property.name})") if @property.key?
72
+ @eq.push("((#{@name}) obj).is#{newname}().equals(#{@property.name})") if @property.key?
78
73
  else
79
- @eq.push("((#{@name}) obj).get#{@property.name.capitalize}().equals(#{@property.name})") if @property.key?
74
+ @eq.push("((#{@name}) obj).get#{newname}().equals(#{@property.name})") if @property.key?
80
75
  end
81
76
  @hs.push("#{@property.name}.hashCode()") if @property.key?
82
77
  end
@@ -6,6 +6,26 @@ module Jaspion
6
6
  class Class
7
7
  include Jaspion::Kilza::Class
8
8
 
9
+ def initialize(name)
10
+ super(name)
11
+ @name = @name + RESERVED_CLASS_POSFIX unless RESERVED_WORDS.index(name.downcase).nil?
12
+ end
13
+
14
+ def equals
15
+ r = StringIO.new
16
+ r << '- (BOOL)isEqual:(id)anObject {'
17
+ fields = []
18
+ for pr in @properties
19
+ fields.push("[((#{@name}) anObject).#{pr.name} isEqual:#{pr.name}]") if pr.key?
20
+ end
21
+ r << "\n if (anObject instanceof #{@name}) {"
22
+ r << " return (" + fields.join(" &&\n ") + "});"
23
+ r << "\n }"
24
+ r << "\n return false;"
25
+ r << "\n}"
26
+ r.string
27
+ end
28
+
9
29
  def sources
10
30
  [code('objc', 'h'), code('objc', 'm')]
11
31
  end
@@ -14,29 +34,56 @@ module Jaspion
14
34
  end
15
35
  end
16
36
 
37
+ module Jaspion
38
+ module Kilza
39
+ class Objc
40
+ class Property < Jaspion::Kilza::Property
41
+
42
+ def class_name
43
+ return if !object?
44
+ Jaspion::Kilza::Objc::Class.new(@original_name).name
45
+ end
46
+
47
+ def class_reference
48
+ return "@class #{class_name};" unless class_name.nil? || array?
49
+ end
50
+
51
+ def declaration
52
+ "@property (nonatomic, strong) #{@type} #{@name};"
53
+ end
54
+
55
+ def constants(cl_name)
56
+ "NSString *const k#{cl_name}#{@name.capitalize} = @\"#{@original_name}\";"
57
+ end
58
+
59
+ end
60
+ end
61
+ end
62
+ end
63
+
17
64
  module Jaspion
18
65
  module Kilza
19
66
  # Objective-C Language parser
20
67
  class Objc
21
68
  include Jaspion::Kilza::Language
22
69
 
70
+ RESERVED_PROPERTY_PREFIX = '_'
71
+ RESERVED_CLASS_POSFIX = 'Class'
72
+ RESERVED_WORDS = %w(
73
+ auto break case char const continue,
74
+ class default do double else enum extern float for,
75
+ goto if id implementation inline int interface long,
76
+ new nonatomic property protocol readonly readwrite register,
77
+ restrict retain return short signed sizeof static strong,
78
+ struct switch typedef union unsafe_unretained unsigned void,
79
+ volatile weak while _bool _complex _imaginary sel imp,
80
+ bool nil yes no self super __strong __weak oneway,
81
+ in out inout bycopy byref
82
+ )
83
+
23
84
  def initialize(json_string)
24
85
  super(json_string)
25
86
 
26
- @reserved_delimiter = '_my'
27
-
28
- @reserved_words = %w(
29
- auto break case char const continue,
30
- class default do double else enum extern float for,
31
- goto if id implementation inline int interface long,
32
- new nonatomic property protocol readonly readwrite register,
33
- restrict retain return short signed sizeof static strong,
34
- struct switch typedef union unsafe_unretained unsigned void,
35
- volatile weak while _bool _complex _imaginary sel imp,
36
- bool nil yes no self super __strong __weak oneway,
37
- in out inout bycopy byref
38
- )
39
-
40
87
  @types = {
41
88
  'nilclass' => 'id',
42
89
  'string' => 'NSString *',
@@ -51,17 +98,29 @@ module Jaspion
51
98
  end
52
99
 
53
100
  def clazz(name)
101
+ name = name + RESERVED_CLASS_POSFIX unless RESERVED_WORDS.index(name.downcase).nil?
54
102
  Jaspion::Kilza::Objc::Class.new(name)
55
103
  end
56
104
 
105
+ def property(name, type, array, key)
106
+ original_name = name
107
+ name = RESERVED_PROPERTY_PREFIX + name unless RESERVED_WORDS.index(name.downcase).nil?
108
+ prop = Jaspion::Kilza::Objc::Property.new(name , type, array, key)
109
+ prop.original_name = original_name
110
+ prop
111
+ end
112
+
57
113
  def classes(class_name)
58
114
  super(class_name)
59
115
 
60
116
  @classes.each do |cl|
61
117
  cl.properties.each do |pr|
62
118
  if pr.object? || (pr.array? && pr.null?)
63
- pr.type = pr.name.capitalize + ' *'
64
- cl.imports.push("#import \"#{pr.name.capitalize}.h\"")
119
+ name = Kilza.clean(pr.original_name)
120
+ name[0] = name[0].capitalize
121
+ name = name + RESERVED_CLASS_POSFIX unless RESERVED_WORDS.index(name.downcase).nil?
122
+ pr.type = name + ' *'
123
+ cl.imports.push("#import \"#{name}.h\"")
65
124
  end
66
125
 
67
126
  pr.type = 'NSMutableArray *' if pr.array?
@@ -9,15 +9,15 @@
9
9
  #import <Foundation/Foundation.h>
10
10
 
11
11
  <% for @property in @properties %>
12
- <% if @property.object? && !@property.array? %>
13
- @class <%= @property.name.capitalize %>;
12
+ <% unless @property.class_reference.nil? %>
13
+ <%= @property.class_reference %>
14
14
  <% end %>
15
15
  <% end %>
16
16
 
17
17
  @interface <%= @name %> : NSObject <NSCoding, NSCopying>
18
18
 
19
19
  <% for @property in @properties %>
20
- @property (nonatomic, strong) <%= @property.type %> <%= @property.name %>;
20
+ <%= @property.declaration %>
21
21
  <% end %>
22
22
 
23
23
  + (<%= @name %> *)modelWithDictionary:(NSDictionary *)dict;
@@ -13,7 +13,7 @@
13
13
 
14
14
  // Original names
15
15
  <% for @property in @properties %>
16
- NSString * const k<%= @name %><%= @property.name.capitalize %> = @"<%= @property.original_name %>";
16
+ <%= @property.constants(name) %>
17
17
  <% end %>
18
18
 
19
19
  @interface <%= @name %> ()
@@ -24,145 +24,132 @@ NSString * const k<%= @name %><%= @property.name.capitalize %> = @"<%= @property
24
24
 
25
25
  @implementation <%= @name %>
26
26
 
27
- + (<%= @name %> *)modelWithDictionary:(NSDictionary *)dict
28
- {
29
- <%= @name %> *instance = [[<%= @name %> alloc] initWithDictionary:dict];
30
- return instance;
27
+ + (<%= @name %> *)modelWithDictionary:(NSDictionary *)dict {
28
+ <%= @name %> *instance = [[<%= @name %> alloc] initWithDictionary:dict];
29
+ return instance;
31
30
  }
32
31
 
33
- + (<%= @name %> *)modelWithString:(NSString *)json
34
- {
35
- <%= @name %> *instance = [[<%= @name %> alloc] initWithString:json];
36
- return instance;
32
+ + (<%= @name %> *)modelWithString:(NSString *)json {
33
+ <%= @name %> *instance = [[<%= @name %> alloc] initWithString:json];
34
+ return instance;
37
35
  }
38
36
 
39
- - (instancetype)initWithString:(NSString *)json
40
- {
41
- self = [super init];
37
+ - (instancetype)initWithString:(NSString *)json {
38
+ self = [super init];
42
39
  <% if @properties.length == 1 %>
43
40
 
44
- if (![[json stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] hasPrefix:@"{"])
45
- json = [NSString stringWithFormat:@"{ \"%@\" : %@ }", k<%= @name %><%= @properties.first.name.capitalize %>, json];
41
+ if (![[json stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] hasPrefix:@"{"])
42
+ json = [NSString stringWithFormat:@"{ \"%@\" : %@ }", k<%= @name %><%= @properties.first.name.capitalize %>, json];
46
43
 
47
44
  <% end %>
48
- NSError *jsonError = nil;
49
- NSData *objectData = [json dataUsingEncoding:NSUTF8StringEncoding];
50
- NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:objectData
51
- options:NSJSONReadingMutableContainers
52
- error:&jsonError];
53
- if (!jsonError)
54
- self = [self initWithDictionary:dict];
55
-
56
- return self;
45
+ NSError *jsonError = nil;
46
+ NSData *objectData = [json dataUsingEncoding:NSUTF8StringEncoding];
47
+ NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:objectData
48
+ options:NSJSONReadingMutableContainers
49
+ error:&jsonError];
50
+ if (!jsonError)
51
+ self = [self initWithDictionary:dict];
52
+
53
+ return self;
57
54
  }
58
55
 
59
- - (instancetype)initWithDictionary:(NSDictionary *)dict
60
- {
61
- self = [super init];
56
+ - (instancetype)initWithDictionary:(NSDictionary *)dict {
57
+ self = [super init];
62
58
 
63
- if (self && [dict isKindOfClass:[NSDictionary class]])
64
- {
59
+ if (self && [dict isKindOfClass:[NSDictionary class]]) {
65
60
  <% for @property in @properties %>
66
61
  <% if @property.object? || (@property.null? && @property.array?) %>
67
- NSObject *obj<%= @property.name.capitalize %> = [dict objectForKey:k<%= @name %><%= @property.name.capitalize %>];
62
+ NSObject *obj<%= @property.class_name %> = [self objectOrNilForKey:k<%= @name %><%= @property.name.capitalize %> fromDictionary:dict];
68
63
  <% if @property.array? %>
69
- if ([obj<%= @property.name.capitalize %> isKindOfClass:[NSArray class]])
70
- {
71
- NSMutableArray *list<%= @property.name.capitalize %> = [NSMutableArray array];
72
- for (NSDictionary *item in (NSArray *)obj<%= @property.name.capitalize %>) {
73
- if ([item isKindOfClass:[NSDictionary class]]) {
74
- [list<%= @property.name.capitalize %> addObject:[<%= @property.name.capitalize %> modelWithDictionary:(NSDictionary *)item]];
64
+ if ([obj<%= @property.class_name %> isKindOfClass:[NSArray class]]) {
65
+ NSMutableArray *list<%= @property.class_name %> = [NSMutableArray array];
66
+ for (NSDictionary *item in (NSArray *)obj<%= @property.name.capitalize %>) {
67
+ if ([item isKindOfClass:[NSDictionary class]]) {
68
+ [list<%= @property.class_name %> addObject:[<%= @property.class_name %> modelWithDictionary:(NSDictionary *)item]];
69
+ }
70
+ }
71
+ self.<%= @property.name %> = list<%= @property.name.capitalize %>;
75
72
  }
76
- }
77
- self.<%= @property.name %> = list<%= @property.name.capitalize %>;
78
- }
79
73
  <% else %>
80
- {
81
- self.<%= @property.name %> = [<%= @property.name.capitalize %> modelWithDictionary:(NSDictionary *)obj<%= @property.name.capitalize %>];
82
- }
74
+ if (obj<%= @property.class_name %>) {
75
+ self.<%= @property.name %> = [<%= @property.class_name %> modelWithDictionary:(NSDictionary *)obj<%= @property.class_name %>];
76
+ }
83
77
  <% end %>
84
78
  <% else %>
85
- self.<%= @property.name %> = [self objectOrNilForKey:k<%= @name %><%= @property.name.capitalize %> fromDictionary:dict];
79
+ self.<%= @property.name %> = [self objectOrNilForKey:k<%= @name %><%= @property.name.capitalize %> fromDictionary:dict];
86
80
  <% end %>
87
81
  <% end %>
88
- }
89
- return self;
82
+ }
83
+ return self;
90
84
  }
91
85
 
92
- - (NSDictionary *)dictionaryRepresentation
93
- {
94
- NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
86
+ - (NSDictionary *)dictionaryRepresentation {
87
+ NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
95
88
 
96
89
  <% for @property in @properties %>
97
90
  <% if @property.object? || (@property.null? && @property.array?) %>
98
91
  <% if @property.array? %>
99
- NSMutableArray *tempArray<%= @property.name.capitalize %> = [NSMutableArray array];
100
- for (NSObject *subArray in self.<%= @property.name %>) {
101
- if ([subArray respondsToSelector:@selector(dictionaryRepresentation)]) {
102
- [tempArray<%= @property.name.capitalize %> addObject:[subArray performSelector:@selector(dictionaryRepresentation)]];
103
- } else {
104
- [tempArray<%= @property.name.capitalize %> addObject:subArray];
92
+ NSMutableArray *tempArray<%= @property.name.capitalize %> = [NSMutableArray array];
93
+ for (NSObject *subArray in self.<%= @property.name %>) {
94
+ if ([subArray respondsToSelector:@selector(dictionaryRepresentation)]) {
95
+ [tempArray<%= @property.name.capitalize %> addObject:[subArray performSelector:@selector(dictionaryRepresentation)]];
96
+ } else {
97
+ [tempArray<%= @property.name.capitalize %> addObject:subArray];
98
+ }
105
99
  }
106
- }
107
- [mutableDict setValue:[NSArray arrayWithArray:tempArray<%= @property.name.capitalize %>] forKey:k<%= @name %><%= @property.name.capitalize %>];
100
+ [mutableDict setValue:[NSArray arrayWithArray:tempArray<%= @property.name.capitalize %>] forKey:k<%= @name %><%= @property.name.capitalize %>];
108
101
  <% else %>
109
- if ([self.<%= @property.name %> respondsToSelector:@selector(dictionaryRepresentation)]) {
110
- [mutableDict setValue:[self.<%= @property.name %> performSelector:@selector(dictionaryRepresentation)] forKey:k<%= @name %><%= @property.name.capitalize %>];
111
- } else {
112
- [mutableDict setValue:self.<%= @property.name %> forKey:k<%= @name %><%= @property.name.capitalize %>];
113
- }
102
+ if ([self.<%= @property.name %> respondsToSelector:@selector(dictionaryRepresentation)]) {
103
+ [mutableDict setValue:[self.<%= @property.name %> performSelector:@selector(dictionaryRepresentation)] forKey:k<%= @name %><%= @property.name.capitalize %>];
104
+ } else {
105
+ [mutableDict setValue:self.<%= @property.name %> forKey:k<%= @name %><%= @property.name.capitalize %>];
106
+ }
114
107
  <% end %>
115
108
  <% else %>
116
- [mutableDict setValue:self.<%= @property.name %> forKey:k<%= @name %><%= @property.name.capitalize %>];
109
+ [mutableDict setValue:self.<%= @property.name %> forKey:k<%= @name %><%= @property.name.capitalize %>];
117
110
  <% end %>
118
111
  <% end %>
119
112
 
120
- return [NSDictionary dictionaryWithDictionary:mutableDict];
113
+ return [NSDictionary dictionaryWithDictionary:mutableDict];
121
114
  }
122
115
 
123
- - (NSString *)description
124
- {
125
- return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
116
+ - (NSString *)description {
117
+ return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
126
118
  }
127
119
 
128
120
  #pragma mark - Helper Method
129
- - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
130
- {
131
- id object = [dict objectForKey:aKey];
132
- return [object isEqual:[NSNull null]] ? nil : object;
121
+ - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
122
+ id object = [dict objectForKey:aKey];
123
+ return [object isEqual:[NSNull null]] ? nil : object;
133
124
  }
134
125
 
135
126
  #pragma mark - NSCoding Methods
136
127
 
137
- - (id)initWithCoder:(NSCoder *)aDecoder
138
- {
139
- self = [super init];
128
+ - (id)initWithCoder:(NSCoder *)aDecoder {
129
+ self = [super init];
140
130
 
141
131
  <% for @property in @properties %>
142
- self.<%= @property.name %> = [aDecoder decodeObjectForKey:k<%= @name %><%= @property.name.capitalize %>];
132
+ self.<%= @property.name %> = [aDecoder decodeObjectForKey:k<%= @name %><%= @property.name.capitalize %>];
143
133
  <% end %>
144
134
 
145
- return self;
135
+ return self;
146
136
  }
147
137
 
148
- - (void)encodeWithCoder:(NSCoder *)aCoder
149
- {
138
+ - (void)encodeWithCoder:(NSCoder *)aCoder {
150
139
  <% for @property in @properties %>
151
- [aCoder encodeObject:_<%= @property.name %> forKey:k<%= @name %><%= @property.name.capitalize %>];
140
+ [aCoder encodeObject:_<%= @property.name %> forKey:k<%= @name %><%= @property.name.capitalize %>];
152
141
  <% end %>
153
142
  }
154
143
 
155
- - (id)copyWithZone:(NSZone *)zone
156
- {
157
- <%= @name %> *copy = [[<%= @name %> alloc] init];
158
- if (copy)
159
- {
144
+ - (id)copyWithZone:(NSZone *)zone {
145
+ <%= @name %> *copy = [[<%= @name %> alloc] init];
146
+ if (copy) {
160
147
  <% for @property in @properties %>
161
- copy.<%= @property.name %> = [self.<%= @property.name %> copyWithZone:zone];
148
+ copy.<%= @property.name %> = [self.<%= @property.name %> copyWithZone:zone];
162
149
  <% end %>
163
- }
150
+ }
164
151
 
165
- return copy;
152
+ return copy;
166
153
  }
167
154
 
168
155
  @end
@@ -1,42 +1,28 @@
1
1
  require 'date'
2
2
 
3
- module Jaspion
4
- module Kilza
5
- class Swift
6
- class Class
7
- include Jaspion::Kilza::Class
8
-
9
- def sources
10
- [code('swift', 'swift')]
11
- end
12
- end
13
- end
14
- end
15
- end
16
-
17
3
  module Jaspion
18
4
  module Kilza
19
5
  # Swift Language parser
20
6
  class Swift
21
7
  include Jaspion::Kilza::Language
22
8
 
9
+ RESERVED_PROPERTY_PREFIX = '_'
10
+ RESERVED_CLASS_POSFIX = 'Class'
11
+ RESERVED_WORDS = %w(
12
+ class break as associativity deinit case dynamicType
13
+ convenience enum continue false dynamic extension default
14
+ is didSet func do nil final import else self get init
15
+ fallthrough Self infix internal for super inout let
16
+ if true lazy operator in left private return mutating
17
+ protocol switch none public where nonmutating static
18
+ while optional struct override subscript postfix
19
+ typealias precedence var prefix required right set
20
+ type unowned weak id description
21
+ )
22
+
23
23
  def initialize(json_string)
24
24
  super(json_string)
25
25
 
26
- @reserved_delimiter = '_my'
27
-
28
- @reserved_words = %w(
29
- class break as associativity deinit case dynamicType
30
- convenience enum continue false dynamic extension default
31
- is didSet func do nil final import else self get init
32
- fallthrough Self infix internal for super inout let
33
- if true lazy operator in left private return mutating
34
- protocol switch none public where nonmutating static
35
- while optional struct override subscript postfix
36
- typealias precedence var prefix required right set
37
- type unowned weak id
38
- )
39
-
40
26
  @types = {
41
27
  'nilclass' => 'AnyObject',
42
28
  'string' => 'String',
@@ -51,16 +37,29 @@ module Jaspion
51
37
  end
52
38
 
53
39
  def clazz(name)
40
+ name = name + RESERVED_CLASS_POSFIX unless RESERVED_WORDS.index(name.downcase).nil?
54
41
  Jaspion::Kilza::Swift::Class.new(name)
55
42
  end
56
43
 
44
+ def property(name, type, array, key)
45
+ original_name = name
46
+ name = RESERVED_PROPERTY_PREFIX + name unless RESERVED_WORDS.index(name.downcase).nil?
47
+ prop = Jaspion::Kilza::Swift::Property.new(name , type, array, key)
48
+ prop.original_name = original_name
49
+ prop
50
+ end
51
+
57
52
  def classes(class_name)
58
53
  super(class_name)
59
54
 
60
55
  @classes.each do |cl|
61
56
  cl.properties.each do |pr|
62
57
  if pr.object? || (pr.array? && pr.null?)
63
- pr.type = pr.name.capitalize
58
+ name = Kilza.clean(pr.original_name)
59
+ name[0] = name[0].capitalize
60
+ name = name + RESERVED_CLASS_POSFIX unless RESERVED_WORDS.index(name.downcase).nil?
61
+
62
+ pr.type = name
64
63
  cl.imports.push("import #{pr.name.capitalize}")
65
64
  end
66
65
 
@@ -0,0 +1,18 @@
1
+ module Jaspion
2
+ module Kilza
3
+ class Swift
4
+ class Class
5
+ include Jaspion::Kilza::Class
6
+
7
+ def initialize(name)
8
+ super(name)
9
+ @name = @name + RESERVED_CLASS_POSFIX unless RESERVED_WORDS.index(name.downcase).nil?
10
+ end
11
+
12
+ def sources
13
+ [code('swift', 'swift')]
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ require 'jaspion/kilza/language/swift/class'
2
+
3
+ module Jaspion
4
+ module Kilza
5
+ class Swift
6
+ class Property < Jaspion::Kilza::Property
7
+ def class_name
8
+ return if !object? || array?
9
+ Jaspion::Kilza::Swift::Class.new(@original_name).name
10
+ end
11
+
12
+ def constants(cl_name)
13
+ " static let k#{cl_name}#{@name.capitalize}: String = \"#{@original_name.gsub('"', '\"')}\""
14
+ end
15
+
16
+ def declaration
17
+ " public var #{@name}: #{@type}?"
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,5 +1,5 @@
1
1
  //
2
- // <%= @name %>.m
2
+ // <%= @name %>.swift
3
3
  //
4
4
  // Created on <%= Time.now.strftime("%Y-%m-%d") %>
5
5
  // Copyright (c) <%= Time.now.strftime("%Y") %>. All rights reserved.
@@ -11,62 +11,66 @@ import Foundation
11
11
  public class <%= @name %>: NSObject, NSCoding {
12
12
  // Original names
13
13
  <% for @property in @properties %>
14
- static let k<%= @name %><%= @property.name.capitalize %>: String = "<%= @property.original_name.gsub('"', '\"') %>"
14
+ <%= @property.constants(@name) %>
15
15
  <% end %>
16
16
 
17
17
  <% for @property in @properties %>
18
18
  public var <%= @property.name %>: <%= @property.type %>?
19
19
  <% end %>
20
20
 
21
- public class func model(obj: AnyObject) -> <%= @name %> {
21
+ public class func model(obj: AnyObject) -> <%= @name %>? {
22
22
  var instance: <%= @name %>?
23
23
  if (obj is String) {
24
24
  instance = <%= @name %>.init(str: obj as! String)
25
25
  } else if (obj is Dictionary<String, AnyObject>) {
26
26
  instance = <%= @name %>.init(dict: obj as! Dictionary)
27
27
  }
28
- return instance!
28
+ return instance
29
29
  }
30
30
 
31
- public convenience init(str: String) {
31
+ public convenience init?(str: String) {
32
32
  <% if @properties.length == 1 %>
33
33
  var nStr: String = str
34
34
  if let trimmed: String = str.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) {
35
- if !trimmed.hasPrefix("{") {
36
- nStr = "{ \"\(<%= @name %>.k<%= @name %><%= @properties.first.name.capitalize %>)\" : \(str) }"
37
- }
35
+ if !trimmed.hasPrefix("{") {
36
+ nStr = "{ \"\(<%= @name %>.k<%= @name %><%= @properties.first.name.capitalize %>)\" : \(str) }"
37
+ }
38
38
  }
39
39
 
40
40
  if let data = nStr.dataUsingEncoding(NSUTF8StringEncoding) {
41
41
  <% else %>
42
42
  if let data = str.dataUsingEncoding(NSUTF8StringEncoding) {
43
43
  <% end %>
44
- do {
45
- let object: AnyObject = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)
46
- self.init(dict: object as! Dictionary)
47
- } catch _ as NSError {
48
- self.init(dict: Dictionary())
49
- }
44
+ do {
45
+ let object: AnyObject = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)
46
+ self.init(dict: object as! Dictionary)
47
+ } catch _ as NSError {
48
+ self.init(dict: Dictionary())
49
+ }
50
50
  } else {
51
- self.init(dict: Dictionary())
51
+ self.init(dict: Dictionary())
52
52
  }
53
53
  }
54
54
 
55
- public init(dict: Dictionary<String, AnyObject>) {
56
- super.init()
55
+ public init?(dict: Dictionary<String, AnyObject>) {
56
+ super.init()
57
57
  <% for @property in @properties %>
58
58
  <% if @property.object? || @property.null? %>
59
59
  <% if @property.array? %>
60
- let obj<%= @property.name.capitalize %>: [AnyObject] = dict[<%= @name %>.k<%= @name %><%= @property.name.capitalize %>]! as! [AnyObject]
61
- var list<%= @property.name.capitalize %> = <%= @property.type %>()
62
- for item in obj<%= @property.name.capitalize %> {
63
- if item is Dictionary<String, AnyObject> {
64
- list<%= @property.name.capitalize %>.append(<%= @property.name.capitalize %>.model(item))
60
+ if let obj<%= @property.name.capitalize %>: [AnyObject] = dict[<%= @name %>.k<%= @name %><%= @property.name.capitalize %>] as? [AnyObject] {
61
+
62
+ var list<%= @property.name.capitalize %> = <%= @property.type %>()
63
+ for item in obj<%= @property.name.capitalize %> {
64
+ if item is Dictionary<String, AnyObject> {
65
+ list<%= @property.name.capitalize %>.append(<%= @property.name.capitalize %>.model(item))
66
+ }
65
67
  }
68
+ self.<%= @property.name %> = list<%= @property.name.capitalize %>
69
+ } else {
70
+ return nil
66
71
  }
67
- self.<%= @property.name %> = list<%= @property.name.capitalize %>
68
72
  <% else %>
69
- self.<%= @property.name %> = <%= @property.name.capitalize %>.model(dict[<%= @name %>.k<%= @name %><%= @property.name.capitalize %>]!)
73
+ self.<%= @property.name %> = <%= @property.class_name %>.model(dict[<%= @name %>.k<%= @name %><%= @property.name.capitalize %>]!)
70
74
  <% end %>
71
75
  <% else %>
72
76
  self.<%= @property.name %> = objectOrNil(forKey: <%= @name %>.k<%= @name %><%= @property.name.capitalize %>, fromDictionary:dict)<% if @property.type != 'AnyObject' %> as? <%= @property.type %><% end %>
@@ -91,7 +95,7 @@ public class <%= @name %>: NSObject, NSCoding {
91
95
  }
92
96
  mutableDict[<%= @name %>.k<%= @name %><%= @property.name.capitalize %>] = Array.init(tempArray<%= @property.name.capitalize %>)
93
97
  <% else %>
94
- if let dic = <%= @property.name %>?.dictionaryRepresentation() {
98
+ if let dic = self.<%= @property.name %>?.dictionaryRepresentation() {
95
99
  mutableDict[<%= @name %>.k<%= @name %><%= @property.name.capitalize %>] = dic
96
100
  } else {
97
101
  mutableDict[<%= @name %>.k<%= @name %><%= @property.name.capitalize %>] = self.<%= @property.name %>
@@ -106,12 +110,12 @@ public class <%= @name %>: NSObject, NSCoding {
106
110
 
107
111
  public func objectOrNil(forKey key: String, fromDictionary dict: Dictionary<String, AnyObject>) -> AnyObject?
108
112
  {
109
- if let object: AnyObject = dict[key] {
110
- if !(object is NSNull) {
111
- return object
113
+ if let object: AnyObject = dict[key] {
114
+ if !(object is NSNull) {
115
+ return object
116
+ }
112
117
  }
113
- }
114
- return nil
118
+ return nil
115
119
  }
116
120
 
117
121
  required public init(coder aDecoder: NSCoder) {
@@ -1,6 +1,6 @@
1
1
  # Tranforms a JSON string into Objects
2
2
  module Jaspion
3
3
  module Kilza
4
- VERSION = '1.1.0'
4
+ VERSION = '1.1.1'
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jaspion-kilza
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toshiro Sugii
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-06 00:00:00.000000000 Z
11
+ date: 2016-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -167,6 +167,8 @@ files:
167
167
  - lib/jaspion/kilza/language/objc/h.erb
168
168
  - lib/jaspion/kilza/language/objc/m.erb
169
169
  - lib/jaspion/kilza/language/swift.rb
170
+ - lib/jaspion/kilza/language/swift/class.rb
171
+ - lib/jaspion/kilza/language/swift/property.rb
170
172
  - lib/jaspion/kilza/language/swift/swift.erb
171
173
  - lib/jaspion/kilza/property.rb
172
174
  - lib/jaspion/kilza/source.rb