ObjCGenerator 0.0.5 → 0.0.6

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: 6bda9cfd347531a805f73ddab559605a3d8cdc11
4
- data.tar.gz: cb6bc9885cc2016f8a75266ff18406b207e8bf4b
3
+ metadata.gz: 99a5a896777c606d06a79dfaaed6c290d62b4cf8
4
+ data.tar.gz: 2c4dbfacda9f5e823bebaadd0b8ba4864e7cf450
5
5
  SHA512:
6
- metadata.gz: 1b6c58d00ac110f5bf98e411f6b51b0ca42c6727e84be93371e02d23f4675c5b4c87b905cf84d95ce2d05a28c75476c8bdcb525c67320b0fb6b20b0ce540cdce
7
- data.tar.gz: 57626ddd58af53a87bf6ac7cd95264ab4102fe9135bf7319cb7d7914f36ecd029ce3da3aef3d445620a8458f0c648c305a569297e555e1b53101bfd8dae95aaf
6
+ metadata.gz: 2aebc58d88362c096cd5a70f8ed0e82fe4444d019557a80fcadf00bd43e3e9fe5658ccb89fbae7fbaf7130b1c1d3a99a2267d4d9bac6184b6c144b9dfe210a5b
7
+ data.tar.gz: 9278bae480f767287746a6b79b8700b97029fdc434a450c377dbd592eab5ea2fd7559a1cc3a520a8b99772afb142b92b58f0490ab7a747744e63db744c79614c
data/bin/objcgenerator CHANGED
@@ -13,23 +13,22 @@ command :generate do |c|
13
13
  c.syntax = 'objgenerator generate [options]'
14
14
  c.summary = 'Generate the Objective-C code'
15
15
  c.description = 'Generate the Objective-C code. Provide a valid json description with the --input <string> parameter.'
16
- c.example 'Use the "api.json" definition file and generate the classes in the current directory', 'objcgenerator generate --input "api.json" --output .'
16
+ c.example 'Use the "api.json" definition file and generate the classes in the current directory', 'objcgenerator generate --input "api.json" --output . --generate-categories'
17
17
  c.option '--input STRING', String, 'The path oth the JSON desctription'
18
18
  c.option '--output STRING', String, 'The output path where the obj-c code will be generated'
19
+ c.option '--generate-categories', 'For each class, a category is also created'
19
20
 
20
21
  c.action do |args, options|
21
22
  options.default :output => '.'
22
23
 
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
24
+ raise ArgumentError.new("Input parameter is required!") unless options.input
30
25
 
31
- command.run json, options.output
32
- end
26
+ input=File.new(options.input,"r");
27
+ json = input.read
28
+ command = ObjCGenerator::Command.new
29
+ command.run json, options.output
30
+
31
+ command.generate_categories(json, options.output) if options.generate_categories
33
32
  end
34
33
  end
35
34
 
@@ -0,0 +1,31 @@
1
+ module ObjCGenerator
2
+ class CategoryGenerator
3
+ def template_path filename
4
+ File.join(File.expand_path("../..", File.dirname(__FILE__)), "templates/#{filename}.erb")
5
+ end
6
+
7
+ def generate_incipit()
8
+ author = "Ignazioc"
9
+
10
+ incipit_template = File.read(template_path('incipit'))
11
+ ERB.new(incipit_template).result(binding)
12
+ end
13
+
14
+ def generate_category_header(class_name)
15
+ incipit = generate_incipit()
16
+
17
+ header_template = File.read(template_path('category_header'))
18
+ category_header = ERB.new(header_template).result(binding)
19
+ return incipit + category_header
20
+ end
21
+
22
+ def generate_category_implementation(class_name)
23
+ incipit = generate_incipit()
24
+
25
+ template = File.read(template_path('category_implementation'))
26
+ category_implementation = ERB.new(template).result(binding)
27
+ return incipit + category_implementation
28
+ end
29
+
30
+ end
31
+ end
@@ -14,7 +14,6 @@ module ObjCGenerator
14
14
 
15
15
  def generate_class_header (class_name, variables)
16
16
  incipit = generate_incipit()
17
-
18
17
  @class_predefinitions = generate_import_classes(variables)
19
18
  @class_name = "@interface #{class_name} : NSObject <NSCopying>"
20
19
  @class_properties = generate_class_properties(variables)
@@ -2,42 +2,36 @@ module ObjCGenerator
2
2
  class Command
3
3
 
4
4
  def run input_file, output_dir
5
+ parser = JSONParser.new()
5
6
 
6
- parsed = JSON.parse(input_file)
7
-
8
- parsed.each do |var|
9
- if var["type"] == "Class"
10
-
11
- vars = var["vars"].map { |hash|
12
- case hash["type"]
13
- when "Bool"
14
- TypeBool.new(hash["name"])
15
- when "Int"
16
- TypeInt.new(hash["name"])
17
- when "Float"
18
- TypeFloat.new(hash["name"])
19
- when "String"
20
- TypeString.new(hash["name"])
21
- when "Date"
22
- TypeDate.new(hash["name"])
23
- when "Array"
24
- TypeArray.new(hash["name"], hash["subType"])
25
- else
26
- TypeCustomObject.new(hash["name"], hash["type"])
27
-
28
- end
29
- }
30
-
31
- g = ClassInterfaceGenerator.new()
32
- gc = ClassInterfaceGenerator.new()
33
-
34
- header = g.generate_class_header var["name"], vars
35
- File.open( output_dir + "/#{var["name"]}.h", 'w') { |file| file.write(header) }
36
-
37
- implementation = gc.class_implementation var["name"], vars
38
- File.open( output_dir + "/#{var["name"]}.m", 'w') { |file| file.write(implementation) }
39
-
40
- end
7
+ result = parser.parse(input_file)
8
+ result.each do |klass|
9
+
10
+ g = ClassInterfaceGenerator.new()
11
+ gc = ClassInterfaceGenerator.new()
12
+
13
+ header = g.generate_class_header klass[:name], klass[:var_list]
14
+ File.open( output_dir + "/#{klass[:name]}.h", 'w') { |file| file.write(header) }
15
+
16
+ implementation = gc.class_implementation klass[:name], klass[:var_list]
17
+ File.open( output_dir + "/#{klass[:name]}.m", 'w') { |file| file.write(implementation) }
18
+ end
19
+ end
20
+
21
+ def generate_categories(input_file, output_dir)
22
+ parser = JSONParser.new()
23
+ result = parser.parse(input_file)
24
+
25
+ categoryGenerator = CategoryGenerator.new()
26
+
27
+ result.each do |klass|
28
+ class_name = klass[:name]
29
+
30
+ header = categoryGenerator.generate_category_header(class_name)
31
+ File.open( output_dir + "/#{klass[:name]}+CustomCode.h", 'w') { |file| file.write(header) }
32
+
33
+ implementation = categoryGenerator.generate_category_implementation(class_name)
34
+ File.open( output_dir + "/#{klass[:name]}+CustomCode.m", 'w') { |file| file.write(implementation) }
41
35
  end
42
36
  end
43
37
  end
@@ -0,0 +1,36 @@
1
+ module ObjCGenerator
2
+ class JSONParser
3
+
4
+
5
+ #return an array of maps
6
+ #in this format {:name => 'SampleClass', :var_list => [....]}
7
+ def parse(input_file)
8
+ parsed = JSON.parse(input_file)
9
+ result = []
10
+ parsed.each do |klass|
11
+ if klass["type"] == "Class"
12
+ var_list = klass["vars"].map { |hash|
13
+ case hash["type"]
14
+ when "Bool"
15
+ TypeBool.new(hash["name"])
16
+ when "Int"
17
+ TypeInt.new(hash["name"])
18
+ when "Float"
19
+ TypeFloat.new(hash["name"])
20
+ when "String"
21
+ TypeString.new(hash["name"])
22
+ when "Date"
23
+ TypeDate.new(hash["name"])
24
+ when "Array"
25
+ TypeArray.new(hash["name"], hash["subType"])
26
+ else
27
+ TypeCustomObject.new(hash["name"], hash["type"])
28
+ end
29
+ }
30
+ end #end if
31
+ result << {name: klass["name"], var_list: var_list }
32
+ end #end each
33
+ result
34
+ end #end parse
35
+ end
36
+ end
@@ -242,4 +242,7 @@ class TypeArray < ObjCType
242
242
  def copyrow newVarName
243
243
  "#{newVarName}.#{self.varname} = [self.#{self.varname} copyWithZone:nil];"
244
244
  end
245
+ def hash_row
246
+ "[self.#{self.varname} hash];"
247
+ end
245
248
  end
@@ -1,3 +1,3 @@
1
1
  module ObjCGenerator
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
data/lib/ObjCGenerator.rb CHANGED
@@ -4,6 +4,8 @@ require 'erb'
4
4
  require "ObjCGenerator/version"
5
5
  require "ObjCGenerator/class_interface_generator"
6
6
  require "ObjCGenerator/class_implementation_generator"
7
+ require "ObjCGenerator/category_generator"
8
+ require "ObjCGenerator/json_parser"
7
9
  require "ObjCGenerator/command"
8
10
  require "ObjCGenerator/types"
9
11
 
@@ -0,0 +1,5 @@
1
+ #import "<%= class_name %>.h"
2
+
3
+ @interface <%= class_name %> (CustomCode)
4
+
5
+ @end
@@ -0,0 +1,5 @@
1
+ #import "<%= class_name %>+CustomCode.h"
2
+
3
+ @implementation <%= class_name %> (CustomCode)
4
+
5
+ @end
@@ -0,0 +1,9 @@
1
+ #import <Foundation/Foundation.h>
2
+ <%= @class_predefinitions %>
3
+ <%= @class_name %>
4
+
5
+ <%= @class_properties %>
6
+
7
+ <%= @class_methods %>
8
+
9
+ @end
@@ -0,0 +1,23 @@
1
+ #import "<%= class_name %>.h"
2
+ <%= @import_other_classes %>
3
+ @implementation <%= class_name %>
4
+
5
+ <%= @init_method %>
6
+
7
+ <%= @init_with_dict_method %>
8
+
9
+ <%= @class_init_with_dict %>
10
+
11
+ <%= @to_dict_method %>
12
+
13
+ <%= @is_equals_method%>
14
+
15
+ <%= @is_equals_to_Object_method %>
16
+
17
+ <%= @description_method %>
18
+
19
+ <%= @copy_method %>
20
+
21
+ <%= @hash_method %>
22
+
23
+ @end
@@ -0,0 +1,5 @@
1
+ //
2
+ // This code is autogenerated using ObjCGenerator by <%= author %>
3
+ // https://github.com/ignazioc/ObjCGenerator
4
+ //
5
+
@@ -0,0 +1,3 @@
1
+ + (instancetype)<%= @lowercaseClassName %>WithDict:(NSDictionary *)dict {
2
+ return [[self alloc] initWithDict:dict];
3
+ }
@@ -0,0 +1,5 @@
1
+ - (instancetype)copyWithZone:(NSZone *)zone {
2
+ <%= @class_name %> *<%= @varname %> = [[[self class] allocWithZone:zone] init];<% for item in @copy_rows %>
3
+ <%= item %><% end %>
4
+ return <%= @varname %>;
5
+ }
@@ -0,0 +1,7 @@
1
+ - (NSString *)description {
2
+ NSString *params = [@[<% for item in @description_rows %>
3
+ <%= item %><% end %>
4
+ ] componentsJoinedByString:@", "
5
+ ];
6
+ return [NSString stringWithFormat:@"%@ { %@ }", NSStringFromClass([self class]), params];
7
+ }
@@ -0,0 +1,6 @@
1
+ - (NSUInteger)hash {
2
+ NSUInteger result = 1;
3
+ NSUInteger prime = 31;<% for item in @hash_rows %>
4
+ result += prime * result + <%= item %><% end %>
5
+ return result;
6
+ }
@@ -0,0 +1,7 @@
1
+ - (instancetype)init {
2
+ if ((self = [super init])) {
3
+ <% for item in @init_rows %>
4
+ <%= item %><% end %>
5
+ }
6
+ return self;
7
+ }
@@ -0,0 +1,8 @@
1
+ - (instancetype)initWithDict:(NSDictionary *)dict {
2
+ if ((self = [self init])) {
3
+
4
+ id val = nil;<% for item in @initWithDictRows %>
5
+ <%= item %><% end %>
6
+ }
7
+ return self;
8
+ }
@@ -0,0 +1,7 @@
1
+ - (BOOL)isEqual:(id)object {
2
+ if (self == object) { return true; }
3
+ else if ([object isKindOfClass:[self class]]) {
4
+ return [self isEqualTo<%= @class_name %>:object];
5
+ }
6
+ else { return NO; }
7
+ }
@@ -0,0 +1,5 @@
1
+ - (BOOL)isEqualTo<%= @class_name %>:(<%= @class_name %> *)other {
2
+ if (self == other) return YES;<% for item in @is_equal_rows %>
3
+ <%= item %><% end %>
4
+ return YES;
5
+ }
@@ -0,0 +1,5 @@
1
+ - (NSDictionary *)toDict {
2
+ return @{<% for item in @to_ditc_rows %>
3
+ <%= item %><% end %>
4
+ };
5
+ }
@@ -0,0 +1,10 @@
1
+ - (instancetype)init;
2
+ - (instancetype)initWithDict:(NSDictionary *)dict;
3
+ + (instancetype)<%= lowercase_class_name %>WithDict:(NSDictionary *)dict;
4
+ - (NSDictionary *)toDict;
5
+
6
+ - (BOOL)isEqual:(id)object;
7
+ - (BOOL)isEqualTo<%= class_name %>:(<%= class_name %> *)other;
8
+ - (NSUInteger)hash;
9
+ - (NSString *)description;
10
+ - (instancetype)copyWithZone:(NSZone *)zone;
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.5
4
+ version: 0.0.6
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-05-25 00:00:00.000000000 Z
11
+ date: 2015-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -62,11 +62,28 @@ extra_rdoc_files: []
62
62
  files:
63
63
  - bin/objcgenerator
64
64
  - lib/ObjCGenerator.rb
65
+ - lib/ObjCGenerator/category_generator.rb
65
66
  - lib/ObjCGenerator/class_implementation_generator.rb
66
67
  - lib/ObjCGenerator/class_interface_generator.rb
67
68
  - lib/ObjCGenerator/command.rb
69
+ - lib/ObjCGenerator/json_parser.rb
68
70
  - lib/ObjCGenerator/types.rb
69
71
  - lib/ObjCGenerator/version.rb
72
+ - templates/category_header.erb
73
+ - templates/category_implementation.erb
74
+ - templates/class_header.erb
75
+ - templates/class_implementation.erb
76
+ - templates/incipit.erb
77
+ - templates/method_class_init_with_dict.erb
78
+ - templates/method_copy.erb
79
+ - templates/method_description.erb
80
+ - templates/method_hash.erb
81
+ - templates/method_init.erb
82
+ - templates/method_init_with_dictionary.erb
83
+ - templates/method_is_equal.erb
84
+ - templates/method_is_equal_to.erb
85
+ - templates/method_to_dict.erb
86
+ - templates/methods_definition.erb
70
87
  homepage: ''
71
88
  licenses:
72
89
  - MIT