subsonic 3.0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/LICENSE.txt +41 -0
- data/lib/SubSonic.Core.dll +0 -0
- data/lib/T4 Templates/ActiveRecord/ActiveRecord.tt +620 -0
- data/lib/T4 Templates/ActiveRecord/Context.tt +295 -0
- data/lib/T4 Templates/ActiveRecord/SQLServer.ttinclude +347 -0
- data/lib/T4 Templates/ActiveRecord/Settings.ttinclude +552 -0
- data/lib/T4 Templates/ActiveRecord/StoredProcedures.tt +30 -0
- data/lib/T4 Templates/ActiveRecord/Structs.tt +67 -0
- data/lib/T4 Templates/LinqTemplates/Classes.tt +117 -0
- data/lib/T4 Templates/LinqTemplates/Context.tt +276 -0
- data/lib/T4 Templates/LinqTemplates/SQLServer.ttinclude +349 -0
- data/lib/T4 Templates/LinqTemplates/Settings.ttinclude +551 -0
- data/lib/T4 Templates/LinqTemplates/StoredProcedures.tt +30 -0
- data/lib/T4 Templates/LinqTemplates/Structs.tt +67 -0
- data/lib/T4 Templates/README.txt +7 -0
- data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/ActiveRecord.tt +560 -0
- data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/ActiveRecord.vb +6447 -0
- data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/Context.tt +258 -0
- data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/Context.vb +349 -0
- data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/SQLServer.ttinclude +332 -0
- data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/Settings.ttinclude +550 -0
- data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/StoredProcedures.tt +30 -0
- data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/StoredProcedures.vb +55 -0
- data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/Structs.tt +57 -0
- data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/Structs.vb +1478 -0
- data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/Tests/FetchTests.vb +52 -0
- data/lib/T4 Templates/SubSonic.TemplatesVB/App.config +9 -0
- data/lib/T4 Templates/SubSonic.TemplatesVB/My Project/Application.Designer.vb +13 -0
- data/lib/T4 Templates/SubSonic.TemplatesVB/My Project/Application.myapp +10 -0
- data/lib/T4 Templates/SubSonic.TemplatesVB/My Project/AssemblyInfo.vb +35 -0
- data/lib/T4 Templates/SubSonic.TemplatesVB/My Project/Resources.Designer.vb +63 -0
- data/lib/T4 Templates/SubSonic.TemplatesVB/My Project/Resources.resx +117 -0
- data/lib/T4 Templates/SubSonic.TemplatesVB/My Project/Settings.Designer.vb +73 -0
- data/lib/T4 Templates/SubSonic.TemplatesVB/My Project/Settings.settings +7 -0
- data/lib/T4 Templates/SubSonic.TemplatesVB/SubSonic.TemplatesVB.vbproj +171 -0
- data/lib/T4 Templates/SubSonic.TemplatesVB/SubSonic.TemplatesVB.vbproj.user +5 -0
- data/lib/T4 Templates/TemplateProviders/MySQL.ttinclude +278 -0
- data/lib/T4 Templates/TemplateProviders/MySQL.ttinclude.orig +303 -0
- data/lib/T4 Templates/TemplateProviders/MySQLTest.tt +27 -0
- data/lib/T4 Templates/TemplateProviders/SQLite.ttinclude +194 -0
- data/lib/T4 Templates/TemplateProviders/SQLite.ttinclude.orig +236 -0
- data/lib/T4 Templates/TemplateProviders/SQLiteTest.tt +27 -0
- data/lib/T4 Templates/TemplateProviders/Settings.ttinclude +551 -0
- metadata +112 -0
@@ -0,0 +1,27 @@
|
|
1
|
+
<#@ template language="C#v3.5" debug="False" hostspecific="True" #>
|
2
|
+
<#@ output extension=".cs" #>
|
3
|
+
<#@ include file="SQLite.ttinclude" #>
|
4
|
+
<#
|
5
|
+
var tables = LoadTables();
|
6
|
+
|
7
|
+
foreach(var tbl in tables){
|
8
|
+
#>
|
9
|
+
//***********************************************
|
10
|
+
//Table=<#=tbl.Name#>
|
11
|
+
//Columns=<#=tbl.Columns.Count#>
|
12
|
+
//PK =<#=tbl.PK.Name#>
|
13
|
+
//FKs = <#=tbl.FKTables.Count#>;
|
14
|
+
|
15
|
+
|
16
|
+
<# foreach(var col in tbl.Columns){#>
|
17
|
+
//<#=col.Name#> (<#=col.SysType#>): Nullable: <#=col.IsNullable#> <#=col.DbType#>
|
18
|
+
<# }#>
|
19
|
+
|
20
|
+
//FKs
|
21
|
+
<# foreach(var fk in tbl.FKTables){#>
|
22
|
+
//<#=fk.ThisTable#> --> <#=fk.OtherTable#>
|
23
|
+
<# }#>
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
<# }#>
|
@@ -0,0 +1,551 @@
|
|
1
|
+
<#@ template language="C#v3.5" debug="True" hostspecific="True" #>
|
2
|
+
<#@ assembly name="EnvDTE" #>
|
3
|
+
<#@ assembly name="System.Core.dll" #>
|
4
|
+
<#@ assembly name="System.Data" #>
|
5
|
+
<#@ assembly name="System.Xml" #>
|
6
|
+
<#@ assembly name="System.Configuration" #>
|
7
|
+
<#@ import namespace="System.Collections.Generic" #>
|
8
|
+
<#@ import namespace="System.Data" #>
|
9
|
+
<#@ import namespace="System.Data.SqlClient" #>
|
10
|
+
<#@ import namespace="System.Data.Common" #>
|
11
|
+
<#@ import namespace="System.Diagnostics" #>
|
12
|
+
<#@ import namespace="System.Globalization" #>
|
13
|
+
<#@ import namespace="System.IO" #>
|
14
|
+
<#@ import namespace="System.Linq" #>
|
15
|
+
<#@ import namespace="System.Text" #>
|
16
|
+
<#@ import namespace="System.Text.RegularExpressions" #>
|
17
|
+
<#@ import namespace="System.Configuration" #>
|
18
|
+
|
19
|
+
<#+
|
20
|
+
|
21
|
+
const string Namespace = "SouthWind";
|
22
|
+
const string ConnectionStringName = "NorthwindMySql";
|
23
|
+
|
24
|
+
//This is the name of your database and is used in naming
|
25
|
+
//the repository. By default we set it to the connection string name
|
26
|
+
const string DatabaseName = "Northwind";
|
27
|
+
|
28
|
+
const bool TreatTinyint1AsBool = false;
|
29
|
+
|
30
|
+
//this is a list of tables you don't want generated
|
31
|
+
string[] ExcludeTables = new string[]{
|
32
|
+
"sysdiagrams",
|
33
|
+
"BuildVersion",
|
34
|
+
};
|
35
|
+
|
36
|
+
string CleanUp(string tableName){
|
37
|
+
string result=tableName;
|
38
|
+
|
39
|
+
//strip blanks
|
40
|
+
result=result.Replace(" ","");
|
41
|
+
|
42
|
+
//put your logic here...
|
43
|
+
|
44
|
+
return result;
|
45
|
+
}
|
46
|
+
|
47
|
+
string CheckNullable(Column col){
|
48
|
+
string result="";
|
49
|
+
if(col.IsNullable && col.SysType !="byte[]" && col.SysType !="string")
|
50
|
+
result="?";
|
51
|
+
return result;
|
52
|
+
}
|
53
|
+
string GetConnectionString(string connectionStringName){
|
54
|
+
var _CurrentProject = GetCurrentProject();
|
55
|
+
|
56
|
+
string result="";
|
57
|
+
ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
|
58
|
+
configFile.ExeConfigFilename = GetConfigPath();
|
59
|
+
|
60
|
+
if (string.IsNullOrEmpty(configFile.ExeConfigFilename))
|
61
|
+
throw new ArgumentNullException("The project does not contain App.config or Web.config file.");
|
62
|
+
|
63
|
+
|
64
|
+
var config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None);
|
65
|
+
var connSection=config.ConnectionStrings;
|
66
|
+
|
67
|
+
//if the connectionString is empty - which is the defauls
|
68
|
+
//look for count-1 - this is the last connection string
|
69
|
+
//and takes into account AppServices and LocalSqlServer
|
70
|
+
if(string.IsNullOrEmpty(connectionStringName)){
|
71
|
+
if(connSection.ConnectionStrings.Count>1){
|
72
|
+
result=connSection.ConnectionStrings[connSection.ConnectionStrings.Count-1].ConnectionString;
|
73
|
+
}
|
74
|
+
}else{
|
75
|
+
try{
|
76
|
+
result=connSection.ConnectionStrings[connectionStringName].ConnectionString;
|
77
|
+
}catch{
|
78
|
+
result="There is no connection string name called '"+connectionStringName+"'";
|
79
|
+
}
|
80
|
+
}
|
81
|
+
|
82
|
+
return result;
|
83
|
+
}
|
84
|
+
|
85
|
+
string _connectionString="";
|
86
|
+
public string ConnectionString{
|
87
|
+
get {
|
88
|
+
if(String.IsNullOrEmpty(_connectionString)){
|
89
|
+
|
90
|
+
_connectionString=GetConnectionString(ConnectionStringName);
|
91
|
+
|
92
|
+
}
|
93
|
+
|
94
|
+
if(_connectionString.Contains("|DataDirectory|")){
|
95
|
+
//have to replace it
|
96
|
+
string dataFilePath=GetDataDirectory();
|
97
|
+
_connectionString=_connectionString.Replace("|DataDirectory|",dataFilePath);
|
98
|
+
}
|
99
|
+
|
100
|
+
return _connectionString;
|
101
|
+
}
|
102
|
+
}
|
103
|
+
|
104
|
+
public EnvDTE.Project GetCurrentProject() {
|
105
|
+
|
106
|
+
IServiceProvider _ServiceProvider = (IServiceProvider)Host;
|
107
|
+
if (_ServiceProvider == null)
|
108
|
+
throw new Exception("Host property returned unexpected value (null)");
|
109
|
+
|
110
|
+
EnvDTE.DTE dte = (EnvDTE.DTE)_ServiceProvider.GetService(typeof(EnvDTE.DTE));
|
111
|
+
if (dte == null)
|
112
|
+
throw new Exception("Unable to retrieve EnvDTE.DTE");
|
113
|
+
|
114
|
+
Array activeSolutionProjects = (Array)dte.ActiveSolutionProjects;
|
115
|
+
if (activeSolutionProjects == null)
|
116
|
+
throw new Exception("DTE.ActiveSolutionProjects returned null");
|
117
|
+
|
118
|
+
EnvDTE.Project dteProject = (EnvDTE.Project)activeSolutionProjects.GetValue(0);
|
119
|
+
if (dteProject == null)
|
120
|
+
throw new Exception("DTE.ActiveSolutionProjects[0] returned null");
|
121
|
+
|
122
|
+
return dteProject;
|
123
|
+
|
124
|
+
}
|
125
|
+
|
126
|
+
private string GetProjectPath()
|
127
|
+
{
|
128
|
+
EnvDTE.Project project = GetCurrentProject();
|
129
|
+
System.IO.FileInfo info = new System.IO.FileInfo(project.FullName);
|
130
|
+
return info.Directory.FullName;
|
131
|
+
}
|
132
|
+
|
133
|
+
private string GetConfigPath()
|
134
|
+
{
|
135
|
+
EnvDTE.Project project = GetCurrentProject();
|
136
|
+
foreach (EnvDTE.ProjectItem item in project.ProjectItems)
|
137
|
+
{
|
138
|
+
// if it is the app.config file, then open it up
|
139
|
+
if (item.Name.Equals("App.config",StringComparison.InvariantCultureIgnoreCase) || item.Name.Equals("Web.config",StringComparison.InvariantCultureIgnoreCase))
|
140
|
+
return GetProjectPath() + "\\" + item.Name;
|
141
|
+
}
|
142
|
+
return String.Empty;
|
143
|
+
}
|
144
|
+
|
145
|
+
public string GetDataDirectory(){
|
146
|
+
EnvDTE.Project project=GetCurrentProject();
|
147
|
+
return System.IO.Path.GetDirectoryName(project.FileName)+"\\App_Data\\";
|
148
|
+
}
|
149
|
+
|
150
|
+
public class Table{
|
151
|
+
|
152
|
+
public List<Column> Columns;
|
153
|
+
public List<FKTable> FKTables;
|
154
|
+
public string Name;
|
155
|
+
public string CleanName;
|
156
|
+
public string ClassName;
|
157
|
+
public string PrimaryKey;
|
158
|
+
public string Schema;
|
159
|
+
public string QueryableName;
|
160
|
+
|
161
|
+
public bool HasLogicalDelete(){
|
162
|
+
return this.Columns.Any(x=>x.Name.ToLower()=="deleted" || x.Name.ToLower()=="isdeleted");
|
163
|
+
}
|
164
|
+
public Column DeleteColumn{
|
165
|
+
get{
|
166
|
+
Column result=null;
|
167
|
+
if(this.Columns.Any(x=>x.Name.ToLower()=="deleted"))
|
168
|
+
result=this.Columns.Single(x=>x.Name.ToLower()=="deleted");
|
169
|
+
if(this.Columns.Any(x=>x.Name.ToLower()=="isdeleted"))
|
170
|
+
result=this.Columns.Single(x=>x.Name.ToLower()=="isdeleted");
|
171
|
+
return result;
|
172
|
+
}
|
173
|
+
}
|
174
|
+
public Column PK{
|
175
|
+
get{
|
176
|
+
return this.Columns.FirstOrDefault(x=>x.IsPK) ?? this.Columns[0];
|
177
|
+
}
|
178
|
+
}
|
179
|
+
public Column Descriptor{
|
180
|
+
get{
|
181
|
+
if(this.Columns.Count==1){
|
182
|
+
return this.Columns[0];
|
183
|
+
}else{
|
184
|
+
//get the first string column
|
185
|
+
Column result=null;
|
186
|
+
result=this.Columns.FirstOrDefault(x=>x.SysType.ToLower().Trim()=="string");
|
187
|
+
if(result==null)
|
188
|
+
result=this.Columns[1];
|
189
|
+
return result;
|
190
|
+
}
|
191
|
+
}
|
192
|
+
}
|
193
|
+
}
|
194
|
+
|
195
|
+
public class Column{
|
196
|
+
public string Name;
|
197
|
+
public string CleanName;
|
198
|
+
public string SysType;
|
199
|
+
public string DataType;
|
200
|
+
public DbType DbType;
|
201
|
+
public bool AutoIncrement;
|
202
|
+
public bool IsPK;
|
203
|
+
public int MaxLength;
|
204
|
+
public bool IsNullable;
|
205
|
+
public bool IsForeignKey;
|
206
|
+
}
|
207
|
+
public class FKTable{
|
208
|
+
public string ThisTable;
|
209
|
+
public string ThisColumn;
|
210
|
+
public string OtherTable;
|
211
|
+
public string OtherColumn;
|
212
|
+
public string OtherClass;
|
213
|
+
public string OtherQueryable;
|
214
|
+
}
|
215
|
+
|
216
|
+
public class SP{
|
217
|
+
public string Name;
|
218
|
+
public string CleanName;
|
219
|
+
public string ClassName;
|
220
|
+
public List<SPParam> Parameters;
|
221
|
+
public SP(){
|
222
|
+
Parameters=new List<SPParam>();
|
223
|
+
}
|
224
|
+
public string ArgList{
|
225
|
+
get{
|
226
|
+
StringBuilder sb=new StringBuilder();
|
227
|
+
int loopCount=1;
|
228
|
+
foreach(var par in Parameters){
|
229
|
+
sb.AppendFormat("{0} {1}", par.SysType,par.CleanName);
|
230
|
+
if(loopCount<Parameters.Count)
|
231
|
+
sb.Append(",");
|
232
|
+
loopCount++;
|
233
|
+
}
|
234
|
+
return sb.ToString();
|
235
|
+
}
|
236
|
+
}
|
237
|
+
}
|
238
|
+
public class SPParam{
|
239
|
+
public string Name;
|
240
|
+
public string CleanName;
|
241
|
+
public string SysType;
|
242
|
+
public string DbType;
|
243
|
+
}
|
244
|
+
|
245
|
+
|
246
|
+
/*
|
247
|
+
* SubSonic - http://subsonicproject.com
|
248
|
+
*
|
249
|
+
* The contents of this file are subject to the New BSD
|
250
|
+
* License (the "License"); you may not use this file
|
251
|
+
* except in compliance with the License. You may obtain a copy of
|
252
|
+
* the License at http://www.opensource.org/licenses/bsd-license.php
|
253
|
+
*
|
254
|
+
* Software distributed under the License is distributed on an
|
255
|
+
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
256
|
+
* implied. See the License for the specific language governing
|
257
|
+
* rights and limitations under the License.
|
258
|
+
*/
|
259
|
+
|
260
|
+
/// <summary>
|
261
|
+
/// Summary for the Inflector class
|
262
|
+
/// </summary>
|
263
|
+
public static class Inflector {
|
264
|
+
private static readonly List<InflectorRule> _plurals = new List<InflectorRule>();
|
265
|
+
private static readonly List<InflectorRule> _singulars = new List<InflectorRule>();
|
266
|
+
private static readonly List<string> _uncountables = new List<string>();
|
267
|
+
|
268
|
+
/// <summary>
|
269
|
+
/// Initializes the <see cref="Inflector"/> class.
|
270
|
+
/// </summary>
|
271
|
+
static Inflector() {
|
272
|
+
AddPluralRule("$", "s");
|
273
|
+
AddPluralRule("s$", "s");
|
274
|
+
AddPluralRule("(ax|test)is$", "$1es");
|
275
|
+
AddPluralRule("(octop|vir)us$", "$1i");
|
276
|
+
AddPluralRule("(alias|status)$", "$1es");
|
277
|
+
AddPluralRule("(bu)s$", "$1ses");
|
278
|
+
AddPluralRule("(buffal|tomat)o$", "$1oes");
|
279
|
+
AddPluralRule("([ti])um$", "$1a");
|
280
|
+
AddPluralRule("sis$", "ses");
|
281
|
+
AddPluralRule("(?:([^f])fe|([lr])f)$", "$1$2ves");
|
282
|
+
AddPluralRule("(hive)$", "$1s");
|
283
|
+
AddPluralRule("([^aeiouy]|qu)y$", "$1ies");
|
284
|
+
AddPluralRule("(x|ch|ss|sh)$", "$1es");
|
285
|
+
AddPluralRule("(matr|vert|ind)ix|ex$", "$1ices");
|
286
|
+
AddPluralRule("([m|l])ouse$", "$1ice");
|
287
|
+
AddPluralRule("^(ox)$", "$1en");
|
288
|
+
AddPluralRule("(quiz)$", "$1zes");
|
289
|
+
|
290
|
+
AddSingularRule("s$", String.Empty);
|
291
|
+
AddSingularRule("ss$", "ss");
|
292
|
+
AddSingularRule("(n)ews$", "$1ews");
|
293
|
+
AddSingularRule("([ti])a$", "$1um");
|
294
|
+
AddSingularRule("((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$", "$1$2sis");
|
295
|
+
AddSingularRule("(^analy)ses$", "$1sis");
|
296
|
+
AddSingularRule("([^f])ves$", "$1fe");
|
297
|
+
AddSingularRule("(hive)s$", "$1");
|
298
|
+
AddSingularRule("(tive)s$", "$1");
|
299
|
+
AddSingularRule("([lr])ves$", "$1f");
|
300
|
+
AddSingularRule("([^aeiouy]|qu)ies$", "$1y");
|
301
|
+
AddSingularRule("(s)eries$", "$1eries");
|
302
|
+
AddSingularRule("(m)ovies$", "$1ovie");
|
303
|
+
AddSingularRule("(x|ch|ss|sh)es$", "$1");
|
304
|
+
AddSingularRule("([m|l])ice$", "$1ouse");
|
305
|
+
AddSingularRule("(bus)es$", "$1");
|
306
|
+
AddSingularRule("(o)es$", "$1");
|
307
|
+
AddSingularRule("(shoe)s$", "$1");
|
308
|
+
AddSingularRule("(cris|ax|test)es$", "$1is");
|
309
|
+
AddSingularRule("(octop|vir)i$", "$1us");
|
310
|
+
AddSingularRule("(alias|status)$", "$1");
|
311
|
+
AddSingularRule("(alias|status)es$", "$1");
|
312
|
+
AddSingularRule("^(ox)en", "$1");
|
313
|
+
AddSingularRule("(vert|ind)ices$", "$1ex");
|
314
|
+
AddSingularRule("(matr)ices$", "$1ix");
|
315
|
+
AddSingularRule("(quiz)zes$", "$1");
|
316
|
+
|
317
|
+
AddIrregularRule("person", "people");
|
318
|
+
AddIrregularRule("man", "men");
|
319
|
+
AddIrregularRule("child", "children");
|
320
|
+
AddIrregularRule("sex", "sexes");
|
321
|
+
AddIrregularRule("tax", "taxes");
|
322
|
+
AddIrregularRule("move", "moves");
|
323
|
+
|
324
|
+
AddUnknownCountRule("equipment");
|
325
|
+
AddUnknownCountRule("information");
|
326
|
+
AddUnknownCountRule("rice");
|
327
|
+
AddUnknownCountRule("money");
|
328
|
+
AddUnknownCountRule("species");
|
329
|
+
AddUnknownCountRule("series");
|
330
|
+
AddUnknownCountRule("fish");
|
331
|
+
AddUnknownCountRule("sheep");
|
332
|
+
}
|
333
|
+
|
334
|
+
/// <summary>
|
335
|
+
/// Adds the irregular rule.
|
336
|
+
/// </summary>
|
337
|
+
/// <param name="singular">The singular.</param>
|
338
|
+
/// <param name="plural">The plural.</param>
|
339
|
+
private static void AddIrregularRule(string singular, string plural) {
|
340
|
+
AddPluralRule(String.Concat("(", singular[0], ")", singular.Substring(1), "$"), String.Concat("$1", plural.Substring(1)));
|
341
|
+
AddSingularRule(String.Concat("(", plural[0], ")", plural.Substring(1), "$"), String.Concat("$1", singular.Substring(1)));
|
342
|
+
}
|
343
|
+
|
344
|
+
/// <summary>
|
345
|
+
/// Adds the unknown count rule.
|
346
|
+
/// </summary>
|
347
|
+
/// <param name="word">The word.</param>
|
348
|
+
private static void AddUnknownCountRule(string word) {
|
349
|
+
_uncountables.Add(word.ToLower());
|
350
|
+
}
|
351
|
+
|
352
|
+
/// <summary>
|
353
|
+
/// Adds the plural rule.
|
354
|
+
/// </summary>
|
355
|
+
/// <param name="rule">The rule.</param>
|
356
|
+
/// <param name="replacement">The replacement.</param>
|
357
|
+
private static void AddPluralRule(string rule, string replacement) {
|
358
|
+
_plurals.Add(new InflectorRule(rule, replacement));
|
359
|
+
}
|
360
|
+
|
361
|
+
/// <summary>
|
362
|
+
/// Adds the singular rule.
|
363
|
+
/// </summary>
|
364
|
+
/// <param name="rule">The rule.</param>
|
365
|
+
/// <param name="replacement">The replacement.</param>
|
366
|
+
private static void AddSingularRule(string rule, string replacement) {
|
367
|
+
_singulars.Add(new InflectorRule(rule, replacement));
|
368
|
+
}
|
369
|
+
|
370
|
+
/// <summary>
|
371
|
+
/// Makes the plural.
|
372
|
+
/// </summary>
|
373
|
+
/// <param name="word">The word.</param>
|
374
|
+
/// <returns></returns>
|
375
|
+
public static string MakePlural(string word) {
|
376
|
+
return ApplyRules(_plurals, word);
|
377
|
+
}
|
378
|
+
|
379
|
+
/// <summary>
|
380
|
+
/// Makes the singular.
|
381
|
+
/// </summary>
|
382
|
+
/// <param name="word">The word.</param>
|
383
|
+
/// <returns></returns>
|
384
|
+
public static string MakeSingular(string word) {
|
385
|
+
return ApplyRules(_singulars, word);
|
386
|
+
}
|
387
|
+
|
388
|
+
/// <summary>
|
389
|
+
/// Applies the rules.
|
390
|
+
/// </summary>
|
391
|
+
/// <param name="rules">The rules.</param>
|
392
|
+
/// <param name="word">The word.</param>
|
393
|
+
/// <returns></returns>
|
394
|
+
private static string ApplyRules(IList<InflectorRule> rules, string word) {
|
395
|
+
string result = word;
|
396
|
+
if (!_uncountables.Contains(word.ToLower())) {
|
397
|
+
for (int i = rules.Count - 1; i >= 0; i--) {
|
398
|
+
string currentPass = rules[i].Apply(word);
|
399
|
+
if (currentPass != null) {
|
400
|
+
result = currentPass;
|
401
|
+
break;
|
402
|
+
}
|
403
|
+
}
|
404
|
+
}
|
405
|
+
return result;
|
406
|
+
}
|
407
|
+
|
408
|
+
/// <summary>
|
409
|
+
/// Converts the string to title case.
|
410
|
+
/// </summary>
|
411
|
+
/// <param name="word">The word.</param>
|
412
|
+
/// <returns></returns>
|
413
|
+
public static string ToTitleCase(string word) {
|
414
|
+
return Regex.Replace(ToHumanCase(AddUnderscores(word)), @"\b([a-z])",
|
415
|
+
delegate(Match match) { return match.Captures[0].Value.ToUpper(); });
|
416
|
+
}
|
417
|
+
|
418
|
+
/// <summary>
|
419
|
+
/// Converts the string to human case.
|
420
|
+
/// </summary>
|
421
|
+
/// <param name="lowercaseAndUnderscoredWord">The lowercase and underscored word.</param>
|
422
|
+
/// <returns></returns>
|
423
|
+
public static string ToHumanCase(string lowercaseAndUnderscoredWord) {
|
424
|
+
return MakeInitialCaps(Regex.Replace(lowercaseAndUnderscoredWord, @"_", " "));
|
425
|
+
}
|
426
|
+
|
427
|
+
|
428
|
+
/// <summary>
|
429
|
+
/// Adds the underscores.
|
430
|
+
/// </summary>
|
431
|
+
/// <param name="pascalCasedWord">The pascal cased word.</param>
|
432
|
+
/// <returns></returns>
|
433
|
+
public static string AddUnderscores(string pascalCasedWord) {
|
434
|
+
return Regex.Replace(Regex.Replace(Regex.Replace(pascalCasedWord, @"([A-Z]+)([A-Z][a-z])", "$1_$2"), @"([a-z\d])([A-Z])", "$1_$2"), @"[-\s]", "_").ToLower();
|
435
|
+
}
|
436
|
+
|
437
|
+
/// <summary>
|
438
|
+
/// Makes the initial caps.
|
439
|
+
/// </summary>
|
440
|
+
/// <param name="word">The word.</param>
|
441
|
+
/// <returns></returns>
|
442
|
+
public static string MakeInitialCaps(string word) {
|
443
|
+
return String.Concat(word.Substring(0, 1).ToUpper(), word.Substring(1).ToLower());
|
444
|
+
}
|
445
|
+
|
446
|
+
/// <summary>
|
447
|
+
/// Makes the initial lower case.
|
448
|
+
/// </summary>
|
449
|
+
/// <param name="word">The word.</param>
|
450
|
+
/// <returns></returns>
|
451
|
+
public static string MakeInitialLowerCase(string word) {
|
452
|
+
return String.Concat(word.Substring(0, 1).ToLower(), word.Substring(1));
|
453
|
+
}
|
454
|
+
|
455
|
+
|
456
|
+
/// <summary>
|
457
|
+
/// Determine whether the passed string is numeric, by attempting to parse it to a double
|
458
|
+
/// </summary>
|
459
|
+
/// <param name="str">The string to evaluated for numeric conversion</param>
|
460
|
+
/// <returns>
|
461
|
+
/// <c>true</c> if the string can be converted to a number; otherwise, <c>false</c>.
|
462
|
+
/// </returns>
|
463
|
+
public static bool IsStringNumeric(string str) {
|
464
|
+
double result;
|
465
|
+
return (double.TryParse(str, NumberStyles.Float, NumberFormatInfo.CurrentInfo, out result));
|
466
|
+
}
|
467
|
+
|
468
|
+
/// <summary>
|
469
|
+
/// Adds the ordinal suffix.
|
470
|
+
/// </summary>
|
471
|
+
/// <param name="number">The number.</param>
|
472
|
+
/// <returns></returns>
|
473
|
+
public static string AddOrdinalSuffix(string number) {
|
474
|
+
if (IsStringNumeric(number)) {
|
475
|
+
int n = int.Parse(number);
|
476
|
+
int nMod100 = n % 100;
|
477
|
+
|
478
|
+
if (nMod100 >= 11 && nMod100 <= 13)
|
479
|
+
return String.Concat(number, "th");
|
480
|
+
|
481
|
+
switch (n % 10) {
|
482
|
+
case 1:
|
483
|
+
return String.Concat(number, "st");
|
484
|
+
case 2:
|
485
|
+
return String.Concat(number, "nd");
|
486
|
+
case 3:
|
487
|
+
return String.Concat(number, "rd");
|
488
|
+
default:
|
489
|
+
return String.Concat(number, "th");
|
490
|
+
}
|
491
|
+
}
|
492
|
+
return number;
|
493
|
+
}
|
494
|
+
|
495
|
+
/// <summary>
|
496
|
+
/// Converts the underscores to dashes.
|
497
|
+
/// </summary>
|
498
|
+
/// <param name="underscoredWord">The underscored word.</param>
|
499
|
+
/// <returns></returns>
|
500
|
+
public static string ConvertUnderscoresToDashes(string underscoredWord) {
|
501
|
+
return underscoredWord.Replace('_', '-');
|
502
|
+
}
|
503
|
+
|
504
|
+
|
505
|
+
#region Nested type: InflectorRule
|
506
|
+
|
507
|
+
/// <summary>
|
508
|
+
/// Summary for the InflectorRule class
|
509
|
+
/// </summary>
|
510
|
+
private class InflectorRule {
|
511
|
+
/// <summary>
|
512
|
+
///
|
513
|
+
/// </summary>
|
514
|
+
public readonly Regex regex;
|
515
|
+
|
516
|
+
/// <summary>
|
517
|
+
///
|
518
|
+
/// </summary>
|
519
|
+
public readonly string replacement;
|
520
|
+
|
521
|
+
/// <summary>
|
522
|
+
/// Initializes a new instance of the <see cref="InflectorRule"/> class.
|
523
|
+
/// </summary>
|
524
|
+
/// <param name="regexPattern">The regex pattern.</param>
|
525
|
+
/// <param name="replacementText">The replacement text.</param>
|
526
|
+
public InflectorRule(string regexPattern, string replacementText) {
|
527
|
+
regex = new Regex(regexPattern, RegexOptions.IgnoreCase);
|
528
|
+
replacement = replacementText;
|
529
|
+
}
|
530
|
+
|
531
|
+
/// <summary>
|
532
|
+
/// Applies the specified word.
|
533
|
+
/// </summary>
|
534
|
+
/// <param name="word">The word.</param>
|
535
|
+
/// <returns></returns>
|
536
|
+
public string Apply(string word) {
|
537
|
+
if (!regex.IsMatch(word))
|
538
|
+
return null;
|
539
|
+
|
540
|
+
string replace = regex.Replace(word, replacement);
|
541
|
+
if (word == word.ToUpper())
|
542
|
+
replace = replace.ToUpper();
|
543
|
+
|
544
|
+
return replace;
|
545
|
+
}
|
546
|
+
}
|
547
|
+
|
548
|
+
#endregion
|
549
|
+
}
|
550
|
+
|
551
|
+
#>
|