14

Root

The Root class implements the base messages of the So-o model. All the other classes inherit from the Root class.

All the code for the So-o Root class is in the file Root.js.

  1. import { defclass } from 'So-o';
  2. import * as OL from 'OL';

Imports the function defclass from So-o and all the functions defined by the Object Layer.

  1. defclass('Root',
  2.     // superclass
  3.     null,
  4.     // revision
  5.     1,
  6.     // class properties
  7.     null,
  8.     // instance properties
  9.     null,

The Root class is the only class which doesn't inherit from a superclass. It doesn't have any class or instance properties. It implements all the base messages of the So-o model.

CLASS METHODS

get
set
SYNOPSIS

get(self, attr)

set(self, attr, val)

DESCRIPTION

get returns the value of the property attr of self.

set sets the value of the property attr of self to val.

IMPORTANT: Copy the content of an attribute which is array or an object if you want to modify it without changing the value of the attribute.

CODE
  1.     {   'get':          (self, attr) => OL.class_get(self, attr),
  2.         'set':          (self, attr, val) => OL.class_set(self, attr, val),

get and set return the result of the functions class_get and class_set of the Object Layer.

make
new
SYNOPSIS

make(self)

new(self[, arg ...])

DESCRIPTION

make returns a new instance of self.

new creates an instance of self, sends it the message init with all the parameters of the message following self and returns the new initialized instance.

IMPORTANT: Always create an instance with new.

CODE
  1.         'new':          (self, ...args) => OL.object_send_message(OL.class_send_message(self, 'make'), 'init', args),
  2.         'make':         (self) => OL.class_make(self),

make returns the result of the function class_make of the Object Layer.

new creates an instance by sending make to self, sends it init with the parameters of the message following self and returns the new instance.

free
SYNOPSIS

free(self)

DESCRIPTION

free frees the space allocated by self.

The free method defined by the Root class does nothing.

NOTE: free could be automatically sent by the Object Layer to a class which is not referenced anymore.

CODE
  1.         'free':         (self) => undefined,

free does nothing and returns undefined.

initialize
SYNOPSIS

initialize(self)

DESCRIPTION

initialize is automatically sent by defclass to a new class. The initialize method defined by the Root class does nothing.

initialize is usually redefined by a class which must initialize its class properties.

CODE
  1.         'initialize':   (self) => self,

initialize does nothing and returns self.

class
name
revision
superclass
SYNOPSIS

class(self)

name(self)

revision(self)

superclass(self)

DESCRIPTION

class returns the class of the class self, i.e. self.

name returns the name of the class self.

revision returns the revision number of the class self.

superclass returns the superclass of the class self, null if self is Root, the Root class.

CODE
  1.         'class':        (self) => self,
  2.         'name':         (self) => OL.class_name(self),
  3.         'revision':     (self) => OL.class_revision(self),
  4.         'superclass':   (self) => OL.class_superclass(self),

class returns self. name, revision and superclass return the result of the functions class_name, class_revision and class_superclass of the Object Layer.

classMessages
instanceMessages
SYNOPSIS

classMessages(self, inherit = true)

instanceMessages(self, inherit = true)

  1.         'classMessages':
  2.             (self, inherit = true) => {
  3.                 let messages = OL.class_class_messages(self);
  4.  
  5.                 if (messages)
  6.                     messages = Object.keys(messages);
  7.  
  8.                 if (inherit) {
  9.                     let sc = OL.class_superclass(self);
  10.  
  11.                     if (sc) {
  12.                         let inherited = OL.class_send_message(sc, 'classMessages');
  13.  
  14.                         if (inherited)
  15.                             messages = messages ? [... new Set([...messages, ...inherited])] : inherited;
  16.                     }
  17.                 }
  18.  
  19.                 return messages && messages.length > 0 ? messages : null;
  20.             },
  21.         'instanceMessages':
  22.             (self, inherit = true) => {
  23.                 let messages = OL.class_instance_messages(self);
  24.  
  25.                 if (messages)
  26.                     messages = Object.keys(messages);
  27.  
  28.                 if (inherit) {
  29.                     let sc = OL.class_superclass(self);
  30.  
  31.                     if (sc) {
  32.                         let inherited = OL.class_send_message(sc, 'instanceMessages');
  33.  
  34.                         if (inherited)
  35.                             messages = messages ? [... new Set([...messages, ...inherited])] : inherited;
  36.                     }
  37.                 }
  38.  
  39.                 return messages && messages.length > 0 ? messages : null;
  40.             },

classMessages sets messages to the list of the class messages of self, adds the class messages of the superclasses of self, deleting duplicates, if inherit is true then returns messages. instanceMessages sets messages to the list of the instance messages of self, adds the instance messages of the superclasses of self, deleting duplicates, if inherit is true then returns messages.

classProperties
instanceProperties
SYNOPSIS

classProperties(self, inherit = true)

instanceProperties(self, inherit = true)

  1.         'classProperties':
  2.             (self, inherit = true) => {
  3.                 let properties = OL.class_class_properties(self);
  4.  
  5.                 if (properties)
  6.                     properties = [... properties];
  7.  
  8.                 if (inherit) {
  9.                     let sc = OL.class_superclass(self);
  10.  
  11.                     if (sc) {
  12.                         let inherited_properties = OL.class_send_message(sc, 'classProperties');
  13.    
  14.                         if (inherited_properties)
  15.                             properties = properties ? [... new Set([...properties, ...inherited_properties])] : inherited_properties;
  16.                     }
  17.                 }
  18.  
  19.                 return properties && properties.length > 0 ? properties : null;
  20.             },
  21.         'instanceProperties':
  22.             (self, inherit = true) => {
  23.                 let properties = OL.class_instance_properties(self);
  24.  
  25.                 if (properties)
  26.                     properties = [... properties];
  27.  
  28.                 if (inherit) {
  29.                     let sc = OL.class_superclass(self);
  30.  
  31.                     if (sc) {
  32.                         let inherited_properties = OL.class_send_message(sc, 'instanceProperties');
  33.    
  34.                         if (inherited_properties)
  35.                             properties = properties ? [... new Set([...properties, ...inherited_properties])] : inherited_properties;
  36.                     }
  37.                 }
  38.  
  39.                 return properties && properties.length > 0 ? properties : null;
  40.             },

classProperties sets properties to the list of the class properties of self, adds the class properties of the superclasses of self, deleting duplicates, if inherit is true then returns properties. instanceProperties sets properties to the list of the instance properties of self, adds the instance properties of the superclasses of self, deleting duplicates, if inherit is true then returns properties.

classMethodFor
instanceMethodFor
SYNOPSIS

classMethodFor(self, msg)

instanceMethodFor(self, msg)

DESCRIPTION

classMethodFor returns the function which implements the class message msg of self.

instanceMethodFor returns the function which implements the instance message msg of self.

CODE
  1.         'classMethodFor':           (self, msg) => OL.class_find_class_method(self, msg),
  2.         'instanceMethodFor':        (self, msg) => OL.class_find_instance_method(self, msg),

classMethodFor and instanceMethodFor return the result of the functions class_find_class_method and class_find_instance_method of the Object Layer.

addClassMessage
removeClassMessage
addInstanceMessage
removeInstanceMessage
SYNOPSIS

addClassMessage(self, msg)

removeClassMessage(self, msg)

addInstanceMessage(self, msg)

removeInstanceMessage(self, msg)

DESCRIPTION

addClassMessage adds the class message msg to self.

removeClassMessage removes the class message msg from self.

addInstanceMessage adds the instance message msg to self.

removeInstanceMessage removes the instance message msg from self.

CODE
  1.         'addClassMessage':          (self, msg, f) => OL.class_add_class_message(self, msg, f),
  2.         'removeClassMessage':       (self, msg) => OL.class_remove_class_message(self, msg),
  3.         'addInstanceMessage':       (self, msg, f) => OL.class_add_instance_message(self, msg, f),
  4.         'removeInstanceMessage':    (self, msg) => OL.class_remove_instance_message(self, msg),

addClassMessage, removeClassMessage, addInstanceMessage and removeInstanceMessage return the result of the functions class_add_class_message, class_remove_class_message, class_add_instance_message and class_remove_instance_message of the Object Layer.

addClassProperty
removeClassProperty
addInstanceProperty
removeInstanceProperty
SYNOPSIS

addClassProperty(self, prop)

removeClassProperty(self, prop)

addInstanceProperty(self, prop)

removeInstanceProperty(self, prop)

DESCRIPTION

addClassProperty adds the class property prop to self.

removeClassProperty removes the class property prop from self.

addInstanceProperty adds the instance property prop to self.

removeInstanceProperty removes the instance property prop from self.

CODE
  1.         'addClassProperty':         (self, prop) => OL.class_add_class_property(self, prop),
  2.         'removeClassProperty':      (self, prop) => OL.class_remove_class_property(self, prop),
  3.         'addInstanceProperty':      (self, prop) => OL.class_add_instance_property(self, prop),
  4.         'removeInstanceProperty':   (self, prop) => OL.class_remove_instance_property(self, prop),

addClassProperty, removeClassProperty, addInstanceProperty and removeInstanceProperty return the result of the functions class_add_class_property, class_remove_class_property, class_add_instance_property and class_remove_instance_property of the Object Layer.

perform
SYNOPSIS

perform(self, msg, args = false)

DESCRIPTION

perform returns the result of sending the message msg to self with args in argument. args is an array. By default, the message has no argument.

CODE
  1.         'perform':  (self, msg, args = false) => OL.class_send_message(self, msg, args),

perform returns the result of sending msg and args to self.

read
write
SYNOPSIS

read(self, data)

write(self)

DESCRIPTION

write returns the serialization of the attributes of self.

read initializes the attributes of self with data. data is a serialized object.

CODE
  1.         'read':     (self, sdata) => {
  2.                         let properties = OL.class_send_message(self, 'classProperties');
  3.  
  4.                         if (!properties)
  5.                             return self;
  6.  
  7.                         let data = JSON.parse(sdata);
  8.                        
  9.                         if (typeof data !== 'object')
  10.                             throw new TypeError();
  11.  
  12.                         let attributes = {};
  13.  
  14.                         for (let p of properties) {
  15.                             if (data.hasOwnProperty(p))
  16.                                 attributes[p] = data[p];
  17.                         }
  18.                            
  19.                         return OL.class_set_attributes(self, attributes);
  20.                     },
  21.         'write':    (self) => JSON.stringify(OL.class_attributes(self)),

write calls the function JSON.stringify with in argument the list of attributes of self and returns the result. read obtains the list of properties of self, calls the function JSON.parse with data in argument, checks if the result is an object, then initializes every attribute of self with the corresponding value extracted from this object.

error
doesNotContain
doesNotRecognize
notImplemented
subclassResponsibility
SYNOPSIS

error(self, err[, arg ...])

doesNotContain(self, prop)

doesNotRecognize(self, msg)

notImplemented(self, msg)

subclassResponsibility(self, msg)

DESCRIPTION

error displays on the console an error message whose text is formatted by replacing every field {n} in the string err by the corresponding parameter following err numbering them from 0.

doesNotContain sends the message error to self with in argument the constant Root.InvalidClassProperty, the name of the class self and prop.

doesNotRecognize sends the message error to self with in argument the constant Root.InvalidClassMessage, the name of the class self and msg.

notImplemented sends the message error to self with in argument the constant Root.NotImplemented, the name of the class self and msg.

subclassResponsibility sends the message error to self with in argument the constant Root.SubclassResponsibility, the name of the class self and msg.

doesNotContain and doesNotRecognize are sent by the Object Layer. notImplemented is generally sent by a method which isn't coded yet while subclassResponsibility is sent by a method which must be implemented by a subclass of an abstract class.

CODE
  1.         'error':    (self, err, ...args) => {
  2.                         let errmsg = err;
  3.  
  4.                         for (let i = 0; i < args.length; i++)
  5.                             errmsg = errmsg.replace(new RegExp(`\\{${i}\\}`, 'gi'), args[i]);
  6.  
  7.                         console.error(errmsg);
  8.  
  9.                         return self;
  10.                     },
  11.         'doesNotContain':           (self, prop) => OL.class_send_message(self, 'error', [Root.InvalidClassProperty, OL.class_name(self), prop]),
  12.         'doesNotRecognize':         (self, msg) => OL.class_send_message(self, 'error', [Root.InvalidClassMessage, OL.class_name(self), msg]),
  13.         'notImplemented':           (self, msg) => OL.class_send_message(self, 'error', [Root.NotImplemented, OL.class_name(self), msg]),
  14.         'subclassResponsibility':   (self, msg) => OL.class_send_message(self, 'error', [Root.SubclassResponsibility, OL.class_name(self), msg]),
  15.     },

error formats the error message errmsg by replacing in turn in err the expressions with the format {n} by the arguments following err and displays it on the error output flow of the console. doesNotContain, doesNotRecognize, notImplemented and subclassResponsibility send the message error to self with in argument the constant of the corresponding error message and its parameters.

INSTANCE METHODS

get
set
SYNOPSIS

get(self, attr)

set(self, attr, val)

DESCRIPTION

get returns the value of the property attr of self.

set sets the value of the property attr of self to val.

IMPORTANT: Copy the content of an attribute which is array or an object if you want to modify it without changing the value of the attribute.

CODE
  1.     {   'get':          (self, attr) => OL.object_get(self, attr),
  2.         'set':          (self, attr, val) => OL.object_set(self, attr, val),

get and set return the result of the functions object_get and object_set of the Object Layer.

init
SYNOPSIS

init(self[, arg ...])

DESCRIPTION

init is automatically sent by new to a new instance. The init method defined by the Root class does nothing.

init is usually redefined by a class which must initialize its instance properties.

CODE
  1.         'init':         (self) => self,

init does nothing and returns self.

free
SYNOPSIS

free(self)

DESCRIPTION

free frees the space allocated by self.

The free method defined by the Root class does nothing.

NOTE: free could be automatically sent by the Object Layer to a instance which is not referenced anymore.

CODE
  1.         'free':         (self) => undefined,

free does nothing and returns undefined.

class
superclass
messages
properties
SYNOPSIS

class(self)

superclass(self)

messages(self, inherit=true)

properties(self, inherit=true)

DESCRIPTION

class returns the class of self.

superclass returns the superclass of the class of self, null if self is an instance of the Root class.

messages returns the messages recognized by self in an array. If inherit is true, messages also returns the messages inherited from all the superclasses of self. inherit is true by default.

instanceProperties returns the properties of self in an array. If inherit is true, properties also returns the properties inherited from all the superclasses of self. inherit is true by default.

CODE
  1.         'class':        (self) => OL.object_class(self),
  2.         'superclass':   (self) => OL.class_superclass(OL.object_class(self)),
  3.         'messages':     (self, inherit = true) => OL.class_send_message(OL.object_class(self), 'instanceMessages', [inherit]),
  4.         'properties':   (self, inherit = true) => OL.class_send_message(OL.object_class(self), 'instanceProperties', [inherit]),

class returns the result of the function object_class of the Object Layer. superclass returns the result of the function class_superclass with the class of self in argument. messages sends the message instanceMessages to the class of self with inherit in argument. properties sends the message instanceProperties to the class of self with inherit in argument.

isKindOf
methodFor
respondsTo
assume
SYNOPSIS

isKindOf(self, c)

methodFor(self, msg)

respondsTo(self, msg)

assume(self, c)

DESCRIPTION

isKindOf returns true if self is an instance of the class or a subclass of c or false if not.

methodFor returns the function which implements the message msg for self or false if self doesn't respond to msg.

respondsTo returns true if self has a method for the message msg, false if not.

assume changes the class of self to c. The values of the properties of the former class of self which are not part of the class c are preserved but they are not accessible.

CODE
  1.         'respondsTo':   (self, msg) => OL.class_find_instance_method_class(OL.object_class(self), msg) ? true : false,
  2.         'methodFor':    (self, msg) => OL.class_find_instance_method(OL.object_class(self), msg),
  3.         'isKindOf':     (self, c) => OL.class_is_kind_of(OL.object_class(self), c),
  4.         'assume':       (self, c) => OL.object_assume(self, c),

isKindOf returns the result of the function class_is_kind_of of the Object Layer with in argument the class of self and c. methodFor returns the result of the function class_find_instance_method with in argument the class of self and msg. respondsTo returns true if the result of the function class_find_instance_method with in argument the class of self and msg isn't false, true otherwise. assume calls object_assume with c in argument.

copy
SYNOPSIS

copy(self)

DESCRIPTION

copy returns a copy of self. IMPORTANT: object_copy doesn't duplicate the values of the attributes of self. If a class has properties which are arrays or objects, a redefinition of the method is necessary.

CODE
  1.         'copy':         (self) => OL.object_copy(self),

copy returns the result of the function object_copy of the Object Layer.

toString
SYNOPSIS

toString($self)

DESCRIPTION

toString returns a readable representation of self. The toString method of the Root class returns an empty string.

CODE
  1.         'toString':     (self) => '',

toString returns an empty string.

delegate
setDelegate
SYNOPSIS

delegate(self, msg=false[, arg ...])

setDelegate(self, $delegate)

DESCRIPTION

delegate returns the result of sending the message msg and the arguments arg following msg to the delegate of self. If self doesn't have a delegate or if the delegate of self doesn't respond to the message msg, delegate returns false. If msg is false, delegate returns the delegate of self.

setDelegate initializes the delegate property of self with $delegate.

NOTE: Sending delegate or setDelegate to an instance which does have the property delegate sends the message doesNotContain to the instance.

CODE
  1.         'delegate':     (self, msg = false, ...args) => {
  2.                             let delegate = OL.object_get(self, 'delegate');
  3.  
  4.                             if (!msg)
  5.                                 return delegate;
  6.  
  7.                             if (!delegate)
  8.                                 return false;
  9.  
  10.                             if (!OL.object_send_message(delegate, 'respondsTo', [msg]))
  11.                                 return false;
  12.            
  13.                             return OL.object_send_message(delegate, msg, args);
  14.                         },
  15.         'setDelegate':  (self, delegate) => {
  16.                             if (! (delegate === null || (typeof delegate === 'object' && delegate instanceof OL.Instance)))
  17.                                 throw new TypeError();
  18.  
  19.                             return OL.object_set(self, 'delegate', delegate);
  20.                         },

delegate sets delegate to the valeur of the property delegate of self, returns delegate if msg is false, returns false if delegate is null or if delegate doesn't respond to msg, returns the result of sending msg with the arguments of delegate following msg to delegate. setDelegate checks if delegate is null or an object then sets the property delegate of self to delegate.

perform
SYNOPSIS

perform(self, msg, args = false)

DESCRIPTION

perform returns the result of sending the message msg to self with args in argument. args is an array. By default, the message has no argument.

CODE
  1.         'perform':  (self, msg, args = false) => OL.object_send_message(self, msg, args),

perform returns the result of sending msg and args to self.

read
write
SYNOPSIS

read(self, data)

write(self)

DESCRIPTION

write returns the serialization of the attributes of self.

read initializes the attributes of self with data. data is a serialized object.

CODE
  1.         'read':     (self, sdata) => {
  2.                         let properties = OL.class_send_message(OL.object_class(self), 'instanceProperties');
  3.  
  4.                         if (!properties)
  5.                             return self;
  6.  
  7.                         let data = JSON.parse(sdata);
  8.                        
  9.                         if (typeof data !== 'object')
  10.                             throw new TypeError();
  11.  
  12.                         let attributes = {};
  13.  
  14.                         for (let p of properties) {
  15.                             if (data.hasOwnProperty(p))
  16.                                 attributes[p] = data[p];
  17.                         }
  18.                            
  19.                         return OL.object_set_attributes(self, attributes);
  20.                     },
  21.         'write':    (self) => JSON.stringify(OL.object_attributes(self)),

write calls the function JSON.stringify with in argument the list of attributes of self and returns the result. read obtains the list of properties of self, calls the function JSON.parse with data in argument, checks if the result is an object, then initializes every attribute of self with the corresponding value extracted from this object.

error
doesNotContain
doesNotRecognize
notImplemented
subclassResponsibility
SYNOPSIS

error(self, err[, arg ...])

doesNotContain(self, prop)

doesNotRecognize(self, msg)

notImplemented(self, msg)

subclassResponsibility(self, msg)

DESCRIPTION

error displays on the console an error message whose text is formatted by replacing every field {n} in the string err by the corresponding parameter following err numbering them from 0.

doesNotContain sends the message error to self with in argument the constant Root.InvalidClassProperty, the name of the class of self and prop.

doesNotRecognize sends the message error to self with in argument the constant Root.InvalidClassMessage, the name of the class of self and msg.

notImplemented sends the message error to self with in argument the constant Root.NotImplemented, the name of the class of self and msg.

subclassResponsibility sends the message error to self with in argument the constant Root.SubclassResponsibility, the name of the class self and msg.

doesNotContain and doesNotRecognize are sent by the Object Layer. notImplemented is generally sent by a method which isn't coded yet while subclassResponsibility is sent by a method which must be implemented by a subclass of an abstract class.

CODE
  1.         'error':    (self, err, ...args) => {
  2.                         let errmsg = err;
  3.  
  4.                         for (let i = 0; i < args.length; i++)
  5.                             errmsg = errmsg.replace(new RegExp(`\\{${i}\\}`, 'gi'), args[i]);
  6.  
  7.                         console.error(errmsg);
  8.  
  9.                         return self;
  10.                     },
  11.         'doesNotContain':           (self, prop) => OL.object_send_message(self, 'error', [Root.InvalidInstanceProperty, OL.class_name(OL.object_class(self)), prop]),
  12.         'doesNotRecognize':         (self, msg) => OL.object_send_message(self, 'error', [Root.InvalidInstanceMessage, OL.class_name(OL.object_class(self)), msg]),
  13.         'notImplemented':           (self, msg) => OL.object_send_message(self, 'error', [Root.NotImplemented, OL.class_name(OL.object_class(self)), msg]),
  14.         'subclassResponsibility':   (self, msg) => OL.object_send_message(self, 'error', [Root.SubclassResponsibility, OL.class_name(OL.object_class(self)), msg]),
  15.     }
  16. );

error formats the error message errmsg by replacing in turn in err the expressions with the format {n} by the arguments following err and displays it on the error output flow of the console. doesNotContain, doesNotRecognize, notImplemented and subclassResponsibility send the message error to self with in argument the constant of the corresponding error message and its parameters.

  1. Root.InvalidClassProperty = '{0}::{1} Invalid class property';
  2. Root.InvalidClassMessage = '{0}::{1} Invalid class message';
  3. Root.InvalidInstanceProperty = '{0}::{1} Invalid instance property';
  4. Root.InvalidInstanceMessage = '{0}::{1} Invalid instance message';
  5.  
  6. Root.NotImplemented = '{0}::{1} Not implemented';
  7. Root.SubclassResponsibility = '{0}::{1} Subclass responsibility';

Defines the error messages displayed by the class methods and the instance methods doesNotContain, doesNotRecognize, notImplemented and subclassResponsibility.

SEE ALSO

Interface, Object Layer

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].