following structure
```ruby
    def update_hostgroup_list
      new_role_hostgroups = layout.layout_roles.map do |layout_role|
        role_hostgroup = deployment_role_hostgroups.
            where(role_id: layout_role.role).
            first_or_initialize { |drh| drh.hostgroup = Hostgroup.nest(layout_role.role.name, hostgroup) }

        role_hostgroup.hostgroup.add_puppetclasses_from_resource(layout_role.role)
        role_hostgroup.hostgroup.save!

        role_hostgroup.deploy_order = layout_role.deploy_order
        role_hostgroup.save!

        role_hostgroup
      end
      
      # delete any prior mappings that remain
      (deployment_role_hostgroups - new_role_hostgroups).
          each { |role_hostgroup| role_hostgroup.hostgroup.destroy }
    end
  end
```
would be more readable for me but that is matter of taste so feel free to ignore this comment :)