Refactor large if blocks into idiomatic Ruby