Old Emmanuel Oga's Weblog (new one is at www.emmanueloga.com)

Moving home to a jekyll powered home site: www.emmanueloga.com

Posted in ruby by emmanueloga on octubre 25, 2010

My new home is at www.emmanueloga.com

I got bored of having to use the quirky little wordpress textarea to type stuff. Is much more fun to type my blog posts in my favourite editor instead of having to mess around with the markup on wordpress (or blogger, or any other web based editor that does not behave like vim :-/). That’s why I’m using github pages (powered by jekyll) now.

My new home is at www.emmanueloga.com. Yeah I might change the domain name at some other time. For now, please endure the funny prefix. The proper domain name works too, but it redirects to the www. version. Trust me, there is a very good reason for the «www.» prefix (and that is a boring story).

YAAAY we finished our #railsrumble entry!!!

Posted in ruby by emmanueloga on octubre 17, 2010

Background processing yaaaay! :-)

Posted in ruby by emmanueloga on octubre 17, 2010

First 6 hours of rumble

Posted in ruby by emmanueloga on octubre 16, 2010

So basically spent the first «stage» (6hours) of the rumble installing the system, initiating the rails application, creating deploy scripts,  initial database setup/deploy, and creating a project on PT / checking some PT tasks. By this time I could have had 6 more hours of sleep if I had put the PT tasks in advance,  used puppet or chef, and used a template for the rails app. Being that I’m the only one working on the app right now I cannot show you much progress, but, the screenshot is attached. NOTE TO MY SELF FOR NEXT RUMBLE: do some homework before it starts! 🙂

I’m just gonna hit the sack and continue to work tomorrow with my buddy.

first commit

Posted in ruby by emmanueloga on octubre 15, 2010

EPIC

First Commit

Rumble 2010

Posted in ruby by emmanueloga on octubre 15, 2010

Ok, first rumble I participate in. My team

http://railsrumble.com/teams/carbon-based-lifeforms

Let’s see what happens. Installing the system as first step.

Pretty printing xhtml with nokogiri and xslt

Posted in ruby, xhtml, xml by emmanueloga on septiembre 29, 2009

[UPDATE]

Check this gist for a command line version of  xml indenter in this post.

Today I was looking for a way to pretty print xhtml. Good’ol REXML supports this in a very simple way:

Document.new("<some>XML</some>")doc.write($stdout, indent_spaces = 4)

This generates a nicely indented xml document. But REXML was not robust enough for my needs. Luckily, we now have a couple of excellent choices on ruby for parsing xml, including hpricot, nokogiri and libxml-ruby bindings.

I did not find a way to pretty print xhtml as easy as you can do with REXML with any of these libraries, though. But I did find a way of doing it using XSLT. Nokogiri supports applying XSLT to an XML document (probably libxml bindings do too, hpricot does not). Here is how:

    xsl = Nokogiri::XSLT(File.read("pretty_print.xsl"))
   html = Nokogiri(File.read("source.html"))
   File.open("output.html", "w") { |f| f << xsl.apply_to(html).to_s }

That’s it, simple enough. Got the idea from this dzone snippet.

For the xslt file I used this nice one I found on this site: http://www.printk.net/~bds/indent.html

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="ISO-8859-1"/>
  <xsl:param name="indent-increment" select="'   '"/>

  <xsl:template name="newline">
    <xsl:text disable-output-escaping="yes">
</xsl:text>
  </xsl:template>

  <xsl:template match="comment() | processing-instruction()">
    <xsl:param name="indent" select="''"/>
    <xsl:call-template name="newline"/>
    <xsl:value-of select="$indent"/>
    <xsl:copy />
  </xsl:template>

  <xsl:template match="text()">
    <xsl:param name="indent" select="''"/>
    <xsl:call-template name="newline"/>
    <xsl:value-of select="$indent"/>
    <xsl:value-of select="normalize-space(.)"/>
  </xsl:template>

  <xsl:template match="text()[normalize-space(.)='']"/>

  <xsl:template match="*">
    <xsl:param name="indent" select="''"/>
    <xsl:call-template name="newline"/>
    <xsl:value-of select="$indent"/>
      <xsl:choose>
       <xsl:when test="count(child::*) > 0">
        <xsl:copy>
         <xsl:copy-of select="@*"/>
         <xsl:apply-templates select="*|text()">
           <xsl:with-param name="indent" select="concat ($indent, $indent-increment)"/>
         </xsl:apply-templates>
         <xsl:call-template name="newline"/>
         <xsl:value-of select="$indent"/>
        </xsl:copy>
       </xsl:when>
       <xsl:otherwise>
        <xsl:copy-of select="."/>
       </xsl:otherwise>
     </xsl:choose>
  </xsl:template>
</xsl:stylesheet>
Tagged with: , , ,

Linux: moving around files with white spaces on the path name

Posted in ruby by emmanueloga on agosto 11, 2009

I have a couple of big hard drives on my desktop PC. Still, from time to time I need to do some maintenance to get rid of all the junk I throw at them. Lately, I’ve been moving files around a lot. And I was doing it all wrong! I set the laziness aside and thought about ways of improving my «work flow».

Whitespace, go away!

The first issue I had is that bash gets confused when the filenames include white space.


[emmanuel@going_merry ~/files]
$ ll
total 4
-rw-r--r-- 1 emmanuel users 2818 2009-08-11 11:38 This is my afn.py

[emmanuel@going_merry ~/files]
$ mv This is my afn.py afn.py
mv: target `afn.py’ is not a directory

[emmanuel@going_merry ~/files]
$ mv This\ is\ my\ afn.py afn.py

Notice the slashes I had to add on the second mv call. This might seem pretty obvious. Bash usually puts those slashes automatically when using tab completion. But the thing is, what I really need in my case is to move a whole set of files returned by other utility, like find, to another place:


[emmanuel@going_merry ~/files]
$ ll
total 8
drwxr-xr-x 2 emmanuel users 4096 2009-08-11 11:41 dir1
drwxr-xr-x 2 emmanuel users 4096 2009-08-11 11:41 dir2

[emmanuel@going_merry ~/files]
$ find dir1/
dir1/
dir1/My afn 1.py
dir1/My afn 2.py

[emmanuel@going_merry ~/files]
$ find dir1/ | xargs -I {} mv {} dir2
mv: cannot stat `dir1/My afn 1.py’: No such file or directory
mv: cannot stat `dir1/My afn 2.py’: No such file or directory

The problem here is in the find output. The list of  path names returned by find may have unescaped spaces on path names. I needed to escape those spaces before using those path names with other utilities. I looked around for a unix utility to help me in this regard, but not easily finding anything good around, I just wrote a little ruby script for dealing with this: «escapepath».


[emmanuel@going_merry ~/files]
$ which escapepath
/usr/bin/escapepath


[emmanuel@going_merry ~/files]
$ cat `which escapepath`

#!/usr/bin/ruby
require 'shellwords'

while not STDIN.eof?
  puts(STDIN.gets.chomp.shellescape)
end

gist

Now I can do things like:


[emmanuel@going_merry ~/files]
$ find dir1/ -type f | escapepath | xargs -I {} mv {} dir2/


If you are typing too much, you are not aliasing enough

An anti-pattern I was using was… typing a lot. I kept running again and again things like:

[emmanuel@going_merry ~/files]
$ ack -a -f | grep thing_i_want_to_find -i | escapepath | xargs -I {} mv {} /some/directory

…to find several files at once using a case insensitive regular expression and move all of them to a certain directory. The solution: bash aliases:


[emmanuel@going_merry ~/files]
$ cat ~/.bashrc

# ...
alias xmv='escapepath | xargs -I {} mv {}'
alias ifind='ack -a -f | grep -i'
# ...

Note: ack is a cool alternative to find+grep which also has some nice file finding features. Check it out.

Know I can do things like:


[emmanuel@going_merry ~/files]
$ ifind thing_i_want_to_rm | xargs rm
[emmanuel@going_merry ~/files]
$ ifind thing_i_want_to_move | xmv /some/directory

Ah! much more easy on my poor fingers…. 🙂

How to spec a rails plugin?

Posted in rails, rspec, Uncategorized by emmanueloga on junio 25, 2009

Basically, the same way you spec a rails application.
The cool trick is you take advantage of the spec_helper.rb of by loading it in the spec_helper of your plugin.

# ../vendor/plugin/YOUR_PLUGIN/spec/spec_helper.rb
begin
  # load your main app spec_helper
  require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
rescue LoadError
  puts "You need to install rspec in your base app"
  exit
end

I took this idea from Pat Maddox’s rspec-plugin-generator. You can use that generator to generate the basic plugin boiler plate for you.

The caveats are, you need to have rspec installed on your main app and you need to actually have the plugin installed in a rails application to test it. In any case, this works for my current needs.

For more info see this post.

Conferencia Locos X Rails tomorrow,

Posted in Uncategorized by emmanueloga on abril 2, 2009

Tomorrow starts Locos X Rails conference in Buenos Aires, Argentina. I’m very exited about it! Yesterday I met Desi McAdam, of DevChix fame, and also the keynote speaker Obie Fernandez, founder of HashRocket. I’m eager to hear their talks, and hope to see you in the conf. too!

Desi and Obie at the press meeting for the LocosXRails conf.

Desi and Obie at the press meeting for the LocosXRails conf.

Tagged with: , ,