Clean configuration defaults via Object.create in Node.js

While recently coming back to clean up some Node.js module
code I was looking for a nicer way to handle configuration of an object besides
doing something like this to handle both configuration options and defaults.

var MyModule = function(opts){
  this.foo = opts.foo || 1;
  this.bar = opts.bar || 2;
  // the list goes on
}

In my opinion this code is not really readable, and defaults are not as
obvious as I would like them to be. So I decided to let myself get inspired from
jQuery which provides a nice way to handle settings and defaults in plugins, via
$.extend(defaults, options) so my first
Idea was to simple create an extend function in node, but since I would have to
extend Object to make this as clean as possible I was looking for a way to
handle this natively, and this is where
Object.create
comes in handy.

Object.create allows creating of an object based on some prototype as well as
some default values, which is exactly what I was looking for. Do to the way
Object.create handles properties the options need to be prepared before merging
them in the configuration, which is finally bound to this.

var MyModule = function(opts){
  // some default values 
  var defaults = { foo: 1, bar: 2 };

  // prepare the options for Object.create
  var options = {};
  for(var i in opts){
    options[i] = { 
      value: opts[i], 
      enumerable: true, 
      writeable: true, 
      configurable: true } 
    };
  }

  // let Object.create merge the options with the defaults
  var config = Object.create(defaults, options);

  // bind to this
  for(var o in config){
    this[o] = config[o];
  }
}

Any suggestions for a different cleaner handling of configuration options are
highly welcome.

EDIT: Fixed extra level of nesting in options thanks to Sami Samhuri

Filmspieler

Some time ago, me and a good friend of mine, Sebastian Weber decided
to take up podcasting. At the time he was still working for PCGames, a german games magazine, and
so there was not really the opportunity to do so. But now the time has come
and we are now booth working together on recording out new Podcast
“Filmspieler”. To get the “Film” part going, another really good friend
of mine, Florian Negwer is contributing.

So whats this all about? It really is a project born from our love towards
movies, games, and especially talking about those. We are not focusing on news
but the stuff we are currently excited about even if it is older already.

All 3 of us are really excited to get this going, our first episode is currently
out and we hope to continue in a 2 week rhythm, while writing articles on the
webpage as well in between.

We’d love some feedback so check it out at “Filmspieler”.

BEWARE: Everything is in german.

Nginx forward all requests to https

Since listening to Mike West and reading his posts I finally decided to really move all my stuff hosted on my own server to SSL. Certs are really not that expensive anymore (about 9$ per Year) and getting more IPs for my server was easier than expected (Whats up with IPv4 depletion aye?).

Since I want everything reachable with all the old links I decided to forward pretty much everything to the ssl host, to do this via Nginx the following little config should work, so I wanted to share since it was not as easy as expected to actually get everything running nicely. It forwards in this case http://example.com http://www.example.com https://www.example.com all to https://example.com.

https://gist.github.com/1337018.js

To make sure the clients will only use SSL from now on the server serving SSL should furthermore have ” add_header Strict-Transport-Security “max-age=31556926; includeSubdomains”; ” somewhere in the config.

Thats it… now to move over more stuff.

Hermes allows google to index the parcel tracking?

While waiting for my iPhone 4s today I received a message about my tracking status and so I was checking the Hermes tracking page. When it told me the status of my packet I was curious when it will arrive so I decided to google for that, and was kind of surprised what showed up…

why_hermes_one.png

Oh well so It seems like google is indexing the Hermes tracking system, you can even click it and view the links for another persons’ packages….

why_hermes_two.png

It’s actually not a big deal, I’m just confused why Hermes would allow google to index this. It’s not really useful for anybody and even though it is publicly available information feels kind of weird to have another persons delivery as the first google result.

Use a different single template per category in WordPress

In a recent WordPress project the posts are displayed on different pages differentiated by category instead of type. In this case different designs are loaded based on categories as well, since the theme needs to match the design of the current page. WordPress does not provide a build in mechanism to do this, it only does for post types.

The solution is rather simple, use the single.php file to first check the category and load the matching template. Since in my development environment the categories might match to different IDs than in the deployed instance it’s more practical to match by name instead of ID. Importantly first the category ID needs identification by name, and is stored to load the matching template, since matching via the $post variable can only be achieved via ID not name or slug. For anybody interested I put example code up on github.

https://gist.github.com/1272148.js

Using shell script to deploy automatically

Automatic deployment is essential for any project which is supposed to be worked on and run continuously. Until recently I was using capistrano to do this, but since development seems to have kind of stalled recently I decided to look around for an alternative, which I found in deploy a shell script by TJ Holowaychuk. To check it out I am currently using it to deploy a small Codeignitor project I am working on the side and use to try out new stuff. It really is easy to use and quite flexible due to allowing pre and post deploy hooks as well as automatic tests to check if the deployed version is working, and rollback if it is not. The use with CI is quite simple the only thing todo is to make sure that the right permissions are set which can be easily handled by a post-deploy shell script.

Usage

On the server it creates a simple directory structure with

ENV_ROOT/current
ENV_ROOT/shared
ENV_ROOT/source

in my case for both staging and production. This is done via an initial

deploy [ENV] setup

all following deploys are done by issueing

deploy [ENV]

deploy shell script

Modifications

For some reason some commands were not working for me in Bash ond my Mac so I had to do some small ajustments, I guess it is due to the version of bash I am using being not current maybe. You can find them in my fork

Integrate doctrine 2 in zend 1.11

Just some quick notes on zend and doctrine. Right now I am in the process to rewrite a web application which is based on zend and doctrine, and in the process is ported over to the latest versions of both. Since doctrine does not integrate completely seamless in zend, yet, there is some glue code to be included to make it work. While looking for the right way to do it I came across a video which explains everything nicely. The code which holds everything together is Bisna which is available as part of NOLASnowball. To make it work simply check out the
“doctrine2-managed-crud” branch


$ git clone https://github.com/ralphschindler/NOLASnowball
$ git co doctrine2-managed-crud

and copy Bisna, Symphony, Doctrine from the library folder to your project, also the application.ini file needs to be adjusted to. For a more extensive explanation check out the video mentioned above.

Scaling images in Titanium (JPGCompressor Update)

Since I wrote the initial version of jpgcompressor for titanium, I needed scaling of images as well. The only way to do this in titanium is to overlay an Ti.UI.imageView and grab the picture out. This is neither fast nor clean, and it seems to reduce the quality for some weird reason. Since I figured I’m not the only one facing this problem, I added the feature to jpgcompressor via the scale function. Documentation is available in the example. If there are any problems please let me know, the code is build against the new 1.7.2 version of Titanium but should work with previous versions as well by adjusting the manifest as well as titanium.xconfig.