andhapp Random ramblings

Adding multiple classes using classList API

This week, I ran into classList API, properly. I’ve read about it in the past, but never had the chance to use it. Most of the articles on google, just give you an introduction into classList API, but fail to reveal the full picture. For instance, I was trying to add multiple classes to an element in the DOM and kept getting errors. None of the articles could solve the mystery behind it.

I decided to do my own research and ended up on the current DOM spec.

classList API is a convenient way to access and manipulate the classes for an element in the DOM. In the background, it implements DOMTokenlist interface. The interface clears all the confusion and below is the code to add multiple classes:

document.getElementById("some-element-id").classList.add("class1", "class2");

Can you spot any issues with the way the API works?

Well, it doesn’t work like jQuery’s (or zepto) API for adding classes. In popular javascript frameworks, you will do something like this:

$('#some-element-id').addClass("class1 class2");

Does it have to work like jQuery? It doesn’t have to work like any of the javascript frameworks out there, but I was expecting it would try and stay close to the existing trend. But, it doesn’t. Anyways, I continue to live and learn.

Unix pipes don't work with cd

I use pbcopy (on mac) to copy the current working directory path:

pwd | pbcopy

But, the following redirection doesn’t work:

pbpaste | cd

This command will just take me back to the my home directory. Why does this redirection not work?

Replacing cd with less works as normal, that is, the output of pbpaste is given to the next command as input:

pbpaste | less

The reason cd doesn’t work with redirection is because cd is not an external command. It’s a shell builtin function and it runs in the context of the current shell, and not as an external commands, in a different process. The right way to use cd with pbpaste is to do this:

cd $(echo `pbpaste`)

Hope it helps.

Update (2014-10-20) After recently reading Unix Shell in Ruby, I think the builtin commands can be explained much better with the following excerpt I took from the book:

Every shell has some built in commands. These are commands which are inherently different from typical utilies in that they change the state of the shell and run in the shell process itself. For instance, a utility like cat simply prints text to the terminal. It does not change the state of the shell itself. The same goes for common commands like grep, tail, and curl. But there are certain commands that need to change the state of the shell, and cd is one of them.

Pass JVM arguments to Trinidad server when trinidad is a gem installed via Bundler

I’ve experienced some OutOfMemoryError on my JRuby app and all the googling has led me to one solution, that is, pass the -Xmx, -Xss, and -Xms options to JVM. Now, it’s easy to do it with Java, or JRuby as you just pass it in, but I’m using trinidad and just run:

bundle exec rackup -s trinidad

How do I pass any JVM options in this command?

A little bit of searching in trinidad’s wiki, found a performance tuning page, which tells a way to do it but you have to run the app via jruby command, and not using bundle exec. I can’t do that because my gem and other dependencies for it are managed by bundler. Well, here’s a way to do that:

bundle exec jruby --server -J-Xmx2048m -J-Xms2048m -J-Xmn512m -J-XX:MaxPermSize=512m -S trinidad -e production

Please note running it via bundle exec will slow down the startup, but it’s okay. It’s a one-time cost that I’m willing to pay for a huge win.

Hope it helps others.

Register a new library with Bower

Well, this is really a note for self because I tend to remember things better if I write them down and what better place than my blog. So, whenever you register a library with bower, please don’t forget to create a tag in the remote repository for that version. Bower uses the version to pull the library down from the tagged version in the remote repository on installation.

I’ve been spending time trying to write a PhoneGap application, and bower is kind of bundler for the Javascript world.

Update id of an object in Rails backed by MongoDB

I’m not a big fan of developers getting swayed by NoSQL hype and using mongoDB with Rails. Unless there’s a good enough reason, stick to relational databases.

Anyways, I found myself trying to update id of an object in Rails. Tried it in rails console first and it didn’t work as the id is a read-only field. Next stop, mongo console. Don’t ask my why I’m updating the id, because I have to, unfortunately.

Here are the steps:

1. Start mongo console by running ‘mongo’ on the command line (terminal, iterm)

2. Run following commands in order:

 show dbs;
 use <your-database_name>;
 doc = db.<collection>.findOne({_id: ObjectId('required-id')});
 doc._id = ObjectId('new-id');
 db.<collection_name>.insert(doc);

If you want you can delete the old document, but apparently you can’t update the id. You will have to clone the doc and give it a new id, and then insert into collection, which is what we’ve done. Took me a little while to get this working, essentially due to my lack of understanding of mongoDB in general.

Update:

In the commands above, I forgot to commit the insertion to the disk. Please don’t forget to run this at the end:

db.<collection_name>.commit;