Created
August 15, 2009 17:15
-
-
Save coreymartella/168404 to your computer and use it in GitHub Desktop.
rake task to generate objc models from rails ones
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
desc "Generate iPhone Models" | |
task :objective_resource_skeleton => :environment do | |
output_path = "#{Rails.root}/doc/iphone_models" | |
#get all our models | |
model_files = Dir.glob("#{Rails.root}/app/models/*.rb").each { |file| require file } | |
models = Object.subclasses_of(ActiveRecord::Base).select { |model| | |
File.exist?("#{Rails.root}/app/models/#{model.name.underscore}.rb") | |
} | |
#make sure our output path exists | |
FileUtils.mkdir_p(output_path) | |
models.each do |model| | |
model_downcase_name = "#{model.name[0,1].downcase}#{model.name[1..-1]}" | |
fields = [] | |
properties = [] | |
column_names = [] | |
object_column_names = [] | |
model.columns.each do |c| | |
field_type = "NSString*" | |
case c.type | |
when :integer | |
field_type = "NSInteger" | |
when :decimal | |
field_type = "CGFloat" | |
when :boolean | |
field_type = "BOOL" | |
when :date, :datetime | |
field_type = "NSDate*" | |
end | |
object_type = field_type.include?("*") | |
#set the field properties depending on if its an object or not | |
field_properties = object_type ? "nonatomic, retain, readwrite" : "nonatomic, assign, readwrite" | |
#use a special classNameId format for ids, otherwise ue regular camelcase with the first char being lower | |
column_downcase_name = (c.name == 'id' ? "#{model_downcase_name}Id" : "#{c.name[0,1].downcase}#{c.name.camelcase[1..-1]}") | |
column_names << column_downcase_name | |
object_column_names << column_downcase_name if object_type | |
fields << "#{field_type} #{column_downcase_name};" | |
properties << "@property (#{field_properties}) #{field_type} #{column_downcase_name};" | |
end | |
#write the files | |
File.open("#{output_path}/#{model.name}.h","w") do |f| | |
f.puts(H_CODE_TEMPLATE % [model.name, fields.join("\n "), properties.join("\n")]) | |
end | |
File.open("#{output_path}/#{model.name}.m","w") do |f| | |
f.puts(M_CODE_TEMPLATE % [model.name, model.name, column_names.join(", "), object_column_names.map{|c| "[#{c} release];"}.join("\n ")]) | |
end | |
end | |
#create an archive of the models | |
`cd #{output_path} && tar -zcf iphone_models.tgz *.m *.h` | |
end | |
#add in your own headers and comments | |
H_CODE_TEMPLATE = <<-CODE | |
#import "ObjectiveResource.h" | |
@interface %s : NSObject | |
{ | |
%s | |
} | |
%s | |
@end | |
CODE | |
M_CODE_TEMPLATE = <<-CODE | |
#import "%s.h" | |
@implementation %s | |
@synthesize %s; | |
-(void) dealloc | |
{ | |
//release the object columns | |
%s | |
[super dealloc]; | |
} | |
@end | |
CODE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment