jaspion-kilza 1.1.1 → 1.1.2

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: f4bd19ba66c3c9b74c745750c4cb82ae424d9de9
4
- data.tar.gz: e25b99e560d34b8abdba7777c89a576c4ab24e95
3
+ metadata.gz: 2ba6bd6fcbaf07090a7b8fbb96f2a42770b83184
4
+ data.tar.gz: 4f3c0d86a9a346327f9f67f0d6ba5a3ab1127519
5
5
  SHA512:
6
- metadata.gz: 6b4e6b21fd2eb25ef7e5f618a997a785a96155d5f7b662f89c2192276fcb5845e4df0f6687b5253a1096d9868836f00575ea843e8f3419d414ab09eeb228282e
7
- data.tar.gz: 2f9497d13dce60eeb46b740c007257b31bae8d7e4f50c65c590a7875d469171c0f2a6d2fce5057f19db5b3b0a6d3349a1c7901914653eab1899f4691fcaa2fdb
6
+ metadata.gz: 4d53ded510827a5b16fcde8569c4434d50a95d62707555dcaa73005c5fb7e2f3f8bbd62c9317d6b07d1197b37b5b4bbaf5e00cb626e31c9e7c8c67d59c13d843
7
+ data.tar.gz: f60a8b7ec1b71629310aaa9df292fea03efb36204f61b20139f7c629746fd1563a3a549b41221870bbd39567e8e8e58fb6fea40cc212f5b9bb41e437d888a329
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Jaspion Kilza
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/kilza.svg)](https://badge.fury.io/rb/kilza)
3
+ [![Gem Version](https://badge.fury.io/rb/jaspion-kilza.svg)](https://badge.fury.io/rb/jaspion-kilza)
4
4
 
5
5
  [![Build Status](https://travis-ci.org/Jaspion/Kilza.svg?branch=master)](https://travis-ci.org/Jaspion/Kilza)
6
6
 
@@ -5,7 +5,11 @@ require 'jaspion/kilza/class'
5
5
  require 'jaspion/kilza/property'
6
6
  require 'jaspion/kilza/language'
7
7
  require 'jaspion/kilza/language/objc'
8
+ require 'jaspion/kilza/language/objc/class'
9
+ require 'jaspion/kilza/language/objc/property'
8
10
  require 'jaspion/kilza/language/java'
11
+ require 'jaspion/kilza/language/java/class'
12
+ require 'jaspion/kilza/language/java/property'
9
13
  require 'jaspion/kilza/language/swift'
10
14
  require 'jaspion/kilza/language/swift/class'
11
15
  require 'jaspion/kilza/language/swift/property'
@@ -22,28 +26,32 @@ class String
22
26
  end
23
27
  end
24
28
 
29
+ # Ruby class
30
+ class Array
31
+ # Inserts an separator between each element
32
+ #
33
+ # @param sep [String] string to be used as separator
34
+ #
35
+ # @return [Array] the new array
36
+ def separate(sep = '')
37
+ a = self
38
+ l = a.length - 2
39
+ (0..l).each { |i| a.insert(((2 * i) + 1), sep) }
40
+ a
41
+ end
42
+
43
+ # Inserts an separator between each element
44
+ #
45
+ # @param sep [String] string to be used as separator
46
+ def separate!(sep = '')
47
+ l = length - 2
48
+ (0..l).each { |i| insert(((2 * i) + 1), sep) }
49
+ end
50
+ end
51
+
25
52
  # Tranforms a JSON string into Objects
26
53
  module Jaspion
27
54
  module Kilza
28
- # Removes everything except numbers and letters.
29
- #
30
- # @param str [String] string to be cleaned
31
- #
32
- # @return [String] cleaned string
33
- def self.clean(str)
34
- return if str.nil?
35
- str = '_' + str if str[0].number?
36
- str.gsub(/[^a-zA-Z0-9]/, '_')
37
- end
38
55
 
39
- # Cleans the string and make it lowercase.
40
- #
41
- # @param str [String] string to be cleaned
42
- #
43
- # @return [String] cleaned string
44
- def self.normalize(str)
45
- return if str.nil?
46
- Jaspion::Kilza.clean(str).downcase
47
- end
48
56
  end
49
57
  end
@@ -5,10 +5,6 @@ module Jaspion
5
5
  # Class name
6
6
  attr_accessor :name
7
7
 
8
- # Array with all class dependecies
9
- # Specific for each language
10
- attr_accessor :imports
11
-
12
8
  # Array with all class properties
13
9
  attr_accessor :properties
14
10
 
@@ -16,18 +12,22 @@ module Jaspion
16
12
  #
17
13
  # @param name [String] Class Name
18
14
  def initialize(name)
19
- @name = Kilza.clean(name)
20
- @name[0] = @name[0].capitalize
15
+ @name = Jaspion::Kilza::Class.normalize(name)
16
+ # @name[0] = @name[0].capitalize
21
17
  @properties = []
22
18
  @imports = []
23
19
  end
24
20
 
21
+ def imports
22
+ @imports.sort.separate.flatten
23
+ end
24
+
25
25
  # Adds a new property
26
26
  #
27
27
  # @param property [Kilza::Property] Property to include
28
28
  def push(property)
29
29
  index = @properties.index(property)
30
- if !index.nil?
30
+ unless index.nil?
31
31
  current = @properties[index]
32
32
  @properties[index] = update(current, property)
33
33
  else
@@ -68,6 +68,25 @@ module Jaspion
68
68
  }.to_s
69
69
  end
70
70
 
71
+ # Adds an new import statement
72
+ #
73
+ # @param import [String|Array] The whole statement that has to be printted
74
+ # It can be an Array
75
+ def push_import(import)
76
+ import = [import] if import.is_a? String
77
+ index = @imports.index(import)
78
+ @imports.push(import) if index.nil?
79
+ end
80
+
81
+ # Removes an new import statement
82
+ #
83
+ # @param import [String|Array] The statement to be removed
84
+ def delete_import(import)
85
+ import = [import] if import.is_a? String
86
+ i = @imports.index(import)
87
+ @imports.delete_at(i) unless i.nil?
88
+ end
89
+
71
90
  protected
72
91
 
73
92
  # Compares two properties and fill the src property with relevant
@@ -84,6 +103,21 @@ module Jaspion
84
103
  src.key = dst.key unless src.key
85
104
  src
86
105
  end
106
+
107
+ # Removes everything except numbers and letters
108
+ # and removes all _ at the beginning and capitalizes the first character
109
+ #
110
+ # @param str [String] string to be cleaned
111
+ #
112
+ # @return [String] cleaned string
113
+ def self.normalize(str)
114
+ return if str.nil?
115
+ str = '_' + str if str[0].number?
116
+ str = str.gsub(/[^a-zA-Z0-9]/, '')
117
+ str = str.gsub(/_*(.+)/) { $1 }
118
+ str[0] = str[0].capitalize
119
+ str
120
+ end
87
121
  end
88
122
  end
89
123
  end
@@ -14,12 +14,6 @@ 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 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
22
-
23
17
  # Words that will receive an undescore before property name
24
18
  attr_accessor :reserved_words
25
19
 
@@ -40,8 +34,6 @@ module Jaspion
40
34
  @classes = []
41
35
  @types = {}
42
36
  @reserved_words = []
43
- @reserved_class_posfix = 'Class'
44
- @reserved_property_prefix = '_'
45
37
  @equal_keys = []
46
38
  end
47
39
 
@@ -67,7 +59,10 @@ module Jaspion
67
59
  #
68
60
  # @return [Kilza::Class] new class
69
61
  def clazz(name)
70
- name = name + @reserved_class_posfix unless @reserved_words.index(name.downcase).nil?
62
+ # cl_name = self.class.name.split('::').last
63
+ # cl = Jaspion::Kilza.const_get("#{cl_name}::Class")
64
+ # return cl.new(name) unless cl.nil?
65
+ #
71
66
  Class.new(name)
72
67
  end
73
68
 
@@ -82,7 +77,6 @@ module Jaspion
82
77
  # @return [Kilza::Property] new property
83
78
  def property(name, type, array, key)
84
79
  original_name = name
85
- name = @reserved_property_prefix + name unless @reserved_words.index(name.downcase).nil?
86
80
  prop = Property.new(name , type, array, key)
87
81
  prop.original_name = original_name
88
82
  prop
@@ -95,9 +89,7 @@ module Jaspion
95
89
  #
96
90
  # @return [Kilza::Class] class with the specified name
97
91
  def find(name)
98
- name = name + @reserved_class_posfix unless @reserved_words.index(name.downcase).nil?
99
- name = Kilza.clean(name)
100
- name[0] = name[0].capitalize
92
+ name = Jaspion::Kilza::Class::normalize(name)
101
93
 
102
94
  @classes.each { |cl| return cl if cl.name == name }
103
95
  @classes.push(clazz(name))
@@ -1,118 +1,5 @@
1
1
  require 'date'
2
2
 
3
- module Jaspion
4
- module Kilza
5
- class Java
6
- class Class
7
- include Jaspion::Kilza::Class
8
-
9
- # Represents the Java class package
10
- attr_accessor :package
11
-
12
- def initialize(name, package = nil)
13
- super(name)
14
- @name = @name + RESERVED_CLASS_POSFIX unless RESERVED_WORDS.index(name.downcase).nil?
15
- @package = package
16
- end
17
-
18
- def sources
19
- [code('java', 'java')]
20
- end
21
- end
22
- end
23
- end
24
- end
25
-
26
- module Jaspion
27
- module Kilza
28
- class Java
29
- class Property < Jaspion::Kilza::Property
30
-
31
- def constants
32
- return ' private static final String FIELD_' +
33
- @name.upcase + ' = "' + @original_name + '";'
34
- end
35
-
36
- def declaration
37
- r = %( @Expose
38
- @SerializedName(FIELD_#{@name.upcase})
39
- )
40
-
41
- if array?
42
- r + " private ArrayList<#{@type}> #{@name};"
43
- else
44
- r + " private #{@type} #{@name};"
45
- end
46
- end
47
-
48
- def parse_element
49
- if object?
50
- " this.#{@name} = new #{@type}(object.optJSONObject(FIELD_#{@name.upcase}));"
51
- elsif null?
52
- " this.#{@name} = object.opt(FIELD_#{@name.upcase});"
53
- else
54
- " this.#{@name} = object.opt#{@type}(FIELD_#{@name.upcase});"
55
- end
56
- end
57
-
58
- def parse_array
59
- r = %( if (object.optJSONArray(FIELD_#{@name.upcase}) != null)
60
- {
61
- this.#{@name} = new ArrayList<>();
62
- JSONArray #{@name}JsonArray = object.optJSONArray(FIELD_#{@name.upcase});
63
- for (int i = 0; i < #{@name}JsonArray.length(); i++) {
64
- )
65
-
66
- if object? || null?
67
- r = r + " JSONObject #{@name} = #{@name}JsonArray.optJSONObject(i);"
68
- else
69
- r = r + " #{@type} #{@name} = #{@name}JsonArray.optJSON#{@type}(i);"
70
- end
71
- r = r + %(
72
- this.#{@name}.add(new #{@type}(#{@name}));
73
- }
74
- })
75
- return r
76
- end
77
-
78
- def parse_json
79
- if array?
80
- parse_array
81
- else
82
- parse_element
83
- end
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
111
- end
112
- end
113
- end
114
- end
115
-
116
3
  module Jaspion
117
4
  module Kilza
118
5
  # Objective-C Language parser
@@ -128,20 +15,19 @@ module Jaspion
128
15
  null return transient catch extends int short try char final interface static,
129
16
  void class finally long strictfp volatile const float native super while
130
17
  )
18
+ TYPES = {
19
+ 'nilclass' => 'Object',
20
+ 'string' => 'String',
21
+ 'fixnum' => 'Long',
22
+ 'float' => 'Double',
23
+ 'falseclass' => 'Boolean',
24
+ 'trueclass' => 'Boolean',
25
+ 'hash' => 'Object'
26
+ }
131
27
 
132
28
  def initialize(json_string)
133
29
  super(json_string)
134
30
 
135
- @types = {
136
- 'nilclass' => 'Object',
137
- 'string' => 'String',
138
- 'fixnum' => 'Long',
139
- 'float' => 'Double',
140
- 'falseclass' => 'Boolean',
141
- 'trueclass' => 'Boolean',
142
- 'hash' => 'Object'
143
- }
144
-
145
31
  @equal_keys = %w(id identifier uid)
146
32
  end
147
33
 
@@ -150,29 +36,7 @@ module Jaspion
150
36
  end
151
37
 
152
38
  def property(name, type, array, key)
153
- original_name = name
154
- name = RESERVED_PROPERTY_PREFIX + name unless RESERVED_WORDS.index(name).nil?
155
- prop = Jaspion::Kilza::Java::Property.new(name , type, array, key)
156
- prop.original_name = original_name
157
- prop
158
- end
159
-
160
- def classes(class_name)
161
- super(class_name)
162
-
163
- @classes.each do |cl|
164
- cl.properties.each do |pr|
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?)
169
-
170
- cl.imports.push('import java.util.ArrayList;') if pr.array? &&
171
- cl.imports.index('import java.util.ArrayList;').nil?
172
-
173
- pr.type = @types[pr.type] unless @types[pr.type].nil?
174
- end
175
- end
39
+ Jaspion::Kilza::Java::Property.new(name , type, array, key)
176
40
  end
177
41
  end
178
42
  end
@@ -0,0 +1,99 @@
1
+ module Jaspion
2
+ module Kilza
3
+ class Java
4
+ class Class
5
+ include Jaspion::Kilza::Class
6
+
7
+ # Represents the Java class package
8
+ attr_accessor :package
9
+
10
+ attr_reader :gson
11
+ attr_reader :serializable
12
+ attr_reader :parcelable
13
+
14
+ alias :gson? :gson
15
+ alias_method :serializable?, :serializable
16
+ alias_method :parcelable?, :parcelable
17
+
18
+ def initialize(name, package = nil)
19
+ name = name + RESERVED_CLASS_POSFIX unless RESERVED_WORDS.index(name.downcase).nil?
20
+ super(name)
21
+
22
+ @package = package
23
+ @implements = []
24
+
25
+ push_import('import org.json.*;')
26
+
27
+ self.serializable = true
28
+ self.parcelable = false
29
+ self.gson = true
30
+ end
31
+
32
+ def push(pr)
33
+ pr.gson = gson?
34
+
35
+ pr.type = pr.class_name if pr.object? || (pr.array? && pr.null?)
36
+ push_import('import java.util.ArrayList;') if pr.array?
37
+ pr.type = Jaspion::Kilza::Java::TYPES[pr.type] unless Jaspion::Kilza::Java::TYPES[pr.type].nil?
38
+
39
+ super(pr)
40
+ end
41
+
42
+ def push_implements(implement)
43
+ @implements.push(implement)
44
+ end
45
+
46
+ def implements
47
+ @implements.sort
48
+ end
49
+
50
+ def gson=(g)
51
+ @gson = g
52
+
53
+ v = %w(
54
+ import\ com.google.gson.Gson;
55
+ import\ com.google.gson.GsonBuilder;
56
+ import\ com.google.gson.annotations.SerializedName;
57
+ import\ com.google.gson.annotations.Expose;
58
+ )
59
+
60
+ if (g)
61
+ push_import(v)
62
+ else
63
+ delete_import(v)
64
+ end
65
+ end
66
+
67
+ def parcelable=(p)
68
+ @parcelable = p
69
+
70
+ v = 'import android.os.Parcelable;'
71
+ if (p)
72
+ push_implements('Parcelable')
73
+ push_import(v)
74
+ else
75
+ implements.delete('Parcelable')
76
+ delete_import(v)
77
+ end
78
+ end
79
+
80
+ def serializable=(s)
81
+ @serializable = s
82
+
83
+ v = 'import java.io.Serializable;'
84
+ if (s)
85
+ push_implements('Serializable')
86
+ push_import(v)
87
+ else
88
+ implements.delete('Serializable')
89
+ delete_import(v)
90
+ end
91
+ end
92
+
93
+ def sources
94
+ [code('java', 'java')]
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -4,18 +4,11 @@
4
4
  */
5
5
  package <%= @package %>;
6
6
 
7
- import org.json.*;
8
- import java.io.Serializable;
9
- <% for @import in @imports %>
7
+ <% for @import in imports %>
10
8
  <%= @import %>
11
9
  <% end %>
12
10
 
13
- import com.google.gson.Gson;
14
- import com.google.gson.GsonBuilder;
15
- import com.google.gson.annotations.SerializedName;
16
- import com.google.gson.annotations.Expose;
17
-
18
- public class <%= @name %> implements Serializable
11
+ public class <%= @name %><% if @implements.length > 0 %> implements <%= implements.join(', ') %><% end %>
19
12
  {
20
13
  <% for @property in @properties %>
21
14
  <%= @property.constants %>
@@ -94,7 +87,35 @@ end
94
87
 
95
88
  @Override
96
89
  public String toString() {
97
- Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
98
- return gson.toJson(this);
90
+ Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
91
+ return gson.toJson(this);
92
+ }
93
+ <% if parcelable %>
94
+
95
+ private <%= @name %>(Parcel in) {
96
+ <% for @property in @properties %>
97
+ <%= @property.read_parcel %>
98
+ <% end %>
99
+ }
100
+
101
+ public int describeContents() {
102
+ return 0;
103
+ }
104
+
105
+ public void writeToParcel(Parcel out, int flags) {
106
+ <% for @property in @properties %>
107
+ <%= @property.write_parcel %>
108
+ <% end %>
99
109
  }
110
+
111
+ public static final Parcelable.Creator<<%= @name %>> CREATOR = new Parcelable.Creator<<%= @name %>>() {
112
+ public <%= @name %> createFromParcel(Parcel in) {
113
+ return new <%= @name %>(in);
114
+ }
115
+
116
+ public <%= @name %>[] newArray(int size) {
117
+ return new <%= @name %>[size];
118
+ }
119
+ };
120
+ <% end %>
100
121
  }
@@ -0,0 +1,142 @@
1
+ module Jaspion
2
+ module Kilza
3
+ class Java
4
+ class Property < Jaspion::Kilza::Property
5
+
6
+ attr_accessor :gson
7
+ alias :gson? :gson
8
+
9
+ def initialize(name, type, array, key)
10
+ original_name = name
11
+ name = RESERVED_PROPERTY_PREFIX + name unless RESERVED_WORDS.index(name).nil?
12
+ super(name, type, array, key)
13
+
14
+ @original_name = original_name
15
+ @serializable = true
16
+ end
17
+
18
+ def class_name
19
+ class_name = super
20
+ class_name = class_name + RESERVED_CLASS_POSFIX unless RESERVED_WORDS.index(class_name.downcase).nil?
21
+ class_name
22
+ end
23
+
24
+ def constants
25
+ return ' private static final String FIELD_' +
26
+ @name.upcase + ' = "' + @original_name + '";'
27
+ end
28
+
29
+ def read_parcel
30
+ r = ''
31
+ if array?
32
+ if object?
33
+ r = "in.readTypedList(#{@name}, #{class_name}.CREATOR);"
34
+ else
35
+ r = "#{@name} = in.readArrayList(null);"
36
+ end
37
+ elsif object?
38
+ r = "#{@name} = (#{class_name}) in.readParcelable(#{class_name}.class.getClassLoader());"
39
+ elsif boolean?
40
+ r = "#{@name} = in.readByte() != 0;"
41
+ else
42
+ r = "#{@name} = in.read#{type}();"
43
+ end
44
+ r
45
+ end
46
+
47
+ def write_parcel
48
+ r = ''
49
+ if array?
50
+ if object?
51
+ r = "out.writeTypedList(#{@name});"
52
+ else
53
+ r = "out.writeList(#{@name});"
54
+ end
55
+ elsif object?
56
+ r = "out.writeParcelable(#{@name}, flags);"
57
+ elsif boolean?
58
+ r = "out.writeByte((byte) (#{name} ? 1 : 0));"
59
+ else
60
+ r = "out.write#{type}(#{name});"
61
+ end
62
+ r
63
+ end
64
+
65
+ def declaration
66
+ r = ''
67
+ r << " @Expose\n" if gson?
68
+ r << " @SerializedName(FIELD_#{@name.upcase})\n" if gson?
69
+ if array?
70
+ r + " private ArrayList<#{@type}> #{@name};"
71
+ else
72
+ r + " private #{@type} #{@name};"
73
+ end
74
+ end
75
+
76
+ def parse_element
77
+ if object?
78
+ " this.#{@name} = new #{@type}(object.optJSONObject(FIELD_#{@name.upcase}));"
79
+ elsif null?
80
+ " this.#{@name} = object.opt(FIELD_#{@name.upcase});"
81
+ else
82
+ " this.#{@name} = object.opt#{@type}(FIELD_#{@name.upcase});"
83
+ end
84
+ end
85
+
86
+ def parse_array
87
+ r = %( if (object.optJSONArray(FIELD_#{@name.upcase}) != null)
88
+ {
89
+ this.#{@name} = new ArrayList<>();
90
+ JSONArray #{@name}JsonArray = object.optJSONArray(FIELD_#{@name.upcase});
91
+ for (int i = 0; i < #{@name}JsonArray.length(); i++) {
92
+ )
93
+
94
+ if object? || null?
95
+ r = r + " JSONObject #{@name} = #{@name}JsonArray.optJSONObject(i);"
96
+ else
97
+ r = r + " #{@type} #{@name} = #{@name}JsonArray.optJSON#{@type}(i);"
98
+ end
99
+ r = r + %(
100
+ this.#{@name}.add(new #{@type}(#{@name}));
101
+ }
102
+ })
103
+ return r
104
+ end
105
+
106
+ def parse_json
107
+ if array?
108
+ parse_array
109
+ else
110
+ parse_element
111
+ end
112
+ end
113
+
114
+ def setter
115
+ newname = @name.gsub(/_*(.+)/) { $1 }.capitalize
116
+ r = StringIO.new
117
+ if array?
118
+ r << "public void set#{newname}(ArrayList<#{@type}> value) {"
119
+ else
120
+ r << "public void set#{newname}(#{@type} value) {"
121
+ end
122
+ r << "\n this.#{@name} = value;\n }"
123
+ r.string
124
+ end
125
+
126
+ def getter
127
+ newname = @name.gsub(/_*(.+)/) { $1 }.capitalize
128
+ r = StringIO.new
129
+ if array?
130
+ r << "public ArrayList<#{@type}> get#{@name.capitalize}() {"
131
+ elsif boolean?
132
+ r << "public #{@type} is#{newname}() {"
133
+ else
134
+ r << "public #{@type} get#{newname}() {"
135
+ end
136
+ r << "\n return this.#{@name};\n }"
137
+ r.string
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
@@ -1,66 +1,5 @@
1
1
  require 'date'
2
2
 
3
- module Jaspion
4
- module Kilza
5
- class Objc
6
- class Class
7
- include Jaspion::Kilza::Class
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
-
29
- def sources
30
- [code('objc', 'h'), code('objc', 'm')]
31
- end
32
- end
33
- end
34
- end
35
- end
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
-
64
3
  module Jaspion
65
4
  module Kilza
66
5
  # Objective-C Language parser
@@ -78,55 +17,30 @@ module Jaspion
78
17
  struct switch typedef union unsafe_unretained unsigned void,
79
18
  volatile weak while _bool _complex _imaginary sel imp,
80
19
  bool nil yes no self super __strong __weak oneway,
81
- in out inout bycopy byref
20
+ in out inout bycopy byref description
82
21
  )
22
+ TYPES = {
23
+ 'nilclass' => 'id',
24
+ 'string' => 'NSString *',
25
+ 'fixnum' => 'NSNumber *',
26
+ 'float' => 'NSNumber *',
27
+ 'falseclass' => 'NSNumber *',
28
+ 'trueclass' => 'NSNumber *',
29
+ 'hash' => 'NSObject *'
30
+ }
83
31
 
84
32
  def initialize(json_string)
85
33
  super(json_string)
86
34
 
87
- @types = {
88
- 'nilclass' => 'id',
89
- 'string' => 'NSString *',
90
- 'fixnum' => 'NSNumber *',
91
- 'float' => 'NSNumber *',
92
- 'falseclass' => 'NSNumber *',
93
- 'trueclass' => 'NSNumber *',
94
- 'hash' => 'NSObject *'
95
- }
96
-
97
35
  @equal_keys = %w(id identifier uid)
98
36
  end
99
37
 
100
38
  def clazz(name)
101
- name = name + RESERVED_CLASS_POSFIX unless RESERVED_WORDS.index(name.downcase).nil?
102
39
  Jaspion::Kilza::Objc::Class.new(name)
103
40
  end
104
41
 
105
42
  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
-
113
- def classes(class_name)
114
- super(class_name)
115
-
116
- @classes.each do |cl|
117
- cl.properties.each do |pr|
118
- if pr.object? || (pr.array? && pr.null?)
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\"")
124
- end
125
-
126
- pr.type = 'NSMutableArray *' if pr.array?
127
- pr.type = @types[pr.type] unless @types[pr.type].nil?
128
- end
129
- end
43
+ Jaspion::Kilza::Objc::Property.new(name , type, array, key)
130
44
  end
131
45
  end
132
46
  end
@@ -0,0 +1,51 @@
1
+ module Jaspion
2
+ module Kilza
3
+ class Objc
4
+ class Class
5
+ include Jaspion::Kilza::Class
6
+
7
+ def initialize(name)
8
+ name = name + RESERVED_CLASS_POSFIX unless RESERVED_WORDS.index(name.downcase).nil?
9
+ super(name)
10
+ end
11
+
12
+ def push(pr)
13
+ if pr.object? || (pr.array? && pr.null?)
14
+ pr.type = pr.class_name + ' *'
15
+ push_import("#import \"#{pr.class_name}.h\"")
16
+ end
17
+
18
+ pr.type = 'NSMutableArray *' if pr.array?
19
+ unless Jaspion::Kilza::Objc::TYPES[pr.type].nil?
20
+ pr.type = Jaspion::Kilza::Objc::TYPES[pr.type]
21
+ end
22
+
23
+ super(pr)
24
+ end
25
+
26
+ def imports
27
+ @imports.sort.flatten
28
+ end
29
+
30
+ def equals
31
+ r = StringIO.new
32
+ r << '- (BOOL)isEqual:(id)anObject {'
33
+ fields = []
34
+ for pr in @properties
35
+ fields.push("[((#{@name}) anObject).#{pr.name} isEqual:#{pr.name}]") if pr.key?
36
+ end
37
+ r << "\n if (anObject instanceof #{@name}) {"
38
+ r << " return (" + fields.join(" &&\n ") + "});"
39
+ r << "\n }"
40
+ r << "\n return false;"
41
+ r << "\n}"
42
+ r.string
43
+ end
44
+
45
+ def sources
46
+ [code('objc', 'h'), code('objc', 'm')]
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -20,11 +20,11 @@
20
20
  <%= @property.declaration %>
21
21
  <% end %>
22
22
 
23
- + (<%= @name %> *)modelWithDictionary:(NSDictionary *)dict;
24
- + (<%= @name %> *)modelWithString:(NSString *)json;
23
+ + (<%= @name %> * _Nonnull)modelWithDictionary:(NSDictionary * _Nonnull)dict;
24
+ + (<%= @name %> * _Nonnull)modelWithString:(NSString * _Nonnull)json;
25
25
 
26
- - (instancetype)initWithString:(NSString *)json;
27
- - (instancetype)initWithDictionary:(NSDictionary *)dict;
28
- - (NSDictionary *)dictionaryRepresentation;
26
+ - (instancetype _Nonnull)initWithString:(NSString * _Nonnull)json;
27
+ - (instancetype _Nonnull)initWithDictionary:(NSDictionary * _Nonnull)dict;
28
+ - (NSDictionary * _Nonnull)dictionaryRepresentation;
29
29
 
30
30
  @end
@@ -7,7 +7,7 @@
7
7
  //
8
8
 
9
9
  #import "<%= @name %>.h"
10
- <% for @import in @imports %>
10
+ <% for @import in imports %>
11
11
  <%= @import %>
12
12
  <% end %>
13
13
 
@@ -0,0 +1,38 @@
1
+ module Jaspion
2
+ module Kilza
3
+ class Objc
4
+ class Property < Jaspion::Kilza::Property
5
+
6
+ def initialize(name, type, array, key = '')
7
+ original_name = name
8
+ unless RESERVED_WORDS.index(name.downcase).nil?
9
+ name = RESERVED_PROPERTY_PREFIX + name
10
+ end
11
+ super(name, type, array, key)
12
+ @original_name = original_name
13
+ end
14
+
15
+ def class_name
16
+ return if !(object? || (array? && null?))
17
+
18
+ class_name = super
19
+ class_name = class_name + RESERVED_CLASS_POSFIX unless RESERVED_WORDS.index(class_name.downcase).nil?
20
+ class_name
21
+ end
22
+
23
+ def class_reference
24
+ return "@class #{class_name};" unless class_name.nil? || array?
25
+ end
26
+
27
+ def declaration
28
+ "@property (nonatomic, strong, nullable) #{@type} #{@name};"
29
+ end
30
+
31
+ def constants(cl_name)
32
+ "NSString *const k#{cl_name}#{@name.capitalize} = @\"#{@original_name}\";"
33
+ end
34
+
35
+ end
36
+ end
37
+ end
38
+ end
@@ -19,54 +19,28 @@ module Jaspion
19
19
  typealias precedence var prefix required right set
20
20
  type unowned weak id description
21
21
  )
22
+ TYPES = {
23
+ 'nilclass' => 'AnyObject',
24
+ 'string' => 'String',
25
+ 'fixnum' => 'Int',
26
+ 'float' => 'Double',
27
+ 'falseclass' => 'Bool',
28
+ 'trueclass' => 'Bool',
29
+ 'hash' => 'Dictionary'
30
+ }
22
31
 
23
32
  def initialize(json_string)
24
33
  super(json_string)
25
34
 
26
- @types = {
27
- 'nilclass' => 'AnyObject',
28
- 'string' => 'String',
29
- 'fixnum' => 'Int',
30
- 'float' => 'Double',
31
- 'falseclass' => 'Bool',
32
- 'trueclass' => 'Bool',
33
- 'hash' => 'Dictionary'
34
- }
35
-
36
35
  @equal_keys = 'id identifier uid'
37
36
  end
38
37
 
39
38
  def clazz(name)
40
- name = name + RESERVED_CLASS_POSFIX unless RESERVED_WORDS.index(name.downcase).nil?
41
39
  Jaspion::Kilza::Swift::Class.new(name)
42
40
  end
43
41
 
44
42
  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
-
52
- def classes(class_name)
53
- super(class_name)
54
-
55
- @classes.each do |cl|
56
- cl.properties.each do |pr|
57
- if pr.object? || (pr.array? && pr.null?)
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
63
- cl.imports.push("import #{pr.name.capitalize}")
64
- end
65
-
66
- pr.type = @types[pr.type] unless @types[pr.type].nil?
67
- pr.type = "[#{pr.type}]" if pr.array?
68
- end
69
- end
43
+ Jaspion::Kilza::Swift::Property.new(name , type, array, key)
70
44
  end
71
45
  end
72
46
  end
@@ -5,8 +5,20 @@ module Jaspion
5
5
  include Jaspion::Kilza::Class
6
6
 
7
7
  def initialize(name)
8
+ name = name + RESERVED_CLASS_POSFIX unless RESERVED_WORDS.index(name.downcase).nil?
8
9
  super(name)
9
- @name = @name + RESERVED_CLASS_POSFIX unless RESERVED_WORDS.index(name.downcase).nil?
10
+ end
11
+
12
+ def push(pr)
13
+ if pr.object? || (pr.array? && pr.null?)
14
+ pr.type = pr.class_name
15
+ push_import("import #{pr.class_name}")
16
+ end
17
+
18
+ pr.type = Jaspion::Kilza::Swift::TYPES[pr.type] unless Jaspion::Kilza::Swift::TYPES[pr.type].nil?
19
+ pr.type = "[#{pr.type}]" if pr.array?
20
+
21
+ super(pr)
10
22
  end
11
23
 
12
24
  def sources
@@ -4,9 +4,22 @@ module Jaspion
4
4
  module Kilza
5
5
  class Swift
6
6
  class Property < Jaspion::Kilza::Property
7
+
8
+ def initialize(name, type, array, key = '')
9
+ original_name = name
10
+ unless RESERVED_WORDS.index(name.downcase).nil?
11
+ name = RESERVED_PROPERTY_PREFIX + name
12
+ end
13
+ super(name, type, array, key)
14
+ @original_name = original_name
15
+ end
16
+
7
17
  def class_name
8
- return if !object? || array?
9
- Jaspion::Kilza::Swift::Class.new(@original_name).name
18
+ return if !(object? || null? || (array? && null?))
19
+
20
+ class_name = super
21
+ class_name = class_name + RESERVED_CLASS_POSFIX unless RESERVED_WORDS.index(class_name.downcase).nil?
22
+ class_name
10
23
  end
11
24
 
12
25
  def constants(cl_name)
@@ -58,16 +58,15 @@ public class <%= @name %>: NSObject, NSCoding {
58
58
  <% if @property.object? || @property.null? %>
59
59
  <% if @property.array? %>
60
60
  if let obj<%= @property.name.capitalize %>: [AnyObject] = dict[<%= @name %>.k<%= @name %><%= @property.name.capitalize %>] as? [AnyObject] {
61
-
62
61
  var list<%= @property.name.capitalize %> = <%= @property.type %>()
63
- for item in obj<%= @property.name.capitalize %> {
62
+ for item in obj<%= @property.name.capitalize %> {
64
63
  if item is Dictionary<String, AnyObject> {
65
- list<%= @property.name.capitalize %>.append(<%= @property.name.capitalize %>.model(item))
64
+ if let obj = <%= @property.class_name %>.model(item) {
65
+ list<%= @property.name.capitalize %>.append(obj)
66
+ }
66
67
  }
67
68
  }
68
69
  self.<%= @property.name %> = list<%= @property.name.capitalize %>
69
- } else {
70
- return nil
71
70
  }
72
71
  <% else %>
73
72
  self.<%= @property.name %> = <%= @property.class_name %>.model(dict[<%= @name %>.k<%= @name %><%= @property.name.capitalize %>]!)
@@ -26,8 +26,8 @@ module Jaspion
26
26
  attr_accessor :key
27
27
  alias_method :key?, :key
28
28
 
29
- def initialize(name, type, array, key)
30
- @name = Kilza.normalize(name)
29
+ def initialize(name, type, array, key = '')
30
+ @name = Jaspion::Kilza::Property.normalize(name)
31
31
  @original_name = name
32
32
  @type = type
33
33
  @array = array
@@ -59,6 +59,12 @@ module Jaspion
59
59
  @name == pr.name
60
60
  end
61
61
 
62
+ # If this Property represents a new Class,
63
+ # it returns the formatted class name
64
+ def class_name
65
+ Jaspion::Kilza::Class.normalize(@original_name)
66
+ end
67
+
62
68
  def to_s
63
69
  {
64
70
  name: @name,
@@ -67,6 +73,29 @@ module Jaspion
67
73
  array?: @array
68
74
  }.to_s
69
75
  end
76
+
77
+ # Removes everything except numbers and letters.
78
+ #
79
+ # @param str [String] string to be cleaned
80
+ #
81
+ # @return [String] cleaned string
82
+ def self.clean(str)
83
+ return if str.nil?
84
+ str.gsub(/[^a-zA-Z0-9]/, '')
85
+ end
86
+
87
+ # Cleans the string and make it lowercase.
88
+ #
89
+ # @param str [String] string to be cleaned
90
+ #
91
+ # @return [String] cleaned string
92
+ def self.normalize(str)
93
+ return if str.nil?
94
+ str = str.gsub(/[^a-zA-Z0-9]/, '_')
95
+ str = '_' if str.length == 0
96
+ str = '_' + str if str[0].number?
97
+ str.downcase
98
+ end
70
99
  end
71
100
  end
72
101
  end
@@ -1,6 +1,6 @@
1
1
  # Tranforms a JSON string into Objects
2
2
  module Jaspion
3
3
  module Kilza
4
- VERSION = '1.1.1'
4
+ VERSION = '1.1.2'
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.1
4
+ version: 1.1.2
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-15 00:00:00.000000000 Z
11
+ date: 2016-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -162,10 +162,14 @@ files:
162
162
  - lib/jaspion/kilza/class.rb
163
163
  - lib/jaspion/kilza/language.rb
164
164
  - lib/jaspion/kilza/language/java.rb
165
+ - lib/jaspion/kilza/language/java/class.rb
165
166
  - lib/jaspion/kilza/language/java/java.erb
167
+ - lib/jaspion/kilza/language/java/property.rb
166
168
  - lib/jaspion/kilza/language/objc.rb
169
+ - lib/jaspion/kilza/language/objc/class.rb
167
170
  - lib/jaspion/kilza/language/objc/h.erb
168
171
  - lib/jaspion/kilza/language/objc/m.erb
172
+ - lib/jaspion/kilza/language/objc/property.rb
169
173
  - lib/jaspion/kilza/language/swift.rb
170
174
  - lib/jaspion/kilza/language/swift/class.rb
171
175
  - lib/jaspion/kilza/language/swift/property.rb