~srushe/cricsheet-xml-converter

318b7fe3b64ae7e5189930fccaca08f661efe1d9 — Stephen Rushe 16 days ago 87bb4a0 master
Update convert.rb to correctly sort XML
1 files changed, 30 insertions(+), 2 deletions(-)

M convert.rb
M convert.rb => convert.rb +30 -2
@@ 5,6 5,28 @@ require 'yaml'
require 'optparse'
require 'fileutils'
require 'active_support/core_ext/hash/conversions'
require 'active_support/core_ext/hash/deep_transform_values'

# See https://www.jvt.me/posts/2020/08/24/sort-recursive-hash-ruby/
def recursive_sort(v, &block)
  if v.class == Hash
    v.sorted_hash(&block)
  elsif v.class == Array
    v.collect {|a| recursive_sort(a, &block)}
  else
    v
  end
end

class Hash
  def sorted_hash(&block)
    self.class[
      self.each do |k,v|
        self[k] = recursive_sort(v, &block)
      end.sort(&block)]
  end
end


# A simple class to override default ruby YAML parsing of the over and
# ball entry from the YAML. The 0.9 data has it as a simple float,


@@ 74,6 96,8 @@ files.each_with_index do |file, index|
      { 'team' => team, 'player' => player }
    end
  end
  data['info']['outcome'] = data['info']['outcome'].sort.to_h
  data['info'] = data['info'].sort.to_h

  data[:innings] = yaml['innings'].collect.with_index do |inning, inning_index|
    inning_name = inning.keys.first


@@ 91,8 115,12 @@ files.each_with_index do |file, index|
                     end

      { over: over, ball: ball }
        .merge(delivery_data.except('wicket'))
        .merge(wickets_data)
        .merge(
          delivery_data.except('wicket')
                       .compact
                       .sorted_hash
        )
        .merge(wickets_data.sorted_hash)
    end

    { inningsNumber: inning_index + 1 }