07 April 2012

Built a simple site with Node.js

I had a simple site to build for a friend (a local business called It's A Gift) so I decided this would be a good time to get going with Node.js. Initially I wanted to see if I could do this on Windows, but with my earlier issues with it, I decided to just stick with Ubuntu. More and more I'm preferring working in Ubuntu.

Number 1 goal here was to do coffeescript end to end. Got that working fairly easily. My setup looks like this: 

- HTML Markup: Coffeekup: https://github.com/mauricemach/coffeekup
- MVC: Express:  http://expressjs.com/

I wouldn't say it was easy exactly, there were some minor bumps along the way. But I was pretty impressed with just how easy it was considering how little I knew going into it. Node just wants to get out of your way so you can get your site going. 

Another thing that I like about this setup is all of the markup and code looks fairly similar: 

Markup: 

div '.top.child', ->
  img '.logo', src: "images/logo.png", alt: "It's a Gift logo"
div '.bottom.child', ->
  h3 'Opening April 2012'
  p style: 'margin-bottom: 40px;'

CSS:


light_green = #B8CD88
dark_green = #64893D
beige = #F4BF7F
orange = #F79323
sea_green = #73A4A2
sans_font = 'Yanone Kaffeesatz', sans-serif
body
  font: 12px Helvetica, Arial, sans-serif;
  background: url('/images/paper_texture.png')
.container
  margin-top 60px


So everything is nice and similar. Love that. 

15 March 2012

Oy vey, coffeescript and node.js on Windows

I'm building a new site today. Just a small one, so this seemed like a good place to get started with node.js. Well, I found this really excellent blog post about it: 


http://www.davidarno.org/2012/02/02/beginners-guide-to-using-coffeescript-on-windows-with-node.js/ 


Nifty. So I get started and install coffee via npm. Now, just because I despise CMD (sooo 1990) I do this in PowerShell. OK, so I do the npm: 
PS>npm install -g coffee-script
npm http GET https://registry.npmjs.org/coffee-script
npm http 304 https://registry.npmjs.org/coffee-script
C:\Users\Justin\AppData\Roaming\npm\cake -> C:\Users\Justin\AppData\Roaming\npm\node_modules\coffee-script\bin\cake
C:\Users\Justin\AppData\Roaming\npm\coffee -> C:\Users\Justin\AppData\Roaming\npm\node_modules\coffee-script\bin\coffee
coffee-script@1.2.0 C:\Users\Justin\AppData\Roaming\npm\node_modules\coffee-script


Then I type 'coffee':

PS>coffee 
The term 'coffee' is not recognized as the name of a cmdlet, function, script file, 
OK, that's a path problem. So I use this rather tortured bit of syntax:
[Environment]::SetEnvironmentVariable( "Path", $env:Path + ";C:\Users\xxxx\AppData\Roaming\npm\node_modules\coffee-script;", [System.EnvironmentVariableTarget]::Machine )
Then restart powershell. Now when I type 'coffee' into Powershell I get this:


Say what?? Oy vey. Kinda gave up here.

Then I try it in Linux (in the VM):
~/app/log> sudo apt-get install coffeescript
[sudo] password for user: 
Reading package lists... Done
Building dependency tree       
etc... 
ldconfig deferred processing now taking place
~/app/log> coffee
coffee> ~/app/log> 
~/app/log> 
~/app/log> 
~/app/log>  coffee -bpe "alert i for i in [0..10]"
var i;
for (i = 0; i <= 10; i++) {
  alert(i);
}

Aaand, we're done. Every time this sort of thing happens, it's another nail in the coffin for me developing in Windows.



19 January 2012

Storing values outside your stub with Rspec

I'm working on some code today that saves records to a database. For my rspec tests I want to redirect the "save" to a stub. And I want to take the data that would have been saved and stow it somewhere else. Well, I had plenty of problems with this so I'm here to share the real solution.

The first issue that I ran into: return values on stubs must match the type of the return value in your live code. Duh. But I'm coming from the static typed world, so this one slipped by me. I had a method that was returning the number of new records created (0 or 1). Simple. But the stub was just pushing the input into an array:

  saved_rows = []
    proc.stub(:save) do |arg0| 
      puts 'redirecting save'
      saved_rows << arg0
    end


Which meant that the return value (that was going to 'real' code) was a big array. No bueno. Ok, fixing that was a huge aha moment. Just add a '0' on the line after  @saved_rows << arg0. Much cursing about this one -- the object worked find from irb but failed when I did the test. 


Next up: storing the row in an array outside of the stub. I couldn't find any samples of this, so it was trial and error. In the code above, it certainly looks like it'd work. But it doesn't. I assume saved_rows is getting created in two places. The solution: use an @instance variable: 


    @saved_rows = []
    proc.stub(: save ) do |arg0|
      puts " redirecting save "
      @saved_rows << arg0
      0
    end


Bada-bing. Now I have a save method that is storing the new data in a place where I can get to it (and test it). 

Related Rspec doc:

https://www.relishapp.com/rspec/rspec-mocks/docs/method-stubs/stub-with-substitute-implementation