Recently, I found myself bored with all of my current projects I was working on. I just started hacking away on a new project, and my first attempt at making a DSL. I present to you RTask, a simple library to write tasks for your projects.
RTask wraps tasks in a DSL wrapped in a class, combining both rake and thor’s ideas together.
Install RTask via
Let’s create your first Taskfile. RTask looks for a file named Taskfile, taskfile, Taskfile.rb, or taskfile.rb. Create a file named Taskfile, and insert this into it.
class Hi < RTask::Task
task :start do
puts "hello!"
end
end
Now, in your terminal, run
RTask runs task via rtask class:task_name
Setting a default task to run is also easy. Just put default_tasks TASKS inside your class.
class Hi < RTask::Task
default_task :start
task :start do
puts "hello!"
end
end
This will call the start task without having to explicitly call it.
Namespacing for a task follows ruby’s namespacing model. Wrap your RTask classes within a module to namespace the tasks.
module Tasks
class Tests < RTask::Task
task :start do
ruby "tests.rb"
end
end
end
Now run rtask tasks:tests:start to run the task.
Task grouping is not a chore, just put group TASKS in a task
class Code < RTask::Task
task :build do
sh "gcc yadayada"
end
task :test do
# run tests
end
task :cleanup do
# cleanup
end
task :all do
group :build, :test, :cleanup
end
end
If you feel spontaneous like I did, fork on github and contribute.
I think Rake and Thor are great, this is by no means an attempt at replacement, just a simple project I felt like doing.
Rails 3 has been all about decoupling. The core developers have been hard at work, removing the need to only use a certain library and allowing for a more, framework agnostic view. For example, it’s quite easy to use something other than default, ActiveRecord, for ORM. Sequel and Datamapper are two other options that now need limited effort to switch to rather than ActiveRecord. CouchDB ORM’s can even be used quite easily. Using jQuery instead of Prototype for Ajax is also easy. Rails 3 now includes rails.js in public/javascripts that uses the HTML5 data attributes instead of inline javascript. There are drivers out for Prototype(default rails.js), jQuery, Mootools, and it isn’t that hard to write your own for your own javascript framework. One thing I couldn’t find, however, is to add callbacks for ajax requests.
Rails’ Prototype ajax driver fires several ajax:status events on the element performing the request. They are: ajax:loading, ajax:loaded, ajax:interactive, ajax:complete, ajax:success, ajax:failure. In order to use them with the default Prototype driver, you must observe the element’s event that you want to use, such as success or failure.
$("submit_message").observe("ajax:success", function(event){
$("message").clear();
});
$("submit_message").observe("ajax:failure", function(event){
alert("Oh no! An error has occurred.."):
});
Of course, it’s a good idea to use layouts only for actions that aren’t ajax requests
class PostController < ApplicationController
layout proc{|c| !c.request.xhr?}
def ajax_method
# .. Do Something..
end
#...other methods
end
One of my least favorite things to do in Rails is nitty-gritty dirty work that just isn’t..rails-like. The worst case of this( at least for me ) is how to do dynamic title tags. It bugged me so much that I even created my first rails plugin to accomplish this. This is actually my second version; my first was much too complicated. Without further ado… Fire up your terminal, change to your rails application’s root directory, and enter
rails plugin install git://github.com/justinbaker/title_tag.git
Then in a view, put this in the head section
<%= title_tag("The Cool Kids | :controller » :action") %>
This will be replaced with
<title> The Cool Kids | About » Show</title>
That’s all you have to do, and the plugin will handle the rest.