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.
- import { defclass } from 'So-o';
- import * as OL from 'OL';
Imports the function defclass
from So-o and all the functions defined by the Object Layer.
- defclass('Root',
- // superclass
- null,
- // revision
- 1,
- // class properties
- null,
- // instance properties
- 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
- { 'get': (self, attr) => OL.class_get(self, attr),
- '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
- 'new': (self, ...args) => OL.object_send_message(OL.class_send_message(self, 'make'), 'init', args),
- '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
- '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
- '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
- 'class': (self) => self,
- 'name': (self) => OL.class_name(self),
- 'revision': (self) => OL.class_revision(self),
- '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)
- 'classMessages':
- (self, inherit = true) => {
- let messages = OL.class_class_messages(self);
- if (messages)
- messages = Object.keys(messages);
- if (inherit) {
- let sc = OL.class_superclass(self);
- if (sc) {
- let inherited = OL.class_send_message(sc, 'classMessages');
- if (inherited)
- messages = messages ? [... new Set([...messages, ...inherited])] : inherited;
- }
- }
- return messages && messages.length > 0 ? messages : null;
- },
- 'instanceMessages':
- (self, inherit = true) => {
- let messages = OL.class_instance_messages(self);
- if (messages)
- messages = Object.keys(messages);
- if (inherit) {
- let sc = OL.class_superclass(self);
- if (sc) {
- let inherited = OL.class_send_message(sc, 'instanceMessages');
- if (inherited)
- messages = messages ? [... new Set([...messages, ...inherited])] : inherited;
- }
- }
- return messages && messages.length > 0 ? messages : null;
- },
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)
- 'classProperties':
- (self, inherit = true) => {
- let properties = OL.class_class_properties(self);
- if (properties)
- properties = [... properties];
- if (inherit) {
- let sc = OL.class_superclass(self);
- if (sc) {
- let inherited_properties = OL.class_send_message(sc, 'classProperties');
- if (inherited_properties)
- properties = properties ? [... new Set([...properties, ...inherited_properties])] : inherited_properties;
- }
- }
- return properties && properties.length > 0 ? properties : null;
- },
- 'instanceProperties':
- (self, inherit = true) => {
- let properties = OL.class_instance_properties(self);
- if (properties)
- properties = [... properties];
- if (inherit) {
- let sc = OL.class_superclass(self);
- if (sc) {
- let inherited_properties = OL.class_send_message(sc, 'instanceProperties');
- if (inherited_properties)
- properties = properties ? [... new Set([...properties, ...inherited_properties])] : inherited_properties;
- }
- }
- return properties && properties.length > 0 ? properties : null;
- },
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
- 'classMethodFor': (self, msg) => OL.class_find_class_method(self, msg),
- '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
- 'addClassMessage': (self, msg, f) => OL.class_add_class_message(self, msg, f),
- 'removeClassMessage': (self, msg) => OL.class_remove_class_message(self, msg),
- 'addInstanceMessage': (self, msg, f) => OL.class_add_instance_message(self, msg, f),
- '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
- 'addClassProperty': (self, prop) => OL.class_add_class_property(self, prop),
- 'removeClassProperty': (self, prop) => OL.class_remove_class_property(self, prop),
- 'addInstanceProperty': (self, prop) => OL.class_add_instance_property(self, prop),
- '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
- '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
- 'read': (self, sdata) => {
- let properties = OL.class_send_message(self, 'classProperties');
- if (!properties)
- return self;
- let data = JSON.parse(sdata);
- if (typeof data !== 'object')
- throw new TypeError();
- let attributes = {};
- for (let p of properties) {
- if (data.hasOwnProperty(p))
- attributes[p] = data[p];
- }
- return OL.class_set_attributes(self, attributes);
- },
- '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
- 'error': (self, err, ...args) => {
- let errmsg = err;
- for (let i = 0; i < args.length; i++)
- errmsg = errmsg.replace(new RegExp(`\\{${i}\\}`, 'gi'), args[i]);
- console.error(errmsg);
- return self;
- },
- 'doesNotContain': (self, prop) => OL.class_send_message(self, 'error', [Root.InvalidClassProperty, OL.class_name(self), prop]),
- 'doesNotRecognize': (self, msg) => OL.class_send_message(self, 'error', [Root.InvalidClassMessage, OL.class_name(self), msg]),
- 'notImplemented': (self, msg) => OL.class_send_message(self, 'error', [Root.NotImplemented, OL.class_name(self), msg]),
- 'subclassResponsibility': (self, msg) => OL.class_send_message(self, 'error', [Root.SubclassResponsibility, OL.class_name(self), msg]),
- },
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
- { 'get': (self, attr) => OL.object_get(self, attr),
- '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
- '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
- '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
- 'class': (self) => OL.object_class(self),
- 'superclass': (self) => OL.class_superclass(OL.object_class(self)),
- 'messages': (self, inherit = true) => OL.class_send_message(OL.object_class(self), 'instanceMessages', [inherit]),
- '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
- 'respondsTo': (self, msg) => OL.class_find_instance_method_class(OL.object_class(self), msg) ? true : false,
- 'methodFor': (self, msg) => OL.class_find_instance_method(OL.object_class(self), msg),
- 'isKindOf': (self, c) => OL.class_is_kind_of(OL.object_class(self), c),
- '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
- '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
- '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
- 'delegate': (self, msg = false, ...args) => {
- let delegate = OL.object_get(self, 'delegate');
- if (!msg)
- return delegate;
- if (!delegate)
- return false;
- if (!OL.object_send_message(delegate, 'respondsTo', [msg]))
- return false;
- return OL.object_send_message(delegate, msg, args);
- },
- 'setDelegate': (self, delegate) => {
- if (! (delegate === null || (typeof delegate === 'object' && delegate instanceof OL.Instance)))
- throw new TypeError();
- return OL.object_set(self, 'delegate', delegate);
- },
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
- '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
- 'read': (self, sdata) => {
- let properties = OL.class_send_message(OL.object_class(self), 'instanceProperties');
- if (!properties)
- return self;
- let data = JSON.parse(sdata);
- if (typeof data !== 'object')
- throw new TypeError();
- let attributes = {};
- for (let p of properties) {
- if (data.hasOwnProperty(p))
- attributes[p] = data[p];
- }
- return OL.object_set_attributes(self, attributes);
- },
- '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
- 'error': (self, err, ...args) => {
- let errmsg = err;
- for (let i = 0; i < args.length; i++)
- errmsg = errmsg.replace(new RegExp(`\\{${i}\\}`, 'gi'), args[i]);
- console.error(errmsg);
- return self;
- },
- 'doesNotContain': (self, prop) => OL.object_send_message(self, 'error', [Root.InvalidInstanceProperty, OL.class_name(OL.object_class(self)), prop]),
- 'doesNotRecognize': (self, msg) => OL.object_send_message(self, 'error', [Root.InvalidInstanceMessage, OL.class_name(OL.object_class(self)), msg]),
- 'notImplemented': (self, msg) => OL.object_send_message(self, 'error', [Root.NotImplemented, OL.class_name(OL.object_class(self)), msg]),
- 'subclassResponsibility': (self, msg) => OL.object_send_message(self, 'error', [Root.SubclassResponsibility, OL.class_name(OL.object_class(self)), msg]),
- }
- );
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.
- Root.InvalidClassProperty = '{0}::{1} Invalid class property';
- Root.InvalidClassMessage = '{0}::{1} Invalid class message';
- Root.InvalidInstanceProperty = '{0}::{1} Invalid instance property';
- Root.InvalidInstanceMessage = '{0}::{1} Invalid instance message';
- Root.NotImplemented = '{0}::{1} Not implemented';
- Root.SubclassResponsibility = '{0}::{1} Subclass responsibility';
Defines the error messages displayed by the class methods and the instance methods doesNotContain, doesNotRecognize, notImplemented and subclassResponsibility.
Comments