Factor out repeated code using groovy dynamics