module CounterSupport
  def run
    all.each_with_object({}) do |visit, result|
      visit = visit.attributes
      prison = visit.delete('prison_name') || 'all'
      if result.key?(prison)
        result[prison] = result[prison].deep_merge(hash_visit_data(visit))
      else
        result[prison] = hash_visit_data(visit)
      end
    end
  end

protected

  def hash_visit_data(visit)
    visit.values.reverse.inject { |result, value|
      { value => result }
    }
  end
end

module CounterSupport
  def fetch_and_format
    all.each_with_object({}) do |visit, result|
      visit = visit.attributes
      prison = visit.delete('prison_name') || 'all'
      result[prison] = result.fetch(prison, {}).deep_merge(
        order_and_hash_visit_values(visit)
      )
    end
  end

protected

  # Takes the attributes of a visitor counter object, extracts the values,
  # orders them from least specific to most specific and returns a hash of
  # the results.
  #
  # For example, with these inputs:
  # {isoyear: 2016, month: 1, day: 1, state: 'booked', count: 2 }
  #
  # The output would be:
  #
  # 2016 => { 1 => { 1 => { 'booked' => 2 }}}
  def order_and_hash_visit_values(visit)
    visit.values.reverse.inject { |result, value|
      { value => result }
    }
  end
end

Clean up and try to make intent of method clearer
Clean up and try to make intent of method clearer

Paul was confused about what `#hash_visit_data` did. We disucssed, but
did not arrive at a better alternative.  In the end, we agreed that the
best way to deal with it was to rename it and include a comment. The
renaming only gets you so far...
