Skip to content

Instantly share code, notes, and snippets.

@JasonStoltz
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save JasonStoltz/d75d5a8069628db84317 to your computer and use it in GitHub Desktop.

Select an option

Save JasonStoltz/d75d5a8069628db84317 to your computer and use it in GitHub Desktop.

Scala

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()
}

Javascript

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()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment