Thor – an intro

Thor is a simple and efficient tool for building self-documenting command line utilities. It removes the pain of parsing command line options, writing “USAGE:” banners, and can also be used as an alternative to the Rake build tool. The syntax is Rake-like, so it should be familiar to most Rake users.

I have built a corporate security model. When I say “security model” I am talking in a generic fashion. So what is it? Well it is ER-model for a relational database which allows me to record people objects, their web user objects, the organization objects that they belong to, the role objects for web app usage rights, and many other objects related to general web application use by humans.

When implementing such a model in an MVC architecture like RubyOnRails there will be many files located in various directories. This is not a big problem at all until you decide that you want to use this security model’s functionality in other RubyOnRails web applications. The obvious solution is to build some kind of plugin. Indeed, frameworks such as RubyOnRails do provide some sort of support for building plugins that can be reused.

In the Ruby development world we have the excellent Rake build and automation tool. However, this article will explore Thor which can be considered a kind of “next generation” Rake tool.

I’ll keep this post short and explore more details at a later time. For now let’s take a look at my Thor “configuration.”

[code]
…dev/_thor/testarea$ thor installed
Modules Namespaces
——- ———-
authena authena
fbptools fbp_tools

authena
——-
thor authena:install NAMEs files #
thor authena:removes installed files #

fbp_tools
———
thor fbp_tools:install NAME # installs files
thor fbp_tools:uninstall NAME # removes installed files

[/code]

We see that there are two Thor packages installed in my environment. I built them both. Authena is the name of the Thor task package which installs and uninstalls the security model files. fbp_tools is the package which installs some utility files which I use in many Rails apps.

Both packages install and remove files into various directories such as the “models”, “views”, and “controllers” directories.

Let me finish with a teaser showing you what happens when we install the Authena package. Next time, I’ll dig into the code a bit deeper.

[code]
…testarea$ thor authena:install
create app/models/accessibility.rb
create app/models/address.rb
create app/models/contact.rb
create app/models/organization_binding.rb
create app/models/organization.rb
create app/models/person.rb
create app/models/role.rb
create app/models/software_application.rb
create app/models/user.rb
create app/views/accessibilities/_access.html.haml
create app/views/accessibilities/change_user_role_state.js.rjs
create app/views/accessibilities/invalid_state_change.js.rjs
create app/views/layouts/admin.html.haml
create app/views/layouts/organizations.html.haml
create app/views/layouts/user_sessions.html.haml
create app/views/layouts/users.html.haml
create app/views/organizations/edit.html.haml
create app/views/organizations/_form.html.haml
create app/views/organizations/index.html.haml
create app/views/organizations/new.html.haml
create app/views/organizations/show.html.haml
create app/views/people/cancel_access_edit_area.js.rjs
create app/views/people/edit.html.haml
create app/views/people/new.html.haml
create app/views/people/_roles_by_user.html.haml
create app/views/people/show.html.haml
create app/views/people/destroy.html.haml
create app/views/people/index.html.haml
create app/views/people/_people_list.html.haml
create app/views/people/show_access_edit_area.js.rjs
create app/views/roles/_action_menu.html.haml
create app/views/roles/index.html.haml
create app/views/roles/show.html.haml
create app/views/users/_accessibilities_for_users.html.haml
create app/views/users/change_user_teacher.js.rjs
create app/views/users/_form.html.haml
create app/views/users/show_assign_to_teacher_area.js.rjs
create app/views/users/cancel_assign_student_to_teacher_area.js.rjs
create app/views/users/edit.html.haml
create app/views/users/new.html.haml
create app/views/users/show.html.haml
create app/controllers/accessibilities_controller.rb
create app/controllers/admin_controller.rb
create app/controllers/managers_controller.rb
create app/controllers/organizations_controller.rb
create app/controllers/people_controller.rb
create app/controllers/roles_controller.rb
create app/controllers/users_controller.rb
create app/helpers/accessibilities_helper.rb
create app/helpers/admin_helper.rb
create app/helpers/managers_helper.rb
create app/helpers/organizations_helper.rb
create app/helpers/people_helper.rb
create app/helpers/roles_helper.rb
create app/helpers/user_sessions_helper.rb
create app/helpers/users_helper.rb
create db/schema.rb
[/code]

As you can image it would be very time consuming to copy these files manually every time you want to add this module to your MVC application!!!

Removing this security model’s files is quite easy as well:

[code]
…/testarea$ thor authena:uninstall
remove app/models/accessibility.rb
remove app/models/address.rb
remove app/models/contact.rb
remove app/models/organization_binding.rb
remove app/models/organization.rb
remove app/models/person.rb
remove app/models/role.rb
remove app/models/software_application.rb
remove app/models/user.rb
remove app/views/accessibilities/_access.html.haml
remove app/views/accessibilities/change_user_role_state.js.rjs
remove app/views/accessibilities/invalid_state_change.js.rjs
remove app/views/layouts/admin.html.haml
remove app/views/layouts/organizations.html.haml
remove app/views/layouts/user_sessions.html.haml
remove app/views/layouts/users.html.haml
remove app/views/organizations/edit.html.haml
remove app/views/organizations/_form.html.haml
remove app/views/organizations/index.html.haml
remove app/views/organizations/new.html.haml
remove app/views/organizations/show.html.haml
remove app/views/people/cancel_access_edit_area.js.rjs
remove app/views/people/edit.html.haml
remove app/views/people/new.html.haml
remove app/views/people/_roles_by_user.html.haml
remove app/views/people/show.html.haml
remove app/views/people/destroy.html.haml
remove app/views/people/index.html.haml
remove app/views/people/_people_list.html.haml
remove app/views/people/show_access_edit_area.js.rjs
remove app/views/roles/_action_menu.html.haml
remove app/views/roles/index.html.haml
remove app/views/roles/show.html.haml
remove app/views/users/_accessibilities_for_users.html.haml
remove app/views/users/change_user_teacher.js.rjs
remove app/views/users/_form.html.haml
remove app/views/users/show_assign_to_teacher_area.js.rjs
remove app/views/users/cancel_assign_student_to_teacher_area.js.rjs
remove app/views/users/edit.html.haml
remove app/views/users/new.html.haml
remove app/views/users/show.html.haml
remove app/controllers/accessibilities_controller.rb
remove app/controllers/admin_controller.rb
remove app/controllers/managers_controller.rb
remove app/controllers/organizations_controller.rb
remove app/controllers/people_controller.rb
remove app/controllers/roles_controller.rb
remove app/controllers/users_controller.rb
remove app/helpers/accessibilities_helper.rb
remove app/helpers/admin_helper.rb
remove app/helpers/managers_helper.rb
remove app/helpers/organizations_helper.rb
remove app/helpers/people_helper.rb
remove app/helpers/roles_helper.rb
remove app/helpers/user_sessions_helper.rb
remove app/helpers/users_helper.rb
remove db/schema.rb
[/code]

Leave a Reply

Your email address will not be published. Required fields are marked *