ObjCGenerator 0.0.3 → 0.0.4

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: fb8e1464215c49253200d99f93ac52b332aa580e
4
- data.tar.gz: ce29a585676bc6e3ef8a9d3f529b1324f6c91e5a
3
+ metadata.gz: c359c27e87442b2ffc7da956fce1be4f153016d2
4
+ data.tar.gz: 6578fafc4cbd591a7222e6f5afefb863142eaf9e
5
5
  SHA512:
6
- metadata.gz: 989e408170733ec058d339340534df1287c4d144fe48ded4aa3787459055f4d9813ef0e792cbd657eb4df0699c70b6a065fc55cf0a1679ad6acf3ca23bcf5f6c
7
- data.tar.gz: b156ddb9e3378287a67bcbca25261865a13839e0d26e913c4428c2e395248f2207cdae52c231a61983f1953bf846f04fec32aaa973c02e6c2b75f400140b4387
6
+ metadata.gz: 4881b3b395116a29d08f411a3616cb849d4c7bea04d83c0f496b09966b697b6fca5105960d45068b7937f3683f041799318a0686ea79f7621aa58e92974169e0
7
+ data.tar.gz: 53f181dfc16d4783a2a4b4ccc5ed5fa93f0c5cc38dc181f9ee38200ba6f082eb256279dfefa4ea511ceaeda64543bad56860773bd4bbee11611eb59eea35a990
data/bin/objcgenerator CHANGED
@@ -3,19 +3,32 @@
3
3
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
4
 
5
5
  require 'ObjCGenerator'
6
+ require 'rubygems'
7
+ require 'commander/import'
6
8
 
7
- if (ARGV[0].nil?) then
8
- if (STDIN.tty?)
9
- puts "Usage: objcgenerator <finame>"
10
- puts "Usage: cat <filename> | objcgenerator"
11
- exit
12
- end
13
- input=$<
14
- else
15
- input=File.new(ARGV[0],"r");
16
- end
17
- str = (STDIN.tty?) ? 'not reading from stdin' : $stdin.read
9
+ program :version, ObjCGenerator::VERSION
10
+ program :description, 'Generate Objective-C code from a JSON description of classes.'
11
+
12
+ command :generate do |c|
13
+ c.syntax = 'objgenerator generate [options]'
14
+ c.summary = ''
15
+ c.description = ''
16
+ c.example 'description', 'command example'
17
+ c.option '--input STRING', String, 'The path oth the JSON desctription'
18
+ c.option '--output STRING', String, 'The output path where the obj-c code will be generated'
18
19
 
19
- json = input.read
20
+ c.action do |args, options|
21
+ options.default :output => '.'
20
22
 
21
- ObjCGenerator::Command.run json, Dir.pwd
23
+ if (options.input.nil? or options.output.nil?)
24
+ puts "Inavalid format:"
25
+ puts "objgenerator generate --input <file> --output <path>"
26
+ else
27
+ input=File.new(options.input,"r");
28
+ json = input.read
29
+ command = ObjCGenerator::Command.new
30
+
31
+ command.run json, options.output
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,140 @@
1
+
2
+ module ObjCGenerator
3
+
4
+ class ClassInterfaceGenerator
5
+ def generate_incipit()
6
+ author = "Ignazioc"
7
+
8
+ incipit_template = File.read(template_path('incipit'))
9
+ ERB.new(incipit_template).result(binding)
10
+ end
11
+
12
+ def class_implementation (class_name, vars)
13
+
14
+ incipit = generate_incipit()
15
+
16
+ @import_other_classes = generate_import_other_classes(vars)
17
+ @init_method = generate_init_method(vars)
18
+ @init_with_dict_method = generate_init_with_dict_method(vars)
19
+ @class_init_with_dict = generate_class_init_with_dict(class_name)
20
+ @to_dict_method = generate_to_dict_method(vars)
21
+ @is_equals_method = generate_is_equals_method(class_name)
22
+ @is_equals_to_Object_method = generate_is_equals_to_Object_method(class_name, vars)
23
+ @description_method = generate_description_method( vars)
24
+ @copy_method = generate_copy_method( class_name, vars)
25
+ @hash_method = generate_hash_method( class_name, vars)
26
+
27
+ class_implementation = File.read(template_path('class_implementation'))
28
+ return incipit + ERB.new(class_implementation).result(binding)
29
+ end
30
+
31
+ def generate_import_other_classes vars
32
+ custom_objects = vars.select { | var | var.is_a? TypeCustomObject }
33
+ custom_objects.inject("") { |mem, var| mem << "#import \"#{var.var_type}.h\"" + "\n"}
34
+ end
35
+
36
+ def generate_init_method vars
37
+ @init_rows = vars.map { |var | "_#{var.varname} = #{var.default_value};" }
38
+ @init_rows = ObjCGenerator::vertical_align_vars @init_rows, /(=)/, 0
39
+
40
+ init_method = File.read(template_path('method_init'))
41
+ return ERB.new(init_method).result(binding)
42
+ end
43
+
44
+ def generate_init_with_dict_method vars
45
+ @initWithDictRows = vars.map { |var|
46
+ conversion_type = var.conversion_value("val")
47
+ "if ((val = dict[@\"#{var.varname}\"])) { _#{var.varname} = #{conversion_type}; } else { NSLog(@\"Error initWithDict: Unable to find: #{var.varname}\"); }"
48
+ }
49
+ @initWithDictRows = ObjCGenerator::vertical_align_vars @initWithDictRows, /(\)\))/, 1
50
+ @initWithDictRows = ObjCGenerator::vertical_align_vars @initWithDictRows, /( = )/, 2
51
+ @initWithDictRows = ObjCGenerator::vertical_align_vars @initWithDictRows, /( } )/, 0
52
+
53
+ init_with_dict_method = File.read(template_path('method_init_with_dictionary'))
54
+ return ERB.new(init_with_dict_method).result(binding)
55
+
56
+ end
57
+
58
+ def generate_class_init_with_dict class_name
59
+ @lowercaseClassName = class_name.dup
60
+ @lowercaseClassName[0] = @lowercaseClassName[0].chr.downcase
61
+
62
+ template = File.read(template_path('method_class_init_with_dict'))
63
+ return ERB.new(template).result(binding)
64
+ end
65
+
66
+ def generate_to_dict_method vars
67
+ @to_ditc_rows = vars.map { |var| var.to_dictionary_item + ","}
68
+ @to_ditc_rows[-1] = @to_ditc_rows[-1][0...-1]
69
+ @to_ditc_rows = ObjCGenerator::vertical_align_vars @to_ditc_rows, /( : )/, 0
70
+ @to_ditc_rows = ObjCGenerator::vertical_align_vars @to_ditc_rows, /( \?: )/, 0
71
+
72
+ template = File.read(template_path('method_to_dict'))
73
+ return ERB.new(template).result(binding)
74
+ end
75
+
76
+ def generate_is_equals_method class_name
77
+ @class_name = class_name
78
+ template = File.read(template_path('method_is_equal'))
79
+ return ERB.new(template).result(binding)
80
+ end
81
+
82
+ def generate_is_equals_to_Object_method (class_name, vars)
83
+ @class_name = class_name
84
+
85
+ @is_equal_rows = vars.map { |var| "if (#{var.inEquality_test("other")}) return NO;" }
86
+ @is_equal_rows = ObjCGenerator::vertical_align_vars @is_equal_rows, /( )/, 0
87
+ @is_equal_rows = ObjCGenerator::vertical_align_vars @is_equal_rows, /(\) )/, 0
88
+
89
+ template = File.read(template_path('method_is_equal_to'))
90
+ return ERB.new(template).result(binding)
91
+ end
92
+
93
+ def generate_description_method vars
94
+ @description_rows = vars.map { |var| "[NSString stringWithFormat:#{var.description_row}]," }
95
+ @description_rows[-1] = @description_rows[-1][0...-1]
96
+ @description_rows = ObjCGenerator::vertical_align_vars @description_rows, /( = )/, 0
97
+ @description_rows = ObjCGenerator::vertical_align_vars @description_rows, /( , )/, 0
98
+
99
+ template = File.read(template_path('method_description'))
100
+ return ERB.new(template).result(binding)
101
+ end
102
+
103
+
104
+ def generate_copy_method(class_name, vars)
105
+ @lowercaseClassName = class_name.dup
106
+ @lowercaseClassName[0] = @lowercaseClassName[0].chr.downcase
107
+ newName = "#{@lowercaseClassName}Copy"
108
+ @copy_rows = vars.map { |var| var.copyrow(newName) }
109
+ @copy_rows = ObjCGenerator::vertical_align_vars @copy_rows, /( = )/, 0
110
+
111
+ @class_name = class_name
112
+ @varname = newName
113
+
114
+ template = File.read(template_path('method_copy'))
115
+ return ERB.new(template).result(binding)
116
+ end
117
+
118
+
119
+ def generate_hash_method(class_name, vars)
120
+ @hash_rows = vars.map { |var| "#{var.hash_row};" }
121
+ template = File.read(template_path('method_hash'))
122
+
123
+ return ERB.new(template).result(binding)
124
+ end
125
+
126
+ end
127
+
128
+ def self.vertical_align_vars (strings , regexp, index)
129
+ max_lengt = strings.map { | str|
130
+ str.split(regexp)[0..index].join.length
131
+ }.max
132
+
133
+ result = strings.map { |str|
134
+ arr = str.split(regexp)
135
+ arr[0..index].join.ljust(max_lengt , ' ') + arr[index+1..arr.length].join
136
+ }
137
+ result
138
+ end
139
+
140
+ end
@@ -0,0 +1,53 @@
1
+ module ObjCGenerator
2
+ class ClassInterfaceGenerator
3
+
4
+ def template_path filename
5
+ File.join(File.expand_path("../..", File.dirname(__FILE__)), "templates/#{filename}.erb")
6
+ end
7
+
8
+ def generate_incipit()
9
+ author = "Ignazioc"
10
+
11
+ incipit_template = File.read(template_path('incipit'))
12
+ ERB.new(incipit_template).result(binding)
13
+ end
14
+
15
+ def generate_class_header (class_name, variables)
16
+ incipit = generate_incipit()
17
+
18
+ @class_predefinitions = generate_import_classes(variables)
19
+ @class_name = "@interface #{class_name} : NSObject <NSCopying>"
20
+ @class_properties = generate_class_properties(variables)
21
+ @class_methods = generate_class_methods(class_name)
22
+
23
+ header_template = File.read(template_path('class_header'))
24
+ class_header = ERB.new(header_template).result(binding)
25
+
26
+ return incipit + class_header
27
+
28
+ end
29
+ def generate_class_properties(variables)
30
+ result = []
31
+ result = variables.map { |var| var.property_definition() }
32
+
33
+ result = ObjCGenerator::vertical_align_vars( result, /( )/, 4)
34
+ result = result.intersperse("\n")
35
+
36
+ result.inject("") { |mem, var| mem << var }
37
+ end
38
+
39
+ def generate_class_methods class_name
40
+ lowercase_class_name = class_name.dup
41
+ lowercase_class_name[0] = lowercase_class_name[0].chr.downcase
42
+
43
+ methods_definition_template = File.read(template_path('methods_definition'))
44
+ methods_definition = ERB.new(methods_definition_template).result(binding)
45
+
46
+ end
47
+
48
+ def generate_import_classes variables
49
+ custom_objects = variables.select { | var | var.is_a? TypeCustomObject }
50
+ custom_objects.inject("") { |mem, var| mem << "@class #{var.var_type};" + "\n"}
51
+ end
52
+ end
53
+ end
@@ -1,54 +1,7 @@
1
1
  module ObjCGenerator
2
2
  class Command
3
3
 
4
- def self.class_header (class_name, vars)
5
- @text = "#import <Foundation/Foundation.h>" + "\n" +
6
- "" + "\n" +
7
- "#{import_classes vars}" + "\n" +
8
- "@interface #{class_name} : NSObject <NSCopying>" + "\n" +
9
- "" + "\n" +
10
- "#{class_properties vars}" + "\n" +
11
- "" + "\n" +
12
- "#{class_methods class_name}" + "\n" +
13
- "" + "\n" +
14
- "@end" + "\n" +
15
- "" + "\n" +
16
- ""
17
- end
18
- def self.class_properties vars
19
- result = []
20
-
21
- vars.each do |var|
22
- result << var.property_definition
23
- end
24
-
25
- result = ObjCGenerator::vertical_align_vars result, /( )/, 4
26
- result = result.intersperse("\n")
27
-
28
- result.inject("") { |mem, var| mem << var }
29
- end
30
-
31
- def self.class_methods class_name
32
- t = class_name.dup
33
- t[0] = t[0].chr.downcase
34
- @text = "- (instancetype)init;" + "\n" +
35
- "- (instancetype)initWithDict:(NSDictionary *)dict;" + "\n" +
36
- "+ (instancetype)#{t}WithDict:(NSDictionary *)dict;" + "\n" +
37
- "- (NSDictionary *)toDict;" + "\n" +
38
- "" + "\n" +
39
- "- (BOOL)isEqual:(id)object;" + "\n" +
40
- "- (BOOL)isEqualTo#{class_name}:(#{class_name} *)other;" + "\n" +
41
- "- (NSUInteger)hash;" + "\n" +
42
- "- (NSString *)description;" + "\n" +
43
- "- (instancetype)copyWithZone:(NSZone *)zone;"
44
- end
45
-
46
- def self.import_classes vars
47
- custom_objects = vars.select { | var | var.is_a? TypeCustomObject }
48
- custom_objects.inject("") { |mem, var| mem << "@class #{var.var_type};" + "\n"}
49
- end
50
-
51
- def self.run input_file, output_dir
4
+ def run input_file, output_dir
52
5
 
53
6
  parsed = JSON.parse(input_file)
54
7
 
@@ -67,23 +20,25 @@ module ObjCGenerator
67
20
  TypeString.new(hash["name"])
68
21
  when "Date"
69
22
  TypeDate.new(hash["name"])
23
+ when "Array"
24
+ TypeArray.new(hash["name"], hash["subType"])
70
25
  else
71
26
  TypeCustomObject.new(hash["name"], hash["type"])
72
27
 
73
28
  end
74
29
  }
75
30
 
76
- header = class_header var["name"], vars
31
+ g = ClassInterfaceGenerator.new()
32
+ gc = ClassInterfaceGenerator.new()
33
+
34
+ header = g.generate_class_header var["name"], vars
77
35
  File.open( output_dir + "/#{var["name"]}.h", 'w') { |file| file.write(header) }
78
36
 
79
- implementation = ObjCGenerator::class_implementation var["name"], vars
37
+ implementation = gc.class_implementation var["name"], vars
80
38
  File.open( output_dir + "/#{var["name"]}.m", 'w') { |file| file.write(implementation) }
81
39
 
82
40
  end
83
41
  end
84
-
85
-
86
-
87
42
  end
88
43
  end
89
44
  end
@@ -24,6 +24,9 @@ class ObjCType
24
24
  def copyrow newVarName
25
25
  "-subcalss-"
26
26
  end
27
+ def hash_row
28
+ "-subcalss-"
29
+ end
27
30
  end
28
31
 
29
32
  class TypeBool < ObjCType
@@ -50,6 +53,10 @@ class TypeBool < ObjCType
50
53
  def copyrow newVarName
51
54
  "#{newVarName}.#{self.varname} = self.#{self.varname};"
52
55
  end
56
+ def hash_row
57
+ "(self.#{self.varname}?1231:1237);"
58
+ end
59
+
53
60
  end
54
61
 
55
62
  class TypeInt < ObjCType
@@ -75,6 +82,9 @@ class TypeInt < ObjCType
75
82
  def copyrow newVarName
76
83
  "#{newVarName}.#{self.varname} = self.#{self.varname};"
77
84
  end
85
+ def hash_row
86
+ "self.#{self.varname};"
87
+ end
78
88
  end
79
89
 
80
90
  class TypeFloat < ObjCType
@@ -100,6 +110,9 @@ class TypeFloat < ObjCType
100
110
  def copyrow newVarName
101
111
  "#{newVarName}.#{self.varname} = self.#{self.varname};"
102
112
  end
113
+ def hash_row
114
+ "self.#{self.varname};" #is rounded
115
+ end
103
116
  end
104
117
 
105
118
  class TypeString < ObjCType
@@ -125,6 +138,9 @@ class TypeString < ObjCType
125
138
  def copyrow newVarName
126
139
  "#{newVarName}.#{self.varname} = [self.#{self.varname} copy];"
127
140
  end
141
+ def hash_row
142
+ "[self.#{self.varname} hash];"
143
+ end
128
144
  end
129
145
 
130
146
  class TypeDate < ObjCType
@@ -150,6 +166,9 @@ class TypeDate < ObjCType
150
166
  def copyrow newVarName
151
167
  "#{newVarName}.#{self.varname} = [self.#{self.varname} copy];"
152
168
  end
169
+ def hash_row
170
+ "[self.#{self.varname} hash];"
171
+ end
153
172
  end
154
173
 
155
174
 
@@ -169,7 +188,7 @@ class TypeCustomObject < ObjCType
169
188
  end
170
189
  def conversion_value origin
171
190
  # "[#{origin} description]"
172
- "[[SampleClass2 alloc] initWithDict:#{origin}]"
191
+ "[[#{var_type} alloc] initWithDict:#{origin}]"
173
192
  end
174
193
  def to_dictionary_item
175
194
  "@\"#{@varname}\" : [self.#{@varname} toDict] ?: @{}"
@@ -183,4 +202,44 @@ class TypeCustomObject < ObjCType
183
202
  def copyrow newVarName
184
203
  "#{newVarName}.#{self.varname} = [self.#{self.varname} copyWithZone:nil];"
185
204
  end
205
+ def hash_row
206
+ "[self.#{self.varname} hash];"
207
+ end
208
+ end
209
+
210
+
211
+ ###############################################################################
212
+ ## A R R A Y ##
213
+ ###############################################################################
214
+
215
+ class TypeArray < ObjCType
216
+ attr_accessor :varname
217
+ attr_accessor :var_sub_type
218
+ def initialize( name , sub_type)
219
+ super(name)
220
+
221
+ @var_sub_type = sub_type
222
+ end
223
+
224
+ def property_definition
225
+ "@property (nonatomic) NSArray *#{@varname}; //@[#{var_sub_type}]"
226
+ end
227
+ def default_value
228
+ "@[]"
229
+ end
230
+ def conversion_value origin
231
+ "[NSArray arrayWithArray:#{origin}]"
232
+ end
233
+ def to_dictionary_item
234
+ "@\"#{@varname}\" : self.#{@varname} ?: @[]"
235
+ end
236
+ def inEquality_test other
237
+ "![self.#{self.varname} isEqual:#{other}.#{self.varname}]"
238
+ end
239
+ def description_row
240
+ "@\"self.#{self.varname} = %@\" , self.#{self.varname}"
241
+ end
242
+ def copyrow newVarName
243
+ "#{newVarName}.#{self.varname} = [self.#{self.varname} copyWithZone:nil];"
244
+ end
186
245
  end
@@ -1,3 +1,3 @@
1
1
  module ObjCGenerator
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/ObjCGenerator.rb CHANGED
@@ -1,9 +1,13 @@
1
1
  require "json"
2
2
  require "thor"
3
+ require 'erb'
4
+
3
5
  require "ObjCGenerator/version"
6
+ require "ObjCGenerator/class_interface_generator"
7
+ require "ObjCGenerator/class_implementation_generator"
4
8
  require "ObjCGenerator/command"
5
9
  require "ObjCGenerator/types"
6
- require "ObjCGenerator/class_implementation"
10
+
7
11
 
8
12
  module Enumerable
9
13
  def intersperse(obj=nil)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ObjCGenerator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ignazio Calò
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-29 00:00:00.000000000 Z
11
+ date: 2015-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: commander
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  description:
42
56
  email:
43
57
  - ignazioc@gmail.com
@@ -46,10 +60,10 @@ executables:
46
60
  extensions: []
47
61
  extra_rdoc_files: []
48
62
  files:
49
- - bin/api_desc.json
50
63
  - bin/objcgenerator
51
64
  - lib/ObjCGenerator.rb
52
- - lib/ObjCGenerator/class_implementation.rb
65
+ - lib/ObjCGenerator/class_implementation_generator.rb
66
+ - lib/ObjCGenerator/class_interface_generator.rb
53
67
  - lib/ObjCGenerator/command.rb
54
68
  - lib/ObjCGenerator/types.rb
55
69
  - lib/ObjCGenerator/version.rb
data/bin/api_desc.json DELETED
@@ -1,38 +0,0 @@
1
- [
2
- {
3
- "name": "SampleClass2",
4
- "type": "Class",
5
- "vars": [
6
- {
7
- "name": "someStr",
8
- "type": "String"
9
- }
10
- ]
11
- },
12
- {
13
- "name": "SampleClass",
14
- "type": "Class",
15
- "vars": [
16
- {
17
- "name": "someBool",
18
- "type": "Bool"
19
- },
20
- {
21
- "name": "someInt",
22
- "type": "Int"
23
- },
24
- {
25
- "name": "someFloat",
26
- "type": "Float"
27
- },
28
- {
29
- "name": "someStr",
30
- "type": "String"
31
- },
32
- {
33
- "name": "someClass2",
34
- "type": "SampleClass2"
35
- }
36
- ]
37
- }
38
- ]
@@ -1,208 +0,0 @@
1
-
2
- module ObjCGenerator
3
-
4
- def self.vertical_align_vars (strings , regexp, index)
5
- max_lengt = strings.map { | str|
6
- str.split(regexp)[0..index].join.length
7
- }.max
8
-
9
- result = strings.map { |str|
10
- arr = str.split(regexp)
11
- arr[0..index].join.ljust(max_lengt , ' ') + arr[index+1..arr.length].join
12
- }
13
- result
14
- end
15
-
16
- def self.class_implementation (class_name, vars)
17
- text = "#import \"#{class_name}.h\"" + "\n" +
18
- "#{import_other_classes vars}" + "\n" +
19
- "@implementation #{class_name}" + "\n" +
20
- "" + "\n" +
21
- "#{init_method vars}" + "\n" +
22
- "" + "\n" +
23
- "#{init_with_dict_method vars}" + "\n" +
24
- "" + "\n" +
25
- "#{class_init_with_dict class_name}" + "\n" +
26
- "" + "\n" +
27
- "#{to_dict_method vars}" + "\n" +
28
- "" + "\n" +
29
- "#{is_equals_method class_name}" + "\n" +
30
- "" + "\n" +
31
- "#{is_equals_to_Object_method class_name, vars}" + "\n" +
32
- "" + "\n" +
33
- "#{hash_method}" + "\n" +
34
- "" + "\n" +
35
- "#{description_method vars}" + "\n" +
36
- "" + "\n" +
37
- "#{copy_method class_name, vars}" + "\n" +
38
- "" + "\n" +
39
- "@end" + "\n" +
40
- ""
41
- end
42
-
43
- def self.import_other_classes vars
44
- custom_objects = vars.select { | var | var.is_a? TypeCustomObject }
45
- custom_objects.inject("") { |mem, var| mem << "#import \"#{var.var_type}.h\"" + "\n"}
46
- end
47
-
48
- def self.copy_method(class_name, vars)
49
- t = class_name.dup
50
- t[0] = t[0].chr.downcase
51
- newName = "#{t}Copy"
52
- text = "- (instancetype)copyWithZone:(NSZone *)zone {" + "\n" +
53
- " #{class_name} *#{newName} = [[[self class] allocWithZone:zone] init];" + "\n" +
54
- " #{copy_rows vars, newName}" + "\n" +
55
- " return #{newName};" + "\n" +
56
- "}"
57
- end
58
-
59
- def self.copy_rows vars, newVarName
60
- result = []
61
- vars.each do |var|
62
- r = var.copyrow newVarName
63
- result << r
64
- end
65
-
66
- result = vertical_align_vars result, /( = )/, 0
67
- # result = vertical_align_vars result, /( , )/, 0
68
- result = result.intersperse("\n ")
69
- result.inject("") { |mem, var| mem << var }
70
- end
71
-
72
- def self.description_method vars
73
- text = "- (NSString *)description {" + "\n" +
74
- " NSString *params = [@[" + "\n" +
75
- " #{description_rows vars}" + "\n" +
76
- " ] componentsJoinedByString:@\", \"" + "\n" +
77
- " ];" + "\n" +
78
- " return [NSString stringWithFormat:@\"%@ { %@ }\", NSStringFromClass([self class]), params];" + "\n" +
79
- "}"
80
- end
81
-
82
- def self.description_rows vars
83
- result = []
84
- vars.each do |var|
85
- r = "[NSString stringWithFormat:#{var.description_row}]"
86
- result << r
87
- end
88
-
89
- result = vertical_align_vars result, /( = )/, 0
90
- result = vertical_align_vars result, /( , )/, 0
91
- result = result.intersperse("\n , ")
92
- result.inject("") { |mem, var| mem << var }
93
- end
94
-
95
- def self.hash_method
96
- text = "- (NSUInteger)hash {" + "\n" +
97
- " return [[self description] hash];" + "\n" +
98
- "}"
99
- end
100
-
101
- def self.is_equals_method class_name
102
- text = "- (BOOL)isEqual:(id)object {" + "\n" +
103
- " if (self == object) { return true; }" + "\n" +
104
- " else if ([object isKindOfClass:[self class]]) {" + "\n" +
105
- " return [self isEqualTo#{class_name}:object];" + "\n" +
106
- " }" + "\n" +
107
- " else { return NO; }" + "\n" +
108
- "}"
109
- end
110
-
111
- def self.is_equals_to_Object_method (class_name, vars)
112
- text = "- (BOOL)isEqualTo#{class_name}:(#{class_name} *)other {" + "\n" +
113
- " if (self == other) return YES;" + "\n" +
114
- " #{equality_rows vars}" + "\n" +
115
- " return YES;" + "\n" +
116
- "}"
117
- end
118
-
119
- def self.equality_rows vars
120
- result = []
121
- vars.each do |var|
122
- r = "if (#{var.inEquality_test("other")}) return NO;"
123
- result << r
124
- end
125
-
126
- result = vertical_align_vars result, /( )/, 0
127
- result = vertical_align_vars result, /(\) )/, 0
128
- result = result.intersperse("\n ")
129
- result.inject("") { |mem, var| mem << var }
130
- end
131
-
132
- def self.to_dict_method vars
133
- text = "- (NSDictionary *)toDict {" + "\n" +
134
- " return @{" + "\n" +
135
- " #{value_to_dict vars}" + "\n" +
136
- " };" + "\n" +
137
- "}"
138
- end
139
-
140
- def self.value_to_dict vars
141
- result = []
142
- vars.each do |var|
143
- r = var.to_dictionary_item
144
- result << r
145
- end
146
- result = vertical_align_vars result, /( : )/, 0
147
- result = vertical_align_vars result, /( \?: )/, 0
148
- result = result.intersperse("\n , ")
149
-
150
- result.inject("") { |mem, var| mem << var }
151
- end
152
-
153
- def self.class_init_with_dict class_name
154
- t = class_name.dup
155
- t[0] = t[0].chr.downcase
156
- text = "+ (instancetype)#{t}WithDict:(NSDictionary *)dict {" + "\n" +
157
- " return [[self alloc] initWithDict:dict];" + "\n" +
158
- "}"
159
- end
160
-
161
- def self.init_with_dict_method vars
162
- text = "- (instancetype)initWithDict:(NSDictionary *)dict {" + "\n" +
163
- " if ((self = [self init])) {" + "\n" +
164
- " id val = nil;" + "\n" +
165
- " #{init_values vars}" + "\n" +
166
- " }" + "\n" +
167
- " return self;" + "\n" +
168
- "}"
169
- end
170
-
171
- def self.init_values vars
172
- result = []
173
- vars.each do |var|
174
- conversion_type = var.conversion_value("val")
175
- result << "if ((val = dict[@\"#{var.varname}\"])) { _#{var.varname} = #{conversion_type}; } else { NSLog(@\"Error initWithDict: Unable to find: #{var.varname}\"); }"
176
- end
177
- result = vertical_align_vars result, /(\)\))/, 1
178
- result = vertical_align_vars result, /( = )/, 2
179
- result = vertical_align_vars result, /( } )/, 0
180
- result = result.intersperse("\n ")
181
-
182
- result.inject("") { |mem, var| mem << var }
183
- end
184
-
185
-
186
- def self.init_method vars
187
- text = "- (instancetype)init {" + "\n" +
188
- " if ((self = [super init])) {" + "\n" +
189
- "#{default_values vars}" + "\n" +
190
- " }" + "\n" +
191
- " return self;" + "\n" +
192
- "}"
193
-
194
- end
195
-
196
-
197
- def self.default_values vars
198
- result = []
199
- vars.each do |var|
200
- result << " _#{var.varname} = #{var.default_value};"
201
- end
202
- result = vertical_align_vars result, /(=)/, 0
203
- result = result.intersperse("\n")
204
-
205
- result.inject("") { |mem, var| mem << var }
206
- end
207
-
208
- end