Skip to content

Instantly share code, notes, and snippets.

Avatar

Matt Browne mbrowne

  • New York, NY
View GitHub Profile
@mbrowne
mbrowne / Account.js
Last active Mar 7, 2019
DCI in native Javascript using symbols
View Account.js
const addEntry = Symbol('addEntry'),
getBalance = Symbol('getBalance');
export default class Account {
constructor(ledgers) {
const roles = {
ledgers: {
[addEntry](message, amount) {
ledgers.push(new LedgerEntry(message, amount));
},
@mbrowne
mbrowne / dci.php
Created Oct 22, 2017
Sketch of role-player-wraps-role implementation in PHP.
View dci.php
/*
This is just a demo to show the concept. For a full implementation,
see https://github.com/mbrowne/dci-php
*/
trait RolePlayer
{
private $roles = [];
private $roleMethods = [];
@mbrowne
mbrowne / CustomError.js
Last active Dec 28, 2016
Subclass error in JS with native-like console output
View CustomError.js
function CustomError(message) {
if (!(this instanceof CustomError)) {
throw new TypeError("Constructor 'CustomError' cannot be invoked without 'new'");
}
var err;
if (Object.setPrototypeOf) {
err = new Error(message);
Object.setPrototypeOf(err, CustomError.prototype);
}
@mbrowne
mbrowne / TransferMoney.swift
Last active Apr 24, 2018
DCI in Swift - simple money transfer example
View TransferMoney.swift
//TransferMoney context
class TransferMoney: Context {
private let SourceAccount: SourceAccountRole
private let DestinationAccount: DestinationAccountRole
init(source:AccountData, destination:AccountData) {
SourceAccount = source as! SourceAccountRole
DestinationAccount = destination as! DestinationAccountRole
@mbrowne
mbrowne / TransferMoney.ts
Last active Aug 29, 2015
Alternative TypeScript-DCI money transfer example
View TransferMoney.ts
/**
* Transfer Money use case
*/
function TransferMoney(sourceAcct: Account, destinationAcct: Account, amount: number) {
//bind the objects to their roles
SourceAccount <- sourceAcct;
DestinationAccount <- destinationAcct;
Amount <- amount;
@mbrowne
mbrowne / prototypal.js
Last active Aug 29, 2015
Pure prototypal inheritance in Javascript
View prototypal.js
var Animal = {
init: function(name) {
this.name = name || null;
}
}
var Cat = Object.create(Animal);
Cat.meow = function() {
console.log('meow');
}
@mbrowne
mbrowne / db-sync.sh
Created Mar 24, 2014
Shell script to copy remote MongoDB database (and overwrite local copy)
View db-sync.sh
#!/bin/bash
#SYNC MONGODB DATABASE FROM REMOTE SERVER
#NOTE: This overwrites the local copy of the database
remoteHost='yourhost.com'
remoteDbUser='root'
remoteDbPasswd='password123'
remoteDb='test'
localDb='test'
@mbrowne
mbrowne / php5.3.php
Last active Dec 31, 2015
DCI for PHP - example usage
View php5.3.php
<?php
namespace UseCases
{
class TransferMoney extends \DCI\Context
{
//These would ideally be private but they need to be public so that the roles can access them,
//since PHP doesn't support inner classes
public $sourceAccount;
public $destinationAccount;
@mbrowne
mbrowne / Role.php
Created Dec 20, 2013
DCI reverse wrapper technique for PHP (for full implementation, see https://github.com/mbrowne/dci-php)
View Role.php
<?php
namespace DCI;
/**
* DCI Role base class
*/
abstract class Role
{
/**
* The data object playing this role (the RolePlayer).
@mbrowne
mbrowne / simpleoo.js
Created Oct 26, 2013
simpleoo.js new version preview
View simpleoo.js
(function(define) {
/*
Example usage:
function Animal() {}
Animal.prototype.eat = function() { console.log('yum'); }
function Cat() {}
simpleoo(Cat).extend(Animal, {
meow: function() { console.log('meow'); }
You can’t perform that action at this time.