Fixing an issue with output parameters that are of type IntPtr
[castle.git] / Experiments / Generator / src / app / Extentions / Extentions.boo
blob91e110b0c5578697307f16a349ab52c469652cbe
1 namespace Generator.Extentions
3 import System.IO
4 import System.Text.RegularExpressions
6 # String class extention
7 def ToCapital(self as string) as string:
8 return self[:1].ToUpper() + self[1:].ToLower()
9 def ToTitle(self as string) as string:
10 return join(word.ToCapital() for word in self.Split(char(' ')))
11 def ToCamel(self as string) as string:
12 name = Regex.Replace(self, "(_|\\s)([a-z])", { m as Match | return m.Groups[2].ToString().ToUpper() })
13 return name[:1].ToUpper() + name[1:]
14 def ToClassName(self as string) as string:
15 return self.ToCamel()
16 def ToVarName(self as string) as string:
17 name = self.ToCapital()
18 return name[:1].ToLower() + name[1:]
19 def ToHumanName(self as string) as string:
20 return /[A-Z]/.Replace(self, ' $0').Trim().ToCapital()
21 def ToPlural(self as string) as string:
22 return Inflector.Instance.ToPlural(self)
23 def ToSingular(self as string) as string:
24 return Inflector.Instance.ToSingular(self)
25 def ToFileName(self as string) as string:
26 # All lower case for compatibility
27 return /_/.Replace(self, '').Trim().ToLower()
28 def ToPath(self as string) as string:
29 sep = Path.DirectorySeparatorChar.ToString()
30 return self.Replace("/", sep).Replace("\\", sep)
32 # Integer class extention
33 def Times(self as int, c as callable(int)):
34 for i in range(self):
35 c(i+1)
36 def ToOrdinal(self as int):
37 # Adapted from http://api.rubyonrails.com/classes/Inflector.html#M000814
38 if self % 100 in range(11, 14):
39 return "${self}th"
40 else:
41 if self % 10 == 1:
42 return "${self}st"
43 if self % 10 == 2:
44 return "${self}nd"
45 if self % 10 == 3:
46 return "${self}rd"
47 else:
48 return "${self}th"
50 # Lists extentions
51 def Each(self as System.Collections.IEnumerable, action as callable(object)):
52 for item in self:
53 action(item)
55 # Date and time extentions
56 def Ago(self as System.TimeSpan) as System.DateTime:
57 return System.DateTime.Now.Subtract(self)
59 def FromNow(self as System.TimeSpan) as System.DateTime:
60 return System.DateTime.Now.Add(self)