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

My friend Adam Willson approached me with this interesting project. The main idea of the project was to create art that could be given to people at the street randomly. The motive was to demonstrate that there people doesn’t need to be wealthy in order to have access to art and enjoy it. Together with Carrie-Anne Wysoki, we painted for the project last week.

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.

In security, we use a system of key in order to work on encryption and decryption. The most common system used are the Symmetric Key Encryption and the Public Key Encryption

Share

As you may know, Microsoft is getting into the open source community with the ASP.NET and DNX with the mono project.
The .NET Execution Environment (DNX) is a software development kit which allows you to run .NET applications on Windows, Mac, and Linux using different frameworks such as .NET framework, .NET Core, and Mono. However, everything are not roses. There are many incomplete libraries, incompatibilities, lack of documentation, and most of the examples such as doing SQL or SOAP do not work depending on which the library targets you are planning to code for. Therefore, I decided to test the basics on all the library targets. I am starting with DNX451.

Delegate seems to be working fine in DNX451 with C#. Below is a code example for those who wish to play with it.

Share

I am  working on the base case of the 3D printer. I am planning to make most of the parts such as the power supply reusable.

Modifying the case

Share

In standard ANSI C, if we wish to have two functions with the same name but different number of parameters or type of parameters (called overloading), we can’t. It is not allowed.

In the other hand, in C++, we can overload functions. Overloading a function means that we can have two or more functions sharing the same function name but with different type or different number of parameters:

Share