New group/mailing list
Saturday, April 26th, 2008At several people’s request I’ve created a Google group for Mack. You can find it here:Â http://groups.google.com/group/mack-framework
At several people’s request I’ve created a Google group for Mack. You can find it here:Â http://groups.google.com/group/mack-framework
0.4.7 is a MUST have release. It fixes a few good bugs, like the ability to upload files now works! Plus it introduces a few nice little goodies.
File uploads are now pretty easy to do. There’s a new Mack::Request::UploadedFile that helps to wrap the Hash that Rack gives you when you upload a file. Here’s a simple example of it being used:
class UploadsController < Mack::Controller::Base  def create  @my_new_file = request.file(:my_new_file)  @my_new_file.save_to([MACK_PUBLIC, "uploaded_files", @my_new_file.file_name])  end end
That’s it! That’s all you need to upload a file! I’m going to add file uploads to the blog demo over the next few days.
You can now do ‘namespaced’ routes, such as Admin::UserController. You can very simply map them in your routes.rb as:
r.resource "admin/users"
There’s now a new rake task, generator:list, that shows all the generators available to your app.
Darsono Sutedja gets the prize for being the first person, other than myself, to contribute to Mack. Thanks a ton Darsono! It’s much appreciated.
All of the generators now use the Genosaurus generator system. Not really that exciting, but it makes for a nice, cleaner code base.
There’s a few other things in there as well. The changelog below has a few more things. Enjoy!
Changelog:
So this week I took a small break away from Mack to build another library, Genosaurus. I found that in Mack I had basically written a generator system, so I extracted it out into a stand alone library that anybody can use, and I called it Genosaurus. Genosaurus is, in my opinion, an incredibly easy to use generator framework. Let’s be honest, we’ve all basically built a generator system at one point or another, so I’ve just wrapped it up nicely. The next release of Mack, due out the end of this week, will have all it’s generators converted to use Genosaurus. Let’s take a look at a section of the README for Genosaurus:
The easiest way to use Genosaurus is to let it do the work for you. Let‘s looked at what‘s called an ‘implied’ manifest:
dir:
simple_generator.rb
templates:
hello_world.txt.template
That‘s our folder structure. Now let‘s look at simple_generator.rb:
require 'rubygems' require 'genosaurus' class SimpleGenerator < Genosaurus end
Now if we run that generator:
$irb: SimpleGenerator.run
We should get a file called hello_world.txt generated in the current directory. Yes, it truly is that simple! With implied manifests our directory structure under ‘templates’ tells the whole story, and Genosaurus is smart enough to figure it out. All the file names, and the same goes for folders, need to end in .template, and Genosaurus will do the rest. All the files will go through ERB before they generated, so you can put all your lovely little dynamic goodies in there. File, and folder, names also get run through ERB so you can even make the file name dynamic too! Let‘s look at a more complex example:
dir:
complex_generator.rb
templates:
app:
views:
<%=param(:name).plural%>.template:
hello_world.html.erb
models:
<%=param(:name)%>.rb.template
Let‘s run our complex_generator.rb file:
require 'rubygems'
require 'genosaurus'
class ComplexGenerator < Genosaurus
require_param: name
end
Now if we run that generator:
$irb: ComplexGenerator.run("name" => "user")
Now you should end up with the following:
app:
views:
users:
hello_world.html.erb
models:
user.rb.template
In the ComplexGenerator we told Genosaurus that we are requiring that the parameter, name, be passed into it. We are then using that parameter to generate the names of some files and folders. Pretty cool, eh? See how simple that is.
Explicit manifests are used when there is a manifest.yml supplied at the same level as the generator. If there is a manifest.yml file then implied manifests are not used. This means you have to define the entire generation process. This is great if you have a pretty complicated generator, as the manifest.yml is also sent through ERB before being loaded. Let‘s look at the manifest.yml file for our simple_generator example:
template_1:
type: file
template_path: <%= File.join(templates_directory_path, "templates", "hello_world.txt.template")
output_path: hello_world.txt
Pretty simple. We give the template a name, template_1, it really doesn‘t matter what it is, but Hash objects need keys. The ‘type’ parameter is either file or directory. The template_path is the path to the template. Finally, the output_path is the where you want the file to be generated. Let‘s look at our more complex example. We can change the directory structure a bit, since we really don‘t need ERB in the file names now:
dir:
complex_generator.rb
templates:
hello_world.html.erb.template
model.rb.template
Our manifest.yml file would look like this:
hello_world_template:
type: file
template_path: <%= File.join(templates_directory_path, "templates", "hello_world.html.erb")
output_path: <%= File.join("app", "views", param(:name).plural, "hello_world.html.erb") %>
model_template:
type: file
template_path: <%= File.join(templates_directory_path, "templates", "model.html.erb")
output_path: <%= File.join("app", "models", "#{param(:name)}.rb") %>
This will generate the exact same thing as our implied manifest.
So yesterday I committed a Mack adapter to Thin. What does this mean? It means that in the next release of Thin there will native support for Mack. That means We can get rid of those silly config/thin.yml and config/thin.ru files. It also means we can make really easy use of all that the ‘thin’ command line executable has to offer.
When the next version of Thin comes out, there will be an update to Mack to support these changes.
This post has moved to the wiki @ http://wiki.mackframework.com/index.php/Blog%20Tutorial