Alejandro G. Carlstein Ramos Mejia Blog



var Book = (function(){
    // Private
    var _name;
    var _price;
    var priceChanging = [];
    var priceChanged = [];

    var updatePriceChanging = function(_this, value){
        for (var i = 0; i < priceChanging.length; ++i){
            if (!priceChanging[i](this, value)){
                return true;
            }
        }
        return false;
    }

    var updatePriceChanged = function(_this){
        for (var i = 0; i < priceChanged.length; ++i){
            priceChanged[i](_this);
        }
    }

    //Public
    return {
        factory(name, price){
            _name = name;
            _price = price;
            return this;
        }
        ,name: function(){
            return _name;
        }
        , price: function(value){
            if (value !== undefined
                && value != _price){
                var isPriceTooHigh = updatePriceChanging(this, value)
                if (isPriceTooHigh){
                    return _price;
                }
                _price = value;    
                updatePriceChanged(this);
            }                
            return _price;
        }
        ,onPriceChanging: function(callback){
            priceChanging.push(callback);
        }
        , onPriceChanged: function(callback){
            priceChanged.push(callback);
        }

    }

})();

var book = Book.factory('Lord Of the Rings', 23.99);

console.log(book.name());
console.log(book.price());

book.onPriceChanging(function(book, price){
    console.log('Changing book price to $' + price);
    if (price > 100){
        console.log('-> [X] Price ($' + price + ") is too high. No changing price");
        return false;
    }
    return true;
});

book.onPriceChanged(function(book){
    console.log("The book '" + book.name() + ' changed price to $' + book.price());
});

book.price(19.99);
book.price(200.00);
book.price(99.01);

Output:

Lord Of the Rings
23.99
Changing book price to $19.99
The book 'Lord Of the Rings changed price to $19.99
Changing book price to $200
[X] Price ($200) is too high. No changing price
Changing book price to $99.01
The book 'Lord Of the Rings changed price to $99.01

 

Share

If you wish to create a read-only domain object that insure that no body is allowed to write to the database through your domain object, then add these even listener at the end of your domain object file:

transient beforeInsert = {
throw new RuntimeException('insert not allowed')
}

transient beforeUpdate = {
throw new RuntimeException('update not allowed')
}

transient beforeDelete = {
throw new RuntimeException('delete not allowed')
}

 

Share

NOTIFICATION: These examples are provided for educational purposes. The use of this code and/or information is under your own responsibility and risk. The information and/or code is given ‘as is’. I do not take responsibilities of how they are used. You are welcome to point out any mistakes in my posting and/or leave a comment.

You are required to install Hugs in your system. I believe you can download it at this address: <http://hackage.haskell.org/platform/>. The Haskell platform is available form Windows® , Mac®, and Linux. For Debian distribution and derivation distributions such as Ubuntu, you can execute the following command line:

Share

We got here a running Cast Iron appliance:

Share

Me and my friends are developing a video game for the iPhone and Android.
The following are some images I have being working on:

Share

Example of Ferry Loading algorithm.

NOTIFICATION: These examples are provided for educational purposes. Using this code is under your own responsibility and risk. The code is given ‘as is’. I do not take responsibilities of how they are used.

Share

< Previous (C++ – Part 2) | (C++ – Part 4) Next >

NOTIFICATION: These notes are published for educational purposes. Using these notes is under your own responsibility and risk. These notes are given ‘as is’. I do not take responsibilities for how you use them.

Share