| 1 |
-------------------------------------------------------------------------------- |
|---|
| 2 |
-- Title: LUModule.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 LUFile = require( "LUFile" ) |
|---|
| 13 |
local LUList = require( "LUList" ) |
|---|
| 14 |
local LUString = require( "LUString" ) |
|---|
| 15 |
|
|---|
| 16 |
-- define the class |
|---|
| 17 |
local super = LUObject |
|---|
| 18 |
local self = super() |
|---|
| 19 |
|
|---|
| 20 |
-- initialization method |
|---|
| 21 |
function self:init( anEntity, aName ) |
|---|
| 22 |
self = super.init( self ) |
|---|
| 23 |
|
|---|
| 24 |
self._entity = anEntity |
|---|
| 25 |
self._name = aName |
|---|
| 26 |
|
|---|
| 27 |
return self |
|---|
| 28 |
end |
|---|
| 29 |
|
|---|
| 30 |
-- method to retrieve all the attribute names define by this module |
|---|
| 31 |
function self:attributeNames() |
|---|
| 32 |
local aList = LUList() |
|---|
| 33 |
local anEntity = self:entity() |
|---|
| 34 |
|
|---|
| 35 |
for aName, anAttribute in pairs( anEntity ) do |
|---|
| 36 |
local aType = type( anAttribute ) |
|---|
| 37 |
|
|---|
| 38 |
if aType ~= "function" and aType ~= "table" then |
|---|
| 39 |
aList:add( aName ) |
|---|
| 40 |
end |
|---|
| 41 |
end |
|---|
| 42 |
|
|---|
| 43 |
return aList:sort() |
|---|
| 44 |
end |
|---|
| 45 |
|
|---|
| 46 |
-- method to compare the given object with this object for equality |
|---|
| 47 |
function self:equals( anObject ) |
|---|
| 48 |
if self:isKindOf( anObject ) == true then |
|---|
| 49 |
if self:entity() == anObject:entity() then |
|---|
| 50 |
return true |
|---|
| 51 |
end |
|---|
| 52 |
end |
|---|
| 53 |
|
|---|
| 54 |
return false |
|---|
| 55 |
end |
|---|
| 56 |
|
|---|
| 57 |
-- method for to access this module underlying entity |
|---|
| 58 |
function self:entity() |
|---|
| 59 |
return self._entity |
|---|
| 60 |
end |
|---|
| 61 |
|
|---|
| 62 |
-- method to retrieve all the function names define by this module |
|---|
| 63 |
function self:functionNames() |
|---|
| 64 |
local aList = LUList() |
|---|
| 65 |
local anEntity = self:entity() |
|---|
| 66 |
|
|---|
| 67 |
for aName, aFunction in pairs( anEntity ) do |
|---|
| 68 |
if type( aFunction ) == "function" then |
|---|
| 69 |
aList:add( aName ) |
|---|
| 70 |
end |
|---|
| 71 |
end |
|---|
| 72 |
|
|---|
| 73 |
return aList:sort() |
|---|
| 74 |
end |
|---|
| 75 |
|
|---|
| 76 |
-- method to retrieve this module name |
|---|
| 77 |
function self:name() |
|---|
| 78 |
return self._name |
|---|
| 79 |
end |
|---|
| 80 |
|
|---|
| 81 |
-- method to check if this is a native module |
|---|
| 82 |
function self:isNative() |
|---|
| 83 |
local aPath = self:path() |
|---|
| 84 |
|
|---|
| 85 |
if aPath ~= nil then |
|---|
| 86 |
if LUString:endsWith( aPath:lower(), ".lua" ) == true then |
|---|
| 87 |
return false |
|---|
| 88 |
end |
|---|
| 89 |
end |
|---|
| 90 |
|
|---|
| 91 |
return true |
|---|
| 92 |
end |
|---|
| 93 |
|
|---|
| 94 |
-- method to retrieve this module path |
|---|
| 95 |
function self:path() |
|---|
| 96 |
return nil |
|---|
| 97 |
end |
|---|
| 98 |
|
|---|
| 99 |
-- method to retrieve this module version |
|---|
| 100 |
function self:version() |
|---|
| 101 |
local anEntity = self:entity() |
|---|
| 102 |
local aVersion = anEntity[ "_VERSION" ] |
|---|
| 103 |
|
|---|
| 104 |
if aVersion == nil then |
|---|
| 105 |
aVersion = anEntity[ "version" ] |
|---|
| 106 |
end |
|---|
| 107 |
|
|---|
| 108 |
return aVersion |
|---|
| 109 |
end |
|---|
| 110 |
|
|---|
| 111 |
-- method for string representation |
|---|
| 112 |
function self:toString() |
|---|
| 113 |
return self:name() |
|---|
| 114 |
end |
|---|
| 115 |
|
|---|
| 116 |
-- method to access all the known module names |
|---|
| 117 |
function self:moduleNames() |
|---|
| 118 |
local somePackages = package[ "loaded" ] |
|---|
| 119 |
local aList = LUList() |
|---|
| 120 |
|
|---|
| 121 |
for aName, anEntity in pairs( somePackages ) do |
|---|
| 122 |
if aName == aName:lower() and type( anEntity ) == "table" then |
|---|
| 123 |
aList:add( aName ) |
|---|
| 124 |
end |
|---|
| 125 |
end |
|---|
| 126 |
|
|---|
| 127 |
return aList:sort() |
|---|
| 128 |
end |
|---|
| 129 |
|
|---|
| 130 |
-- method to access a known module |
|---|
| 131 |
function self:moduleWithName( aName ) |
|---|
| 132 |
local anEntity = package[ "loaded" ][ aName ] |
|---|
| 133 |
|
|---|
| 134 |
if aName == "_G" then |
|---|
| 135 |
anEntity = _G |
|---|
| 136 |
end |
|---|
| 137 |
|
|---|
| 138 |
if anEntity == nil then |
|---|
| 139 |
local aStatus, anError = pcall( require, aName ) |
|---|
| 140 |
|
|---|
| 141 |
if aStatus == true then |
|---|
| 142 |
anEntity = package[ "loaded" ][ aName ] |
|---|
| 143 |
else |
|---|
| 144 |
return nil, anError |
|---|
| 145 |
end |
|---|
| 146 |
end |
|---|
| 147 |
|
|---|
| 148 |
if type( anEntity ) == "table" then |
|---|
| 149 |
return self:new( anEntity, aName ) |
|---|
| 150 |
end |
|---|
| 151 |
|
|---|
| 152 |
return nil |
|---|
| 153 |
end |
|---|
| 154 |
|
|---|
| 155 |
return self |
|---|