DHH releases make_refactorable
David Hennemeier Hannson just released a plugin to sort of “level the field” a bit within the rails community. Simply put, make_refactorable takes normal Ruby code and converts it into a more verbose, redundant format.
make_refactorable is like adding milk to your scrambled eggs. It’s passing the ball, when you know that you could make the shot. It’s less Don’t Repeat Yourself and more We’re Everybody’s Team. It’s a clever way of generating bad-to-not-too-bad-to-really-good-looking-but-still-bad code quickly and easily, and it rocks.
h4. Installation
script/plugin install make_refactorable
h4. Basic Usage
Let’s say you have a simple Post model defined in app/models/post.rb with a single method for returning the user’s most important data:
class Post < ActiveRecord::Base
def age_sex_location
"#{self.name}/#{self.sex}/#{self.location}"
end
end
To use make_refactorable, simply call the ‘script/unrefactor’ command from the root of your application directory, passing it the name of whatever file you want to, duh… make refactorable.
script/generate refactorable app/models/post.rb
This generates a new file, app/models/refactorable_post.rb, that looks something like the following:
class RefactorablePost < Post
def age_sex_location
str = ""
1.times do |i|
str << "#{self.name}/#{self.sex}/#{self.location}".gsub("#{self.name}","#{self.name.upcase.downcase}")
end
return str
end
end
Note that the above version of app/models/refactorable_post.rb is only an example of what the output MIGHT look like. make_refactorable will generate pseudo-random yet verifiably redundant output, depending on the options passed to the script/make_refactorable call.
The following command…
script/generate refactorable app/models/post.rb calculate_state:private
Generates the following app/models/refactorable_post.rb
class RefactorablePost < Post
def age_sex_location
return calculate_state
end
private
def calculate_state
mystring = ""
1.times do |i|
mystring << "#{self.name}/#{self.sex}/#{self.location}".gsub("#{self.name}","#{self.name.upcase.downcase}")
end
return mystring
end
end
h4. Advanced Usage
# make two files refactorable
script/generate refactorable app/models/post.rb app/helpers/post_helper.rb
# cross-unrefactor two files
script/generate refactorable app/models/post.rb app/helpers/post_helper.rb cross:true
h4. But why, DHH? Why?!
Hannson explains that, “sometimes, even I write code that is TOO neat.. too DRY. At 37signals, we needed a tool to help some of the less experienced rails developers feel like they had something to contribute to the code I was writing. Naturally, we turned to Ruby, and the result of our investigation was make_refactorable.”
0
comments
