Mailinglist Archive: yast-devel (246 mails)

< Previous Next >
[yast-devel] users controller refactoring, part II
  • From: "Duncan Mac-Vicar Prett" <dmacvicar@xxxxxxx>
  • Date: Wed, 5 Aug 2009 18:27:17 +0200
  • Message-id: <200908051827.17662.dmacvicar@xxxxxxx>
Another refactoring done has to do with 2 things:

1) use the stdlib or look around (active_support has quite a lot of stuff
already)
2) Don't repeat yourself

# FIXME convert ruby's under_scored keys to YaST's camel-cased ones
data["cn"] = [ "s", self.cn ] unless
self.cn.blank?
data["userPassword"] = [ "s", self.user_password ] unless
self.user_password.blank?
data["homeDirectory"] = [ "s", self.home_directory ] unless
self.home_directory.blank?
data["loginShell"] = [ "s", self.login_shell ] unless
self.login_shell.blank?
data["uidNumber"] = [ "s", self.uid_number ] unless
self.uid_number.blank?
data["groupname"] = [ "s", self.groupname ] unless
self.groupname.blank?


Googling, you can find that activesupport already includes this functionality:
http://refactormycode.com/codes/502-camelize

"hello_world".camelize(:lower)
=> helloWorld

Also consider using ActiveSupport attr_accessor_with_default ;-)

Then, if you see code like the assignment above, you can save lot of typing by
extracting the code in methods. For example you can create an array with cn,
userPassword, homeDirectory, etc and iterate it, and then assign to data on
each iteration. Using camelize to adapt the name on the way. Then even better,
you can extract that to a method that taking an object (self in this case)
creates the data structure, (in the refactoring, this was retrieve_data )

Duncan
--
To unsubscribe, e-mail: yast-devel+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: yast-devel+help@xxxxxxxxxxxx

< Previous Next >