Posts Tagged ‘ruby’

My thoughts on spec tests

Wednesday, April 9th, 2008

I’ve been asked by people why I’m not using spec tests to test Mack. I’m currently using just plain old regular unit tests for my tests. It’s a good question, but not really a valid one, I think. Should it matter what type of tests I’m using as long as I’m testing? Tests are tests. The framework you use to do your tests is moot as long as the tests you write are good, solid tests.

I jumped on the spec test bandwagon about a year or so ago. I forced my whole team to start writing in nothing but spec tests. The team, is still not impressed. If I were to tell them we weren’t using spec tests anymore, they would be EXTREMELY happy with that.Spec tests are certainly prettier than ‘regular’ tests. I’ll give you that. They are more ‘human readable’. With that said I find assert_equal to be fairly easy to read.

So why do I use regular tests, and not spec tests? A couple of reasons. First, they’re there and built right in to Ruby, ready to go, no new gems or syntax to learn. They’re fast. They are faster than spec tests. I see it everyday at work. I can also, and this one is HUGE, run just a single test or a regex’d series of tests. I really love that last bit. I have yet to find a way to do that, easily, with spec tests.

Again, how people write tests doesn’t really matter so much to me, it’s just that they write them. Testing is too easy in Ruby, there’s just no reason for not writing them.

Release 0.4.0

Wednesday, March 26th, 2008

It’s finally here! The release you’ve all been waiting for, 0.4.0!! What’s new in this one? Well, the big one, DISTRIBUTED ROUTING!!! Now you can share your routes between all of your Mack applications.

I’m very excited about this release. This is one of the first features that starts to set Mack apart from other Ruby frameworks such as Rails and Merb.  Mack is trying to set itself as the framework for doing multiple, distributed, portal applications. This release gets us headed in that direction.

There will be a tutorial post and app in the next few days. In the meantime I highly encourage you to download this release and check it out.

Changelog:

  • Added Distributed Routes!
  • gem: mack_ruby_core_extensions 0.1.3
  • removed gem: ruby_extensions
$ sudo gem install mack

Announcing Mack Ruby Core Extensions

Wednesday, March 26th, 2008

Mack has been using a combination of the ruby_extensions gem as well some local extensions to the Ruby core in order to make Mack as wonderful as it is. In an effort to make life a little simpler, as well as to help share the wealth, the ruby_extensions gem and the Mack extensions have been combined into a single new gem called mack_ruby_core_extensions.

One of the main Mack pieces that has been broken out into this new gem is the inflection system. Now you can have inflections as part of any Ruby application just by requiring the gem. As far as I can tell this is the first stand alone inflection system for Ruby. I know because I couldn’t find one for Mack, that’s why I had to write one.

This gem will continually be updated, outside of the core Mack code. The forthcoming release of Mack, 0.4.0, will be converted to use the new gem.

Those who wish to contribute to the gem can find it on GitHub at: http://github.com/markbates/mack_ruby_core_extensions

The API for mack_ruby_core_extensions can be found at:
http://mrce-api.mackframework.com/

 $ sudo gem install mack_ruby_core_extensions

Release 0.3.0

Wednesday, March 19th, 2008

I’ve been holding back this release so I could get distributed routing into it, but it appears that there’s still a little more work that needs to be done before it’s ready to go. I’m hoping to get it out by the beginning of next week, but don’t quote me on that.

Instead of focusing on what didn’t make it in, let’s talk about what did make it in! There’s some cool stuff in this release.

Format Driven Content

Mack now allows you to drive different content based on the format requested. For example:

/posts – will render app/views/posts/index.html.erb
/posts.html – will also render app/views/posts/index.html.erb
/posts.xml – will render app/views/posts/index.xml.erb – A special note *.xml.erb files, despite their name, do NOT get run through ERB, instead they use the XML Builder library
/posts.js – will render app/views/posts/index.js.erb
etc…

Alternatively, in your action you can now define ‘want’ blocks, to run specific code based on the format. Example:

class PostsController
  def index
    # find all the posts in the system
     @posts = Post.find(:all)
    wants(:html) do
      # this will only be run if html is requested.
      # we need a username for a 'welcome message in the view'
      @username = @user.username
    end
    wants(:xml) do
      # this will only be run if html is requested.
      # find the last published date
      @last_pub_date = Rss.find_last_by_date_by_object(:posts)
    end
  end
end

XML Builder Support

I’m not going to go into this, there is another nice post coming shortly that will explain how to use this library to add RSS to our blog demo. Here’s the post.

Built-in Encryption

In every app I’ve ever built I found the need to use encryption. Whether it’s for encrypting something into a cookie, a password in the database, or a file on disk, we all need encryption, so I’ve baked the crypt gem into Mack.

At the very simple level you can easily do this in your code:

@my_encrypted_value = _encrypt("hello world")

and you’ll be returned a nice pieced of garbled data using the Crypt/Rijndael library. Decrypting is just as easy:

_decrypt(@my_encrypted_value) # => "hello world"

See, I told you it couldn’t be easier. It gets even better you can even define your own ‘worker’ to implement other encryption schemes. It’s as simple as this:

class Mack::Utils::Crypt::HorribleWorker
  def encrypt(value)
    value.reverse
  end
  def decrypt(value)
    value.reverse
  end
end

_encrypt("hello", :horrible) # => "olleh"
_decrypt("decrypt", :horrible) # => "hello"

See how easy that was? You can also do:

@my_encrypted_value = "Hello".encrypt
@my_encrypted_value.decrypt #=> "Hello"

Either way it’s now easy to handle encryption in your funky cool Mack app.

Changelog:

  • Ticket: #8 Xml Builder Support
  • Ticket: #7 Ability to drive certain content based on ‘format’
  • Ticket: #9 Added a global encryption system to make encrypting/decrypting of strings easy to use
  • gem: builder 2.1.2
  • gem: crypt 1.1.4

0.3.0: Adding RSS/xml feeds to our Blog demo

Wednesday, March 19th, 2008

Ok, as you remember a while back we created a simple blog using mack, http://www.mackframework.com/2008/03/04/the-obligatory-blog-demo/. Well now it’s time to add the all important RSS/xml feed to it.

Mack 0.3.0 introduces xml rendering support natively, so this shouldn’t be so hard. First things first, let’s fire up the app, shall we:

$ rake server

Now let’s head over to http://localhost:3000/posts. We should see our beautiful posts index page. Now let’s try to go to http://localhost:3000/posts.xml you should see something that looks like this:

XML blog demo 1

Clearly, that’s not what we want, is it? I didn’t think so. The error is telling us that it’s looking for a file called index.xml.erb in the app/views/posts directory of our blog project. Obviously that file doesn’t exist.

Let’s take a second and talk about why Mack was looking for index.xml.erb. We haven’t changed anything in our controller. Our index method still looks something like this:

def index
  @posts = Post.find(:all)
end

No where in there does it mention xml. The only place xml is mentioned is on the the url itself, remember? We looked for /posts.xml. By adding .xml you’re telling Mack that you want to render, well… xml. So it goes looking for that. That’s also new in 0.3.0. The default is html, but if you append a format (.js, .xml, etc…), it will go looking for app/views/<controller_name>/<action_name>.<format>.erb and render it.

Ok, now that we understand why we’re looking for an xml file, let’s fire up our trusty text editor and create a new file called: app/views/posts/index.xml.erb. Let’s edit the file to look like this:

xml.instruct! :x ml, :version=>"1.0"
xml.rss(:version => "2.0") do
  xml.channel do
    xml.title("My Mack Blog")
    xml.link(posts_index_full_url)
    xml.description("Find out about all the cool stuff happening on my blog!")
    xml.language("en-us")
    xml.copyright("Copyright Me")
    xml.pubDate(CGI.rfc1123_date(Time.now))
    xml.lastBuildDate(CGI.rfc1123_date(Time.now))
    @posts.each do |post|
      xml.entry do
        xml.title(post.title)
        xml.link(posts_show_full_url(:id => post.id))
        xml.description(post.body)
        xml.pubDate(post.created_at.strftime("%a, %d %b %Y %H:%M:%S"))
      end
    end
  end
end

Mack uses the standard builder gem library. I’m not going to go into explaining how that works, there are plenty of other tutorials and documentation that will show you that. I’m also not going to explain all the necessary pieces of an RSS feed. Instead I’ll point out in that code you’ll see we’re using the @posts instance variable that we set in the index action of our PostsController. Just like regular *.html.erb files we have access to all the instance variables from the controller, as well, helpers, etc…

So now if we go to http://localhost:3000/posts.xml we should see our RSS feed. If we did a view source we should see something that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
 <channel>
  <title>My Mack Blog</title>
  <link>http://localhost:3000/posts</link>
  <description>Find out about all the cool stuff happening on my blog!</description>
  <language>en-us</language>
  <copyright>Copyright Me</copyright>
  <pubDate>Tue, 18 Mar 2008 17:18:05 GMT</pubDate>
  <lastBuildDate>Tue, 18 Mar 2008 17:18:05 GMT</lastBuildDate>
  <entry>
   <title>My New Post</title>
   <link>http://localhost:3000/posts/1</link>
   <description>This is my first post in my cool Mack blog!</description>
   <pubDate>Tue, 18 Mar 2008 11:58:30</pubDate>
  </entry>
 </channel>
</rss>

Awesome! All that’s really left is create one of those fancy RSS tags in the location field of our browsers that people can click and go straight to the RSS feed. Let’s do that now.

At the top of your app/views/posts/index.html.erb file add the following:

<%= rss_tag(posts_index_url(:format => :x ml)) %>

Now, refresh the page in your browser, and there you go, you should now see the little RSS button in the location bar of your browser. If you click that you should be taken to your feed.

That’s all there is to adding not only xml, but an RSS feed to your new blog.

The code for this demo can be found here.