This is the model heirarchy i had envision in scala..
/* Anything that is an epixa resource */
trait ResourceLike {
val $promise
def $reload
...
}
trait DatabaseServerLike extends ResourceLike with WithContacts {
val replication_source
def isReplica {
!!replication_source
}
}
trait EnvironmentLike extends ResourceLike with WithContacts {
val servers
def getPrimaryServer {
// calculate primary server from servers proprety
}
}
trait WithContacts
val contacts
def $assign(contact) {
// Do some logic to add this contact to this resources list of contacts
}
}
// Concrete class
class DatabaseService extends DatabaseServiceLike
//Instantiation
var resource = DatabaseService()
//Type checking
resource match {
case a: DatabaseServiceLike => doSomething1()
case b: EnvironmentServiceLike => doSomething2()
case c: WithContacts => doSomething3()
case _ => doSomething4()
}And this is how i sort of translated it into javascript. It's a little bit different because we aren't using classes or inheritance, since we're not creating new objects. We already have an instance of a resource that was returned from epixa resource... so we're just mixing in functions post creation to try to get the same effect.
function resourceLike(source) {
return _.assign(source, {});
}
function databaseServerLike(source) {
resourceLike(source);
withContacts(source);
_.assign(source, {
isReplica: function() {
return !!this.replication_source;
}
});
}
function environmentLike(source) {
resourceLike(source);
withContacts(source);
_.assign(source, {
getPrimaryServer: function() {
// calculate primary server from this.servers
}
})
}
function withContacts(source) {
_.assign(source, {
$assign: function(contact) {
// Do some logic to add this contact to this resources list of contacts
}
})
}
databaseServiceLike(resource);
//Type checking
if (resource.isA(databaseServiceLike)) {//I left out mixing in this "isA" function, just to simlify
doSomething1();
} else if (resource.isA(environmentServiceLike)) {
doSomething2();
} else if (resource.isA(withContacts)) {
doSomething3()
} else {
doSomething4()
}