NSObject.rb 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.
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ *DS_Store
data/README.md CHANGED
@@ -1,12 +1,12 @@
1
1
  # NSObject
2
2
 
3
- TODO: Write a gem description
3
+ This gem adds a rails generator that will take Active Record models and convert them to native Objective-C classes.
4
4
 
5
5
  ## Installation
6
6
 
7
7
  Add this line to your application's Gemfile:
8
8
 
9
- gem 'NSObject'
9
+ gem 'NSObject.rb'
10
10
 
11
11
  And then execute:
12
12
 
@@ -14,11 +14,11 @@ And then execute:
14
14
 
15
15
  Or install it yourself as:
16
16
 
17
- $ gem install NSObject
17
+ $ gem install NSObject.rb
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ $ rails g objc_model ACTIVE_RECORD_NAME
22
22
 
23
23
  ## Contributing
24
24
 
@@ -1,3 +1,3 @@
1
1
  module NSObject
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,48 @@
1
+ class Objc
2
+ attr_accessor :class_name, :superclass_name, :strings_array, :numbers_array, :dates_array;
3
+
4
+ def initialize(class_name)
5
+ @class_name = class_name
6
+ @superclass_name = "NSObject"
7
+ @strings_array = Array.new()
8
+ @numbers_array = Array.new()
9
+ @dates_array = Array.new()
10
+
11
+ parse
12
+ end
13
+
14
+ def parse
15
+ object_class = Object.const_get(@class_name)
16
+
17
+ if (object_class.superclass != ActiveRecord::Base)
18
+ @superclass_name = object_class.superclass.to_s
19
+ end
20
+
21
+ hash = object_class.columns
22
+
23
+ hash.each do |column|
24
+ column_name = column.name.to_s
25
+ column_type = column.type.to_s
26
+
27
+ if (column_name == 'id' ||
28
+ column_name == 'name')
29
+ column_name = @class_name + '_' + column_name
30
+ end
31
+
32
+ column_name = column_name.camelize(:lower)
33
+
34
+ case column_type
35
+ when "string"
36
+ @strings_array << column_name
37
+ when "integer"
38
+ @numbers_array << column_name
39
+ when "datetime"
40
+ @dates_array << column_name
41
+ end
42
+ end
43
+ end
44
+
45
+ def to_s
46
+ @class_name + " -----------\nstrings_array: " + @strings_array.join(", ") + "\nnumbers_array: " + @numbers_array.join(", ") + "\ndates_array: " + @dates_array.join(", ")
47
+ end
48
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate objc_model Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,16 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/Objc.rb")
2
+
3
+ class ObjcModelGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+ argument :records, :type => :array
6
+
7
+ def generate_layout
8
+ records.each do |r|
9
+ @objc = Objc.new(r)
10
+
11
+ template "NSObject.h.rb.erb", "objc/#{r}.h"
12
+ template "NSObject.m.rb.erb", "objc/#{r}.m"
13
+ end
14
+ end
15
+
16
+ end
@@ -0,0 +1,26 @@
1
+ //
2
+ // Generated with NSObject.rb
3
+ // Created by James Paolantonio
4
+ //
5
+
6
+ #import <Foundation/Foundation.h>
7
+
8
+ <%= "@interface " + @objc.class_name + " : " + @objc.superclass_name + " {" %>
9
+ <% @objc.strings_array.each do |a| %>
10
+ <%= "NSString *" + a + ";" %><% end %>
11
+ <% @objc.numbers_array.each do |a| %>
12
+ <%= "NSNumber *" + a + ";" %><% end %>
13
+ <% @objc.dates_array.each do |a| %>
14
+ <%= "NSDate *" + a + ";" %><% end %>
15
+ }
16
+
17
+ <% @objc.strings_array.each do |a| %>
18
+ <%= "@property (strong, nonatomic) NSString *" + a + ";" %><% end %>
19
+ <% @objc.numbers_array.each do |a| %>
20
+ <%= "@property (strong, nonatomic) NSNumber *" + a + ";" %><% end %>
21
+ <% @objc.dates_array.each do |a| %>
22
+ <%= "@property (strong, nonatomic) NSDate *" + a + ";" %><% end %>
23
+
24
+ - (id)initWithDictionary:(NSDictionary *)dictionary;
25
+
26
+ @end
@@ -0,0 +1,29 @@
1
+ //
2
+ // Generated with NSObject.rb
3
+ // Created by James Paolantonio
4
+ //
5
+
6
+ <%= "#import \"" + @objc.class_name + ".h\"" %>
7
+
8
+ <%= "@implementation " + @objc.class_name %>
9
+ <% @objc.strings_array.each do |a| %>
10
+ <%= "@synthesize " + a + ";" %><% end %>
11
+ <% @objc.numbers_array.each do |a| %>
12
+ <%= "@synthesize " + a + ";" %><% end %>
13
+ <% @objc.dates_array.each do |a| %>
14
+ <%= "@synthesize " + a + ";" %><% end %>
15
+
16
+ - (id)initWithDictionary:(NSDictionary *)dictionary {
17
+ if (self = [super init])
18
+ {
19
+ <% @objc.strings_array.each do |a| %>
20
+ <%= "self." + a + " = [dictionary objectForKey:@\"" + a + "\"];" %><% end %>
21
+ <% @objc.numbers_array.each do |a| %>
22
+ <%= "self." + a + " = [dictionary objectForKey:@\"" + a + "\"];" %><% end %>
23
+ <% @objc.dates_array.each do |a| %>
24
+ <%= "self." + a + " = [dictionary objectForKey:@\"" + a + "\"];" %><% end %>
25
+ }
26
+ return self;
27
+ }
28
+
29
+ @end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - jPaolantonio
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2012-06-15 00:00:00 -04:00
17
+ date: 2012-06-17 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -36,6 +36,12 @@ files:
36
36
  - Rakefile
37
37
  - lib/NSObject.rb
38
38
  - lib/NSObject/version.rb
39
+ - lib/generators/.DS_Store
40
+ - lib/generators/NSObject.rb/Objc.rb
41
+ - lib/generators/NSObject.rb/USAGE
42
+ - lib/generators/NSObject.rb/objc_model_generator.rb
43
+ - lib/generators/NSObject.rb/templates/NSObject.h.rb.erb
44
+ - lib/generators/NSObject.rb/templates/NSObject.m.rb.erb
39
45
  has_rdoc: true
40
46
  homepage: ""
41
47
  licenses: []