Written by Patrick Tulskie
I have a rake task that runs using the application’s production environment. Unlike the production environment though, it is threaded. In order to make any real use out of that, I need to increase the ActiveRecord connection pool size. Normally you’d do this in your database.yml, but in this case I’d rather not modify the production environment’s settings for the sake of a single rake task.
Here’s how you can make the change, on the fly, inside of your rake task:
task :swimming_pool => :environment do
ActiveRecord::Base.connection_pool.instance_variable_set('@size', 15)
ActiveRecord::Base.connection_pool.instance_variable_set('@timeout', 10)
# Do awesome threaded stuff here
end
The first line of the rake task increases your pool size and the second line changes your timeout when waiting for a connection from the pool. You can adjust those values to whatever you need. That just happened to be what I need for this particular task.
Posted in
Code,
ruby at June 16th, 2010.
No Comments.
Written by Patrick Tulskie
This morning, Kevin Glowacz (@kevinglowacz) replied to me a few times on Twitter about Ruby String#Nameize class extension I had posted a while back. I had done some work to it after posting it here. Kevin also asked me a few questions about oddities that were in it that have since been resolved. So thanks to his prodding, you get a slightly updated version…
The only real “feature” is that it will now handle full names just fine. Otherwise, the rest of the stuff was mostly performance related. Here it is:
class String
# Extension of the string class to properly handle camel names
def nameize
if self.match(/ /)
# If the name has a space in it, we gotta run the parts through the nameizer.
name = self.split(' ').each { |part| part.nameize! }.join(' ')
return name
elsif self.match(/^[A-Z]/)
# If they took the time to capitalize their name then let's just jump out.
return self
else
# If there are no spaces and there is no prior
# capitalization then let's downcase the whole thing.
name = self.downcase
end
# Let's now assume that they were lazy...
return case
when name.match(/^mac/)
name.gsub(/^mac/, "").capitalize.insert(0, "Mac")
when name.match(/^mc/)
name.gsub(/^mc/, "").capitalize.insert(0, "Mc")
when name.match(/^o\'/)
name.split("'").each{ |piece| piece.capitalize! }.join("'")
else
name.capitalize # name is a first name or it's not Irish then capitalize it.
end
end
def nameize!
replace nameize # BANG!
end
end
As always – question, comments, suggestions – shoot me an email, leave a comment, or hit me on Twitter (@PatrickTulskie).
Posted in
Code,
Software Development,
ruby at December 30th, 2008.
5 Comments.
Written by Patrick Tulskie
Every Friday we have our code reviews at BeenVerified and it is definitely a non-trivial event. Our development team looks through the code all together and offers suggestions and ways to improve what the creator deems near-complete code. Code reviews have become my favorite part of team based development because they offer me such a badass opportunity to learn more. Everyone looks a problem differently and so getting insight from other people is huge because you might not consider all of your options when you’re knee deep in 1000 lines of ruby, CSS, and js all at once.
Yeah it’s great, except this Friday it didn’t happen. Thanksgiving weekend happened instead so we pushed it to Monday. Being the silly goose I am, I decided to get a new Macbook on Black Friday. I restored my stuff from Time Machine, installed my Ruby Gems, and thought all was well. Monday morning, my turn to present code came up and there was a problem with screen sharing. Crap. All of my code is in a git branch that is not pushed to a server yet and the time it would have taken to get to a state where we could present it from another machine would have been too much so we postponed my review until my screen sharing would work. This was most displeasing to me. Read More…
Written by Patrick Tulskie
Those of you doing rails development work on Leopard with MySQL have probably seen this error message when starting your app:
WARNING: You’re using the Ruby-based MySQL library that ships with Rails. This library is not suited for production. Please install the C-based MySQL library instead (gem install mysql).
Normally I don’t care, but I figured since I was doing some cleanup today and getting things ready to move on to a longer term it might be good to have a properly working MySQL gem. I like to run with a system that is close to what we run production. The closer you get, the less surprises you have when you push it live.
Read More…
Written by Patrick Tulskie
This morning we had an email from someone who wanted us to capitalize their name because they had not done it at signup. That’s fine and all, but this is one of those things we’re going to see again. Normally you’d just slap a .capitalize on the string and call it a day. Unfortunately with Irish names, you run into a problem because capitalize doesn’t capitalize those types of names properly. I wrote a quick function to extend the String class in Ruby that will help get around this problem.
class String
# Extension of the string class to properly handle camel names
# Should be used on pieces of names, not full names.
def nameize
# If they took the time to capitalize their name then let's just jump out
if self.match(/\A[A-Z]/)
return self
else
name = self.downcase
end
# Let's now assume that they were lazy...
return case
when name.match(/^mac/)
name.capitalize.gsub(/Mac/, "").capitalize.insert(0, "Mac")
when name.match(/^mc/)
name.capitalize.gsub(/Mc/, "").capitalize.insert(0, "Mc")
when name.match(/^o\'/)
name.split("'").each{ |piece| piece.capitalize! }.join("'")
else
name.capitalize
end
end
end
I know it’s not perfect, but it should handle most oddly capitalized names. Let me know what you think in the comments or on Twitter or whatever and definitely give me any changes you think should be in there.
Posted in
Code,
Software Development,
ruby at October 24th, 2008.
No Comments.
Written by Patrick Tulskie
Quite frankly, I’m tired of about hearing about REST.
…
So what if I started off my post with a pun? Look you’re just going to have to deal with it. Let’s move on. No stop it with the face, it’s my blog and I can do what I want.
Anyhow – REST is one of those things I hear a lot of people talk about as if it is this big mystery. Then, when you finally find someone to explain it, it turns out they have never actually used it and only understand the theory. It’s so frustrating to the beginning Ruby on Rails developer because not everyone has a mini David Hansson in their pocket and it really is one of those things you need to see to understand.
Read More…
Posted in
Code,
Software Development at August 20th, 2008.
No Comments.
Written by Patrick Tulskie
I decided to start writing a little series of articles based on the lessons I’ve learned in writing code the uses twitter, using twitter itself, and just generally getting the most out of it. This is the first one, and maybe there will be more depending on how I like it.
The Problem – Why You Should Care
Other sites that you pull content from are not always going to be reliable. They could be down, running slow, or some other possible problem. If you are using something like twitter to display your status on your site but you have a lot of other content you want people to see then it would be wise to use client-side scripting to get the content. You might be saying in your head “Oh but I have this sweet PHP script that does that for me.” but you should tell that voice in your head to shut up and just listen.
Read More…
Written by Patrick Tulskie
I won’t sit here and proclaim myself to be some god of Grease Monkey but I do know a thing or two about Java Script. Basically, for those of you who are clueless as to what Grease Monkey is, it’s a plugin for Firefox that allows you to manipulate certain pages you visit using Java Script. Now that we know what it is and have tons of ideas flowing through our heads, let’s just do something kinda cool.
Today we’re going to beat up on certain gallery pages that use images of the format “imageName.sized.jpg”. What I mean by this is – we’re going to take the page (which is usually filled with junk) and replace everything with just the full size image and the caption.
Let’s get some assumptions out of the way:
You know SOME Java Script, the site you’re visiting also has the non-sized images in the same directory as the sized images, and you don’t care what else is on the web page. We’re also assuming there is only one caption with no “id” and we only have a style class to work with.
Ok? Ok. Let’s go.
Read More…
Posted in
Code at June 22nd, 2008.
No Comments.