root/lu/LUClass.lua

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

cleanup

Line 
1 --------------------------------------------------------------------------------
2 -- Title:               LUClass.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 LUBundle = require( "LUBundle" )
12 local LUList = require( "LUList" )
13 local LUString = require( "LUString" )
14
15 -- define the class
16 local super = LUObject
17 local self = super()
18
19 -- initialization method
20 function self:init( anEntity )
21         self = super.init( self )
22        
23         self._entity = anEntity
24        
25         return self
26 end
27
28 -- method to access this class bundle
29 function self:bundle()
30         return LUBundle:bundleWithName( self:name() )
31 end
32        
33 -- method to access the underlying entity
34 function self:entity()
35         return self._entity
36 end
37
38 -- method to compare the given object with this object for equality
39 function self:equals( anObject )
40         if self:isKindOf( anObject ) == true then
41                 if rawequal( self:entity(), anObject:entity() ) == true then
42                         return true
43                 end
44         end
45        
46         return false
47 end
48
49 -- method for retrieving all the method names define by this entity
50 function self:methodNames()
51         local aList = LUList()
52         local anEntity = self:entity()
53        
54         for aMethod, aFunction in pairs( anEntity ) do
55                 if LUString:startsWith( aMethod, "_" ) == false and type( aFunction ) == "function" then
56                        aList:add( aMethod )
57                 end
58         end
59
60         return aList:sort()
61 end
62
63 -- method to retrieve the class name
64 function self:name()
65         return self:entity():className()
66 end
67
68 -- method to access the super class
69 function self:superClass()
70         if self._superClass == nil then
71                 local aParent = self:entity():super()
72                
73                 if aParent ~= nil then
74                         self._superClass = self:new( aParent )
75                 end
76         end
77
78         return self._superClass
79 end
80
81 -- method for string representation
82 function self:toString()
83         return self:name()
84 end
85
86 -- method to access all the known classes
87 function self:classes()
88         local someClasses = LUList()
89        
90         for _, aClassName in self:classNames():iterator() do
91                 someClasses:add( self:classWithName( aClassName ) )
92         end
93        
94         return someClasses
95 end
96
97 -- method to access all the known class names
98 function self:classNames()
99         return LUBundle:bundleNames()
100 end
101
102 -- method to return a class for a given object
103 function self:classWithObject( anObject )
104         if anObject ~= nil then
105                 return self:classWithName( anObject:className() )
106         end
107        
108         return  nil
109 end
110
111 -- method to access a known classs
112 function self:classWithName( aName )
113         local aBundle, aStatus = LUBundle:bundleWithName( aName )
114        
115         if aBundle ~= nil then
116                 return self:new( aBundle:entity() )
117         end
118        
119         return nil, aStatus
120 end
121
122 return self
Note: See TracBrowser for help on using the browser.