Skip to content

Commit

Permalink
Refactoring and adding types
Browse files Browse the repository at this point in the history
  • Loading branch information
twoism committed Mar 7, 2009
1 parent 713fd32 commit b8e6a06
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 35 deletions.
29 changes: 29 additions & 0 deletions generators/objectify/objectify_generator.rb
@@ -1,4 +1,33 @@
POINTER_TYPES = {
:datetime => "NSDate",
:date => "NSDate",
:string => "NSString",
:text => "NSString",
:integer => "NSNumber",
:float => "NSDecimalNumber",
:double => "NSDecimalNumber"
}
class ObjectifyGenerator < Rails::Generator::NamedBase

def objc_type type
POINTER_TYPES[type]
end

def synthesize_cols mdl
file_name.camelize.constantize.columns.inject("") do |str,col|
unless col.name == "id"
str << "#{col.name.camelize(:lower)}"
else
str << "#{file_name.camelize(:lower)}Id"
end
unless col == file_name.camelize.constantize.columns.last
str << ", "
else
str << ";"
end
end
end


def manifest
if file_name.camelize.constantize
Expand Down
22 changes: 2 additions & 20 deletions generators/objectify/templates/model.h
@@ -1,29 +1,11 @@
#import "ObjectiveResource.h"
<%-
def objc_type type
case
when type.match(/^int/)
"NSNumber"
when type.match(/varchar/)
"NSString"
when type.match(/tinyint/)
"NSString"
when type.match(/text/)
"NSString"
when type.match(/date/)
"NSDate"
else
"NSString"
end
end
-%>

@interface <%= file_name.camelize %> : NSObject {
<%- file_name.camelize.constantize.columns.each do |col| -%>
<%- unless col.name == "id" -%>
<%=objc_type col.sql_type%> *<%= col.name.camelize(:lower) -%>;
<%=objc_type col.type%> *<%= col.name.camelize(:lower) -%>;
<%- else -%>
NSString *<%= file_name.camelize(:lower) -%>Id;
<%=objc_type col.type%> *<%= file_name.camelize(:lower) -%>Id;
<%- end -%>
<% end %>
}
Expand Down
17 changes: 2 additions & 15 deletions generators/objectify/templates/model.m
@@ -1,22 +1,9 @@
#import "<%=file_name.camelize%>.h"
<%-
def do_synth mdl
file_name.camelize.constantize.columns.inject("") do |str,col|
unless col.name == "id"
str << "#{col.name.camelize(:lower)}"
else
str << "#{file_name.camelize(:lower)}Id"
end
unless col == file_name.camelize.constantize.columns.last
str << ", "
else
str << ";"
end
end
end

-%>
@implementation <%=file_name.camelize%>
@synthesize <%=do_synth file_name%>
@synthesize <%= synthesize_cols file_name%>

+ (NSString *)getRemoteCollectionName {
return @"<%=file_name.pluralize%>";
Expand Down

0 comments on commit b8e6a06

Please sign in to comment.