root/lu/LUBundle.lua

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

cleanup

Line 
1 --------------------------------------------------------------------------------
2 -- Title:               LUBundle.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 LUFile = require( "LUFile" )
12 local LUList = require( "LUList" )
13 local LUMap = require( "LUMap" )
14 local LUString = require( "LUString" )
15 local debug = require( "debug" )
16
17 -- define the class
18 local super = LUObject
19 local self = super()
20
21 -- class variable(s)
22 local _bundles = LUMap()
23
24 -- initialization method
25 function self:init( anEntity, aName )
26         self = super.init( self )
27        
28         self._entity = anEntity
29         self._name = aName
30        
31         return self
32 end
33
34 -- method to access this bundle underlying entity
35 function self:entity()
36         return self._entity
37 end
38
39 -- method to compare the given object with this object for equality
40 function self:equals( anObject )
41         if self:isKindOf( anObject ) == true then
42                 if rawequal( self:entity(), anObject:entity() ) == true then
43                         return true
44                 end
45         end
46        
47         return false
48 end
49
50 -- method to retrieve this bundle name
51 function self:name()
52         if self._name == nil then
53                 self._name = self:entity():className()
54         end
55        
56         return self._name
57 end
58
59 -- method to retrieve this bundle path
60 function self:path()
61         if self._path == nil then
62                 self._path = self:pathWithObject( self:entity() )
63         end
64        
65         return self._path
66 end
67
68 -- method to access a file with a given name
69 function self:fileWithName( aName, aType, aDirectory, aLocale )
70         local  aPath = self:path()
71        
72         if aDirectory ~= nil then
73                 aPath = aPath .. LUFile:separator() .. aDirectory
74         end
75        
76         if aName ~= nil then
77                 if aLocale ~= nil then
78                         aName = aName .. "." .. aLocale
79                 end
80                
81                 if aType ~= nil then
82                         aName = aName .. "." .. aType
83                 end
84         end
85        
86         return LUFile( aPath, aName )
87 end
88
89 -- method for string representation
90 function self:toString()
91         return self:name()
92 end
93
94 -- method to access all the known bundle names
95 function self:bundleNames()
96         local somePackages = package[ "loaded" ]
97         local aList = LUList()
98
99         for aName, anEntity in pairs( somePackages ) do
100                 if LUObject:isObject( anEntity ) == true then
101                         aList:add( aName )
102                 end
103         end
104        
105         return aList:sort()
106 end
107
108 -- method to access the cached bundles
109 function self:bundles()       
110         return _bundles
111 end
112
113 -- method to access a known bundle
114 function self:bundleWithName( aName )
115         local someBundles = self:bundles()
116         local aBundle = someBundles:get( aName )
117        
118         if aBundle == nil then
119                 local anEntity = package[ "loaded" ][ aName ]
120                
121                 if anEntity == nil then
122                         local aStatus, anError = pcall( require, aName )
123                        
124                         if aStatus == true then
125                                 anEntity = package[ "loaded" ][ aName ]
126                         else
127                                 return nil, anError
128                         end
129                 end
130                
131                 if LUObject:isObject( anEntity ) == true then
132                         aBundle = self:new( anEntity, aName )
133                        
134                         someBundles:put( aName, aBundle )
135                 end
136         end
137        
138         return aBundle
139 end
140
141 -- method to return a bundle for a given object
142 function self:bundleWithObject( anObject )
143         if anObject ~= nil then
144                 return self:bundleWithName( anObject:className() )
145         end
146        
147         return  nil
148 end
149
150 function self:pathWithObject( anObject )
151         local aClass = anObject:class()
152
153         for aKey, aValue in pairs( aClass ) do
154                 if LUString:startsWith( aKey, "_" ) == false and type( aValue ) == "function" then
155                         return self:pathWithMethod( aValue )
156                 end
157         end
158        
159         return nil
160 end
161
162 function self:pathWithMethod( aMethod )
163         local someInfo = debug.getinfo( aMethod, "S" )
164         local aPath = someInfo[ "source" ]
165        
166         if aPath ~= nil and LUString:startsWith( aPath, "@" ) == true then
167                 local someComponents = LUFile:pathComponents( aPath )
168                
169                 if someComponents:size() > 1 then
170                         aPath = someComponents:removeLast():join( LUFile:separator() )
171                 end
172                
173                 return aPath:sub( 2, aPath:len() )
174         end
175        
176         return nil
177 end
178
179 return self
Note: See TracBrowser for help on using the browser.