ObjCGenerator 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/api_desc.json +14 -0
- data/bin/objcgenerator +12 -3
- data/lib/ObjCGenerator/class_implementation.rb +8 -3
- data/lib/ObjCGenerator/command.rb +11 -14
- data/lib/ObjCGenerator/types.rb +33 -0
- data/lib/ObjCGenerator/version.rb +1 -1
- data/lib/ObjCGenerator.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d768d15ac8526021090cfaad4832799fde7da34c
|
4
|
+
data.tar.gz: b63d02360bee09e90fb97955b2b9fb10153c560f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1a114f4e0130ab281d8fa14cd8516bbf6c4f75bc7ea6d3351a6dca99bc4457e9041f2dcafed0f0d080bd5c4a16a64742aebfaad25c7c3f13c9468f28a9af151c
|
7
|
+
data.tar.gz: edbca3206860cd97ad9c040f8454536780506be26491b60acbfe8c92fb7819080256536eb5e174923047a48bb87b41150a421dc5fdac10920c9755b4456eab7e
|
data/bin/api_desc.json
CHANGED
@@ -1,4 +1,14 @@
|
|
1
1
|
[
|
2
|
+
{
|
3
|
+
"name": "SampleClass2",
|
4
|
+
"type": "Class",
|
5
|
+
"vars": [
|
6
|
+
{
|
7
|
+
"name": "someStr",
|
8
|
+
"type": "String"
|
9
|
+
}
|
10
|
+
]
|
11
|
+
},
|
2
12
|
{
|
3
13
|
"name": "SampleClass",
|
4
14
|
"type": "Class",
|
@@ -18,6 +28,10 @@
|
|
18
28
|
{
|
19
29
|
"name": "someStr",
|
20
30
|
"type": "String"
|
31
|
+
},
|
32
|
+
{
|
33
|
+
"name": "someClass2",
|
34
|
+
"type": "SampleClass2"
|
21
35
|
}
|
22
36
|
]
|
23
37
|
}
|
data/bin/objcgenerator
CHANGED
@@ -4,9 +4,18 @@ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
|
4
4
|
|
5
5
|
require 'ObjCGenerator'
|
6
6
|
|
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
|
7
18
|
|
8
|
-
|
9
|
-
file = open(filename)
|
10
|
-
json = file.read
|
19
|
+
json = input.read
|
11
20
|
|
12
21
|
ObjCGenerator::Command.run json, Dir.pwd
|
@@ -14,8 +14,8 @@ module ObjCGenerator
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def self.class_implementation (class_name, vars)
|
17
|
-
text = "#import \"#{class_name}.h\""
|
18
|
-
""
|
17
|
+
text = "#import \"#{class_name}.h\"" + "\n" +
|
18
|
+
"#{import_other_classes vars}" + "\n" +
|
19
19
|
"@implementation #{class_name}" + "\n" +
|
20
20
|
"" + "\n" +
|
21
21
|
"#{init_method vars}" + "\n" +
|
@@ -40,6 +40,11 @@ module ObjCGenerator
|
|
40
40
|
""
|
41
41
|
end
|
42
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
|
+
|
43
48
|
def self.copy_method(class_name, vars)
|
44
49
|
t = class_name.dup
|
45
50
|
t[0] = t[0].chr.downcase
|
@@ -194,7 +199,7 @@ module ObjCGenerator
|
|
194
199
|
vars.each do |var|
|
195
200
|
result << " _#{var.varname} = #{var.default_value};"
|
196
201
|
end
|
197
|
-
result = vertical_align_vars result, /(
|
202
|
+
result = vertical_align_vars result, /(=)/, 0
|
198
203
|
result = result.intersperse("\n")
|
199
204
|
|
200
205
|
result.inject("") { |mem, var| mem << var }
|
@@ -1,21 +1,10 @@
|
|
1
1
|
module ObjCGenerator
|
2
2
|
class Command
|
3
3
|
|
4
|
-
def self.vertical_align strings
|
5
|
-
max_lengt = strings.map { | str|
|
6
|
-
c = str.rpartition(" ").first.length
|
7
|
-
}.max
|
8
|
-
|
9
|
-
result = strings.map { |str|
|
10
|
-
l = str.rpartition(" ").last.start_with?("*") ? max_lengt : max_lengt+1
|
11
|
-
str.rpartition(" ").first.ljust(l , ' ') + str.rpartition(" ").last
|
12
|
-
}
|
13
|
-
result
|
14
|
-
end
|
15
|
-
|
16
4
|
def self.class_header (class_name, vars)
|
17
5
|
@text = "#import <Foundation/Foundation.h>" + "\n" +
|
18
6
|
"" + "\n" +
|
7
|
+
"#{import_classes vars}" + "\n" +
|
19
8
|
"@interface #{class_name} : NSObject <NSCopying>" + "\n" +
|
20
9
|
"" + "\n" +
|
21
10
|
"#{class_properties vars}" + "\n" +
|
@@ -33,7 +22,7 @@ module ObjCGenerator
|
|
33
22
|
result << var.property_definition
|
34
23
|
end
|
35
24
|
|
36
|
-
result =
|
25
|
+
result = ObjCGenerator::vertical_align_vars result, /( )/, 4
|
37
26
|
result = result.intersperse("\n")
|
38
27
|
|
39
28
|
result.inject("") { |mem, var| mem << var }
|
@@ -54,12 +43,18 @@ module ObjCGenerator
|
|
54
43
|
"- (instancetype)copyWithZone:(NSZone *)zone;"
|
55
44
|
end
|
56
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
|
+
|
57
51
|
def self.run input_file, output_dir
|
58
52
|
|
59
53
|
parsed = JSON.parse(input_file)
|
60
54
|
|
61
55
|
parsed.each do |var|
|
62
56
|
if var["type"] == "Class"
|
57
|
+
|
63
58
|
vars = var["vars"].map { |hash|
|
64
59
|
case hash["type"]
|
65
60
|
when "Bool"
|
@@ -72,6 +67,9 @@ module ObjCGenerator
|
|
72
67
|
TypeString.new(hash["name"])
|
73
68
|
when "Date"
|
74
69
|
TypeDate.new(hash["name"])
|
70
|
+
else
|
71
|
+
TypeCustomObject.new(hash["name"], hash["type"])
|
72
|
+
|
75
73
|
end
|
76
74
|
}
|
77
75
|
|
@@ -81,7 +79,6 @@ module ObjCGenerator
|
|
81
79
|
implementation = ObjCGenerator::class_implementation var["name"], vars
|
82
80
|
File.open( output_dir + "/#{var["name"]}.m", 'w') { |file| file.write(implementation) }
|
83
81
|
|
84
|
-
# ObjCGenerator::testa(var["name"])
|
85
82
|
end
|
86
83
|
end
|
87
84
|
|
data/lib/ObjCGenerator/types.rb
CHANGED
@@ -151,3 +151,36 @@ class TypeDate < ObjCType
|
|
151
151
|
"#{newVarName}.#{self.varname} = [self.#{self.varname} copy];"
|
152
152
|
end
|
153
153
|
end
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
class TypeCustomObject < ObjCType
|
158
|
+
attr_accessor :varname
|
159
|
+
attr_accessor :var_type
|
160
|
+
def initialize( name , type)
|
161
|
+
super(name)
|
162
|
+
@var_type = type
|
163
|
+
end
|
164
|
+
def property_definition
|
165
|
+
"@property (nonatomic) #{@var_type} *#{@varname};"
|
166
|
+
end
|
167
|
+
def default_value
|
168
|
+
"[#{@var_type} new]"
|
169
|
+
end
|
170
|
+
def conversion_value origin
|
171
|
+
# "[#{origin} description]"
|
172
|
+
"[[SampleClass2 alloc] initWithDict:#{origin}]"
|
173
|
+
end
|
174
|
+
def to_dictionary_item
|
175
|
+
"@\"#{@varname}\" : [self.#{@varname} toDict] ?: @{}"
|
176
|
+
end
|
177
|
+
def inEquality_test other
|
178
|
+
"![self.#{self.varname} isEqual:#{other}.#{self.varname}]"
|
179
|
+
end
|
180
|
+
def description_row
|
181
|
+
"@\"self.#{self.varname} = %@\" , self.#{self.varname}"
|
182
|
+
end
|
183
|
+
def copyrow newVarName
|
184
|
+
"#{newVarName}.#{self.varname} = [self.#{self.varname} copyWithZone:nil];"
|
185
|
+
end
|
186
|
+
end
|
data/lib/ObjCGenerator.rb
CHANGED
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.
|
4
|
+
version: 0.0.2
|
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-
|
11
|
+
date: 2015-03-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|