It works! What this does is namespace the Mash defined when I require the Twitter gem, so that Mash is now Twitter::Mash. Also, Extlib’s Mash is still there, untouched, so you don’t need to worry about that. Here’s how Aikidoka does its magic:
It temporarily renames existing constants so that they don’t get clobbered. In this case, “Mash” is renamed to “AikidokaMash”. Right now this only works with top-level constants.
It yields to the given block. This block should define the constants you want permanently renamed/namespaced. In this case, we’re requiring “twitter”, which in turn requires “mash”. “mash” defines the constant we want to rename, Mash.
It creates modules as necessary to create the namespace. In this case, the module Twitter is already defined so that’s used. However, if we wanted to rename “Mash” to “Potatoes::Mash”, then a module named “Potatoes” would have been created.
It assigns the object referred to by the old constant to its new constant. “Twitter::Mash” now refers to the same object that “Mash” refers to.
Old constants are removed to clean up the namespace. The constant “Mash” no longer exists, the object it used to refer to lives on.
The constants temporarily renamed in step 1 are now given their original names back. Extlib’s “Mash” is no longer “AikidokaMash”; it’s “Mash” again.
The code is very simple - a total of 67 lines in one file with decent specs - so hopefully it’s easy to dig into.
Right now Aikidoka is best at nesting an existing top-level constant within another constant of a different name. I haven’t tried doing something like Aikidoka.rename("Mash" => "Mash::Twitter") or Aikidoka.rename("ActiveRecord::Base" => "ARBase"), and those examples probably wouldn’t work.
All in all, it does what I want it to and seems to work OK :) You can install it with “gem install flyingmachine-aikidoka“. If you’re wondering about the name, aikido is a martial art designed to resolve conflict harmoniously, and an aikidoka is a student of aikido.
I’ve forked jnunemaker’s twitter and updated to use peterpunk’s Mhash gem instead of the Mash gem it was using. This avoids the namespace collision with Mash in datamapper’s extlib library.
I’ve been trying to make merb a little bit easier to use by implementing form element classes. The approach I’ve taken is influenced by my experience with Cocoa. The view classes I’ve created encapsulate behavior for displaying complex form elements and for parsing the data sent to controllers by the form elements.
One great advantage that Cocoa development has over web development with an MVC framework like merb is that your views are first-class objects and can be communicated with directly using the same language as the rest of the system. With a web app, you have to go to extra lengths so that your Model or Controller will correctly get data from a form element if the element is even slightly complex. Most likely you’ll need to use Javascript in addition to whatever backend language you’re using. You’ll probably also have a lot of code to parse those elements in your controllers, spreading the concept your form element represents all over the place. In Cocoa, interacting with complex “form elements” is easier and cleaner.
Brief Cocoa Example
One iPhone app I’m on working on stores a time interval in seconds. Since it’s not very user-friendly to make a user figure that out, I use a picker that allows him to specify days, hours, and minutes.
This picker is an instance of a subclass of UIPickerView, which means it’s a first-class object and I can define methods on it to get at its tasty insides. The advantage here is that methods that belong together conceptually are placed together physically. The salient method is below:
From what I understand, this is all nothing special in Cocoa development.
The Web App Problem
merb (and Rails) have no mechanism for treating form elements as objects. Form elements are displayed using javascript, templates, and helpers. Then their data is sent to a controller as a hash. They’re usually parsed with code in the controller.
For example, traineo.com we have the following form elements:
For these form elements, we use javascript to change the weight input when the user clicks a radio button.
Initially, we had some code in our controller to get the “weight” value and convert it to kilograms so that we could then pass it to a model. Something like
if (params[:weight_input]["stone"])
# Convert from stone to kg
elsif (params[:weight_input]["american"])
# Convert from lbs to kg
else
# Leave as is; already in kg
This worked ok, but once we started placing the weight input fields in other forms, the code had to be improved. We could have created a method in ApplicationController for parsing date input, but it didn’t seem like good OO programming to make ApplicationController aware of and responsible for one set of form fields. Better to make a class and take advantage of Ruby’s object goodness.
So my code could use some improvement, but I think it lays some good groundwork for treating form elements as objects in Merb. Any feedback would be greatly appreciated!
Don’t wait until you “feel like it” to do something
In fact, doing the thing usually puts you in the right mood to be doing it.I learned this one when my uncle moved from Boston to New Jersey. Before, he was a 45 minute drive away. Now, I’d have to travel at least 4 hours to see him. I regret that I hadn’t gone to see him more, even though I didn’t always feel like it.
Everything will take longer than you expect it to
Well, almost everything. Phone calls that should take 5 minutes will take 20. Errands that you think will take one hour will take two. Even when you think you’ve learned and adjusted your expectations, things will still take longer. This one hit me when my friend, my girlfriend, and I drove to downtown Boston to see a friend perform. We should have been 10 minutes early, but we couldn’t find a parking spot and arrived a little late. This lesson was confirmed recently when I left 30 minutes early to go to an aikido class and ended up 5 minutes late because of unexpected traffic.
There’s no reason to worry
Are you worried that something bad is going to happen? Well something bad IS going to happen, so how’s worrying going to make any difference? Besides, worrying will drain your resources prematurely and make it harder for you to cope. I learned this one from getting laid off twice in three months.
What it means
Around the time that I got laid off for the second time, I was listening to the Alan Watts podcast a lot. He talks mostly about buddhism and mentioned that “nirvana” means “breathe out” - as in, if you hold your breath, you’ll suffocate. In the same way, if you cling to life, you’ll “suffocate”. But if you breathe out, your breath will come back to you, and it works the same with life.
I try to keep this in mind when I have too much inertia to do something, when I’m becoming impatient because something’s taking longer than I expected, or when I find my energy being drained by worry.
A few years ago I ordered DSL service. The DSL modem came with an ethernet cable, and one end was labeled “this end goes in your computer”, and the other was labeled “this end goes in the modem.” Obviously, it doesn’t matter which end goes where, but the labels undoubtedly eliminated any possible confusion.
I’ve been learning more about electronics, in part to get a ground-up understanding of computers. In the process I’ve become interested in learning to build robots.
My first attempt at building a robot, about 8 years ago, wasn’t really successful. I was in high school and I wanted to build a robot that would turn off my light switch across the room because at night I would read in bed, and when I was done I didn’t want to have to get up to turn off the light. So I got my mom to buy me Lego Mindstorms one Christmas and put together a robot, and it would just run into the wall and fall apart. I tried a little more and kept getting the same result, so I just said “fuck it” and I bought a lamp to put beside my bed. Incidentally, that’s still how I approach problems most of the time.
Anyway, one thing I’d like to make is a little wristband with an LCD (like a digital watch) displays random words throughout the day. After that, a robot parrot which repeats words it hears often. It’ll have Barry White’s voice.
Normally I don’t watch TV, much less blog about it, but last night on “My Life on the D-List” they showed Steve Wozniak, billionaire inventor of the personal computer, bragging about how many Kathy Griffin t-shirt he sold at a convention for bears (not the animal - big, burly, gay men). Hilarious.
They also showed Woz going to a “bear maul” (a bazaar for big burly gay men paraphernalia), picking up a whip and saying, “This must be a movie prop.” I tried to find a clip, but only found this lousy 2 minute piece from bravo. It does show Woz briefly at the beginning of the clip.