<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'><id>tag:blogger.com,1999:blog-424828115563775895</id><updated>2008-03-26T08:35:28.853-07:00</updated><title type='text'>Ruby as Poetry</title><link rel='alternate' type='text/html' href='http://alexbartlow.net/'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/424828115563775895/posts/default'/><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://alexbartlow.net/atom.xml'/><author><name>legionnaired</name></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>3</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-424828115563775895.post-7002636152727592888</id><published>2008-03-24T13:00:00.000-07:00</published><updated>2008-03-24T13:01:12.334-07:00</updated><title type='text'>Domain Specific Documentation</title><content type='html'>I've often lamented poor documentation in libraries I've had to use. Even more troubling is a lack of proper documentation in my own code. Many times have I vowed to start every project with a flow-chart of all methods with their contracts, only to have it spiral out of control.&lt;br /&gt;&lt;br /&gt;A problem with Rails is that it's so productive, the time you spend documenting your code can literally be something like the cube of the time you spend writing to the spec. Some wouldn't call this a problem - but it can lead to you iterating out ahead of yourself to the point where, if you're not careful, you'll end up with an unmaintainable mess.&lt;br /&gt;&lt;br /&gt;The solution? RDoc.&lt;br /&gt;&lt;br /&gt;RDoc lets you comment as you go. This can be as simple and useless as "this method does stuff" or a more formal contract for each controller action, such as:&lt;br /&gt;&lt;blockquote&gt; #  requires:&lt;br /&gt;#  - sessions[:username] is_valid_username&lt;br /&gt;#  produces:&lt;br /&gt;#  - @items_in_cart = ELEMENTS(user = sessions[:username], items U sessions[:cart] )&lt;br /&gt;#  - @username = sessions[:username]&lt;br /&gt;#  renders:&lt;br /&gt;#  - view : 'store#cart_view'&lt;br /&gt;#  failure_states:&lt;br /&gt;#  - redirect_to view : 'store#index'&lt;br /&gt;def cart_view&lt;br /&gt;...&lt;br /&gt;end&lt;/blockquote&gt;&lt;br /&gt;It only takes a second to sketch up, and there's absolutely no question what the operation does and doesn't do.&lt;br /&gt;&lt;br /&gt;The requires/ensures clauses are adapted from a &lt;a href="http://www.cse.ohio-state.edu/sce/rcpp/FAQ/Resolve_FAQ/index.htm"&gt;RESOLVE-C++&lt;/a&gt; method of design-by-contract. Just because it's an in-house research language doesn't mean you can't learn from it, right? However, Rails' MVC structure as well as the conspicuous absence of pass-by-reference requires some modification. I propose 'requires (things like params), produces (instance variables created), writes (to database), renders (which view), and fails_to (what does it do when the precondition isn't met?).' Fails-to seems like a bad fit for contract-based-design that should never, in theory, fail... but this is the web, and if there's a way for some of your code to break, then it will. The best thing to do is to define it explicitly.&lt;br /&gt;&lt;br /&gt;Once you have all of these contracts written, it's trivial to get the sweet, sweet documentation from your code. Add the following to your ${RAILS_ROOT}/Rakefile:&lt;br /&gt;&lt;blockquote&gt;Rake::RDocTask.new do |rd|&lt;br /&gt;rd.rdoc_files.include("app/**/*.rb")&lt;br /&gt;end&lt;/blockquote&gt;&lt;br /&gt;Then just ${RAILS_ROOT}/rake rdoc&lt;br /&gt;&lt;br /&gt;Voila! Instant domain documentation.&lt;br /&gt;&lt;br /&gt;The rest of the week: doing this stuff automatically from the top-down.</content><link rel='alternate' type='text/html' href='http://alexbartlow.net/2008/03/domain-specific-documentation.html' title='Domain Specific Documentation'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=424828115563775895&amp;postID=7002636152727592888&amp;isPopup=true' title='2 Comments'/><link rel='replies' type='application/atom+xml' href='http://alexbartlow.net/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/424828115563775895/posts/default/7002636152727592888'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/424828115563775895/posts/default/7002636152727592888'/><author><name>legionnaired</name></author></entry><entry><id>tag:blogger.com,1999:blog-424828115563775895.post-4093144877338387603</id><published>2008-03-24T12:59:00.000-07:00</published><updated>2008-03-24T13:00:34.549-07:00</updated><title type='text'>Drag and Drop</title><content type='html'>So I was getting a 'nil object where didn't expect it' error attempting to use the drop_receivable_element helper while working through some &lt;a href="http://www.devarticles.com/c/a/Ruby-on-Rails/Drag-and-Drop-with-scriptaculous-and-Rails/s/"&gt;tutorials.&lt;/a&gt; Yes, I'm going to school for English to be able to write sentences like that. Yes, I know it's a mistake.&lt;br /&gt;&lt;br /&gt;The problem is that, like a lot of the magicks underlying Rails, many of the helper methods all call each other. At some point in the development process, after the writing of the above tutorial I assume, drop_receivable was modified to call some of the link helpers.&lt;br /&gt;&lt;br /&gt;The assumption is that if you have such an ajax drop-able area, you're going to want to fire off a request to some controller somewhere, and so why not make the link mandatory? While that's all well and good, there should probably be some sort of dignified way to 'opt-out' of this sort of thing.&lt;br /&gt;&lt;br /&gt;You can add a quick hack to get around it if you specify :url =&amp;gt; '#' in the options hash. Mine looked like this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt; &amp;lt;%= drop_receivable_element(:dropDIV , {:url =&amp;gt; '#', :hoverclass =&amp;gt; :hover}) %&amp;gt; &lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Also, I understand that we can strip out 'parameter punctuation' to make things more streamlined? I mean, I know that drop_receivable_element :dropDIV, :url =&amp;gt; '#', :hoverclass =&amp;gt; :hover might fit more into in-line text... but I always thought that the extra symbols were good to help break the code up a bit more and make it more maintainable.&lt;br /&gt;&lt;br /&gt;Anyway, I'm going to be doing some more work later on this week to get drag-and-drop working in 2d space, the first step in getting a web-based, virtual tabletop off and running.</content><link rel='alternate' type='text/html' href='http://alexbartlow.net/2008/03/drag-and-drop.html' title='Drag and Drop'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=424828115563775895&amp;postID=4093144877338387603&amp;isPopup=true' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://alexbartlow.net/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/424828115563775895/posts/default/4093144877338387603'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/424828115563775895/posts/default/4093144877338387603'/><author><name>legionnaired</name></author></entry><entry><id>tag:blogger.com,1999:blog-424828115563775895.post-7722639397354989240</id><published>2008-03-11T12:21:00.000-07:00</published><updated>2008-03-11T12:26:37.513-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='projects'/><category scheme='http://www.blogger.com/atom/ns#' term='design by contract'/><category scheme='http://www.blogger.com/atom/ns#' term='rails'/><title type='text'>Rails, and contract-based design.</title><content type='html'>I've just finished my first Rails web application (&lt;a href="http://hero.alexbartlow.net"&gt;link&lt;/a&gt;) and now it's time for a debrief - since I've actually produced and deployed something, and thus, am not nearly as scrubbly as I once was. Since I actually have a blog now, hopefully that also serves to elevate me above plebeian status.&lt;br /&gt;&lt;br /&gt;&lt;DL&gt;&lt;br /&gt;&lt;DT&gt;&lt;STRONG&gt;Rails is fast.&lt;/STRONG&gt;&lt;br /&gt;&lt;DD&gt;Your friends didn't lie to you. Rails is damn fast. I created the website in probably only two to three hours of actually sitting down and making keystrokes.&lt;br /&gt;&lt;DT&gt;&lt;STRONG&gt;Rails documentation sucks.&lt;/STRONG&gt;&lt;br /&gt;&lt;DD&gt;RDoc is a wonderful, powerful, amazing tool for the creation of great web-based documentation. And Rails doesn't use it. The reason being because the biggest method in any class in any rails app is method_missing, since that's where all the magic happens, and that's really hard to make documentation for.&lt;br /&gt;&lt;DT&gt;&lt;STRONG&gt;Rails lets you write some really bad code.&lt;/STRONG&gt;&lt;br /&gt;&lt;DD&gt;Rails is so amazingly productive, you'll get carried away by developing incrementally and iteratively. At least while you learn it, you'll have absolutely anemic models and bloated controllers with redundant, ugly, logic-filled views. This is not very Rails-like. Yet, if you're like me, you're so focussed on getting to the next milestone and getting the next iteration out the door that unless you've been drilled with good practices, you're likely to forget them.&lt;br /&gt;&lt;/DL&gt;&lt;br /&gt;&lt;br /&gt;So what's a developer to do?&lt;br /&gt;&lt;br /&gt;Enter &lt;a href="http://split-s.blogspot.com/2006/02/design-by-contract-for-ruby.html"&gt;Ruby Design By Contract.&lt;/a&gt; (Note that, according to wikipedia, 'Design By Contract' actually might be a registered trademark, so use it with caution.) RDBC is available through gem, and it actually has a pretty good readme, so just do a "gem install rdbc ; gem_server" and read all about the spec. Done? Ok, good. Basically, rdbc allows you to create a number of pre and post-assertions that execute in your code, which is great if you're creating a library, say, and you need to unit-test it. But what about if you're creating a rails application, and you have all of this magicks going on that you need to keep track of?&lt;br /&gt;&lt;br /&gt;That's where this next project will come in - I'm going to write a rails plugin that allows you to define, from a top-down perspective, everything in your application, which controllers require what inputs and what views they call, what methods are available to each of your method objects... everything.&lt;br /&gt;&lt;br /&gt;The end result?&lt;br /&gt;&lt;br /&gt;Domain-specific documentation. Fat models. Anemic controllers. Beautiful, #&lt;a href="http://haml.hamptoncatlin.com/docs/haml"&gt;HAML-expressible&lt;/a&gt; views. An order of magnitude more productivity, and a logarithmic decrease in bugs.&lt;br /&gt;&lt;br /&gt;Interested? Stay tuned.</content><link rel='alternate' type='text/html' href='http://alexbartlow.net/2008/03/rails-and-contract-based-design.html' title='Rails, and contract-based design.'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=424828115563775895&amp;postID=7722639397354989240&amp;isPopup=true' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://alexbartlow.net/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/424828115563775895/posts/default/7722639397354989240'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/424828115563775895/posts/default/7722639397354989240'/><author><name>legionnaired</name></author></entry></feed>