AceOO-2.0

From WowAce Wiki
Jump to: navigation, search

Example

 local AceOO = AceLibrary("AceOO-2.0")  
 
 local MyClass = AceOO.Class()
 
 function MyClass.prototype:DoSomething()
     print("Something happened!")
 end
 
 local myInstance = MyClass:new()
 
 myInstance:DoSomething() -- "Something happened!"

Be sure to check out AceOO-2.0 Tutorial for better examples.

API Documentation

.Factory(obj, newobj, ...)

Construct a factory for the creation of objects.

Args

obj 
Object - The object whose init method will be called on the new factory object.
newobj 
Object - The object whose init method will be called on the new objects that the Factory creates, to initialize them.
... 
Arguments which will be passed to obj.init() in addition to the Factory object.

Returns

Factory - The new factory which creates a newobj when its new method is called, or when it is called directly (__call metamethod).

Example

 local ClassFactory = AceOO.Factory(Object, Class, Object)

Object

Base of all objects, including Class.

:init(newobject, class)

Initialize a new object.

Args

newobject 
Object - The object to initialize
class 
Class - The class to make newobject inherit from

.uid

string - Unique ID for the object.

.inherits(object, parent)

Return whether an Object or Class inherits from a given parent.

Args

object 
Object or Class to check
parent 
Parent to test inheritance from (Class, Mixin, or Interface)

Returns

boolean - Whether an Object or Class inherits from a given parent.

Remarks

Testing for Interface inheritance works with implicit Interfaces. e.g. even if a class didn't explicitly inherit from an Interface, it would still check for API consistency. A Barkable mixin that implements Bark would technically pass inheritance of IBarkable, even if it wasn't explicitly stated.

Example

 if AceOO.inherits(myClass, parentClass) then
   -- do something
 end

.Class

An object factory which sets up inheritence and supports 'mixins' and 'interfaces'.

:new(...)

Construct a new object.

Args

Arguments to pass to the object init function.

Returns

instance - The new object.

Remarks

Cannot instantiate a new object if .virtual is set to true in the class. If a class has an interface, it is checked for validity on the first instantiation.

Example

 local myObject = MyClass:new()

:init([parent], ...)

Initialize a new class.

Args

[parent] 
Superclass or Mixin or Interface, if not given, Class is the superclass.
... 
Mixins and Interfaces to inherit from

Remarks

This is called when calling the class (as if it were a function). Every argument can take a string, which converts it into the appropriate AceLibrary instance. You would never call :init directly.

Example

 local MyClass = Class(superClass, "HandyMixin-2.0", "IBarkable")

:ToString()

Returns a string representation of the class.

Returns

string - a string representation of the class.

Example

 local text = myClass:ToString()
 -- or
 text = tostring(myClass)
 

.prototype

The inheritable table from which instances will base themselves on. Instances properly inherit from this, which in turn inherits from superclass's prototypes and so on. You would want to declare your instance methods on the prototype.

:init(...)

The initialization method of the class is called when :new(...) is run on the class, wtih the appropriate arguments (up to 20) passed through. You _must_ call the superclass's init method, otherwise an error will fire.

Args
... 
arguments
Example
 function MyClass.prototype:init()
   MyClass.super.prototype.init(self)
   
   -- do stuff here
 end
 local myObject = MyClass:new() -- same as MyClass()

.class

Class - class is a reference to the Class the prototype belongs to.

.super

Class - super is a reference to the parent class. _Never_ use self.class.super, it will cause breakage in situations that inherit from it.

.virtual

boolean - If set to true, then the class cannot be instantiated.

.sealed

boolean - If set to true, the class cannot be inherited from.

.Mixin

A class to create mixin objects, which contain methods that get "mixed in" to class prototypes.

.prototype

The prototype that mixin objects inherit their methods from.

:embed(target)

Mix in the methods of our object which are listed in our interface to the supplied target table. This may be overridden in special circumstances.

Args
target 
Class prototype - prototype to embed into.

:init(export, ...)

Initialize the mixin object.

Args
export 
The exported methods we implement (the list of methods our prototype provides which should be mixed into the target table by embed).
... 
Interfaces the mixin inherits from.
Example
 local MyMixin = AceOO.Mixin { "Method", "Method2" }

:activate(oldLib, oldDeactivate)

Activate function to be fed into AceLibrary's Register. If it is not used, errors will occur on upgrading.

Example
 AceLibrary:Register(MyMixin, "MyMixin-1.0", 1, MyMixin.activate)

.Interface

A class to create interfaces, which contain contracts that classes which inherit from this must comply with.

.prototype

The prototype that interface objects must adhere to.

:init(interface, ...)

Initialize the interface object.

Args
interface 
The interface we contract (the hash of fields forced).
... 
Superinterfaces
Remarks

It's good practice to start interface names with I, e.g. IComparable, IEnumerable

Example
 local ILovable = AceOO.Interface{
   Hug = "function",
   Lick = "function",
 }

.Classpool(superclass, ...)

Obtain a read only class from our pool of classes, indexed by the superclass and mixins.

Args

superclass 
The superclass of the class we want.
... 
Mixins of the class we want's objects.

Returns

A read only class from the class pool.

Remarks

Classpool is handy because it reuses classes with the same superclass/mixins. If it is called twice or more times with the same arguments (order not necessarily the same), it will only create one class.

Example

 local myClass = AceOO.ClassPool(superclass, mixin1, mixin2)