Skip to content

Instantly share code, notes, and snippets.

@brianwebb01
Created March 22, 2012 18:45
Show Gist options
  • Save brianwebb01/2161598 to your computer and use it in GitHub Desktop.
Save brianwebb01/2161598 to your computer and use it in GitHub Desktop.
iOS property, synthesize and dealloc output generated from MySQL describe table output
data = <<EODATA
| id | int(11) | NO | PRI | NULL | auto_increment |
| status_id | int(11) | YES | | NULL | |
| project_type_id | int(11) | YES | | NULL | |
| physical_address | varchar(255) | YES | | NULL | |
| physical_address2 | varchar(255) | YES | | NULL | |
| physical_city | varchar(255) | YES | | NULL | |
| physical_state_id | int(11) | YES | | NULL | |
| physical_postal_code | varchar(255) | YES | | NULL | |
| physical_country_id | int(11) | YES | | NULL | |
| billing_address | varchar(255) | YES | | NULL | |
| billing_address2 | varchar(255) | YES | | NULL | |
| billing_city | varchar(255) | YES | | NULL | |
| billing_state_id | int(11) | YES | | NULL | |
| billing_postal_code | varchar(255) | YES | | NULL | |
| billing_country_id | int(11) | YES | | NULL | |
| account_id | int(11) | YES | | NULL | |
| customer_id | int(11) | YES | | NULL | |
| title | varchar(255) | YES | | NULL | | |
| job_number | varchar(255) | YES | | NULL | |
| project_manager_id | int(11) | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
| records_are_sorted | tinyint(1) | YES | | 0 | |
| customer_number | varchar(255) | YES | | NULL | |
+-----------------------------+--------------+------+-----+---------+----------------+
EODATA
results = data.scan(/^\|{1}\s{1}(\w+)\s+\|{1}\s{1}(\w+\(?\d*\)?)\s+\|{1}\s{1}(\w+)\s+\|{1}\s{1}(\w*)\s+\|{1}\s{1}(\w+)\s+\|{1}\s{1}(\w*)\s+\|{1}/i)
#create property declarations
puts '='*100
puts '| Property Declarations'
puts '='*100
results.each do |match|
field, type, null, key, default, extra = *match
field = "server_id" if field == "id"
out = "@property (nonatomic, readwrite, "
if type =~ /int/
out += "assign) NSInteger #{field};"
else
if type =~ /varchar/ || type =~ /text/
objc_type = "NSString"
elsif type =~ /date/
objc_type = "NSDate"
end
out += "retain) #{objc_type} * #{field};"
end
puts out
end #end each
puts "\n"*2
#create @synthesize declarations
puts '='*100
puts '| Synthesize Declarations'
puts '='*100
results.each do |match|
field, type, null, key, default, extra = *match
field = "server_id" if field == "id"
puts "@synthesize #{field};"
end #end each
puts "\n"*2
#create dealloc declarations
puts '='*100
puts '| Dealloc Declarations'
puts '='*100
results.each do |match|
field, type, null, key, default, extra = *match
field = "server_id" if field == "id"
if !(type =~ /int/)
puts "[#{field} release];"
end
end #end each
puts "\n"*2
#create object declarations
puts '='*100
puts '| Consider making objects for'
puts '='*100
results.each do |match|
field, type, null, key, default, extra = *match
field = "server_id" if field == "id"
if field =~ /_id/ && field != 'server_id'
puts field
end
end #end each
puts "\n"*2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment