Skip to content

Instantly share code, notes, and snippets.

@andrijac
Last active December 17, 2015 08:49
Show Gist options
  • Save andrijac/5582979 to your computer and use it in GitHub Desktop.
Save andrijac/5582979 to your computer and use it in GitHub Desktop.
#TODO: add attribute for PrimaryKey
#TODO: add attribute for GeneratedColumns
#TODO: add attribute for XmlElement
fileName = ARGV[0]
types = Hash.new
types[/bit/] = 'bool'
types[/decimal/] = 'decimal'
types[/varchar\([0-9]\)/] = 'string'
types[/nvarchar\([0-9]+\)/] = 'string'
types[/char\([0-9]+\)/] = 'string'
types[/int/] = 'int'
types[/int/] = 'int'
types[/datetime/] = 'DateTime'
types[/tinyint/] = 'byte'
#constants template
tempConst = 'public const string %{name}ColumnKey = "%{name}";' + "\n"
#properties template
tempProp = "[ColumnInfo(%{name}ColumnKey)]\n"\
"public %{type} %{name}\n"\
"\{\n"\
"get;\n"\
"set;\n"\
"\}\n"
#prepare for iteration through lines
line_num=0
text = File.open(fileName).read
output1 = '';
output2 = '';
text.gsub!(/\r\n?/, "\n")
text.each_line do |line|
arr = line.split(/\s+/)
#TODO: check if line/values are empty, skip if true
#TODO: strip [] if found in property/column name
# constant string build
output1 << tempConst % { :name => arr[0] }
# default type if not found in hash
# set to string1 on purpose, so we get the warning that type is not found
type = 'string1';
# match a type in hash
types.each do |key, value|
if key.match(arr[1])
type = value
break
end
end
# property string build
output2 << tempProp % { :name => arr[0], :type => type }
end
print output1 + output2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment