root/DB/DBModelNaming.lua

Revision 868 (checked in by rsz, 3 years ago)

cleanup

Line 
1 --------------------------------------------------------------------------------
2 -- Title:               DBModelNaming.lua
3 -- Description:         Like a square peg in a round hole
4 -- Author:              Raphaël Szwarc http://alt.textdrive.com/lua/
5 -- Creation Date:       February 1, 2005
6 -- Legal:               Copyright (C) 2005 Raphaël Szwarc
7 --------------------------------------------------------------------------------
8
9 -- import dependencies
10 local LUObject = require( "LUObject" )
11 local LUString = require( "LUString" )
12
13 -- define the class
14 local super = LUObject
15 local self = super()
16
17 -- class variable(s)
18 local _singulars = nil
19
20 function self:singulars()
21         if self._singulars == nil then
22                 self._singulars =
23                 {
24                         { "([^aeiouy])ies$", "%1y" },   -- cities -> city
25                         { "(qu)ies$", "%1y" },           
26                        
27                         { "(x)es$", "%1" },             -- boxes -> box
28                         { "(ch)es$", "%1" },            -- watches -> watch
29                         { "(ss)es$", "%1" },            -- glasses -> glass
30                        
31                         { "oes$", "o" },                -- tomatoes -> tomato
32                        
33                         { "([lr])ves$", "%1f" },
34                         { "([^f])ves$", "%1fe" },
35                        
36                         { "([ti])a$", "%1um" },
37                        
38                         { "men$", "man" },
39                         { "status$", "status" },
40                         { "children$", "child" },
41                        
42                         { "s$", "" },
43                 }
44         end
45        
46         return self._singulars
47 end
48
49 function self:singular( aPlural )
50         if aPlural ~= nil then
51                 for _, aRule in ipairs( self:singulars() ) do
52                         local aPattern = aRule[ 1 ]
53                         local aSubstitution = aRule[ 2 ]
54                         local aSingular, aCount = aPlural:gsub( aPattern, aSubstitution )
55                        
56                         if aCount ~= 0 then
57                                 return aSingular
58                         end
59                 end
60         end
61        
62         return aPlural
63 end
64
65 function self:idName()
66         return "id"
67 end
68
69 function self:idSuffix()
70         return LUString:capitalize( self:idName() )
71 end
72
73 function self:dbName( aName )
74         if aName ~= nil then
75                 aName = LUString:methodNameComponents( aName ):join( "_" ):lower()
76         end
77        
78         return aName
79 end
80
81 return self
Note: See TracBrowser for help on using the browser.