this seems quite general. i'd make it a top-level function, or perhaps even move into lib/helpers?