Sha256: 6b558bd4f5e73795532c4e1fc278a0c9f5adb30aa84e62d4bc15ebe48baeb2c6

Contents?: true

Size: 1.77 KB

Versions: 10

Compression:

Stored size: 1.77 KB

Contents

#!/usr/bin/env ruby

##
# A script to generate disposition_codes.yml from the MDES 2.1
# spreadsheet's Dispositions tab.
#
# This script requires the 'roo' gem, which is not included in
# ncs_mdes's gemspec because it has a huge number of dependencies and
# is not needed at runtime.

require 'roo'
require 'yaml'

MDES_XLSX = ARGV.first or fail 'Please provide the path to the MDES spreadsheet'
TARGET = File.expand_path('../disposition_codes.yml', __FILE__)
SHEET_NAME = 'Dispositions'

MAPPED_COLUMNS = {
  'A' => 'final_category',
  'B' => 'sub_category',
  'C' => 'disposition'
}

def normalize_whitespace(s)
  s.strip.gsub(/\s+/, " ")
end

book = Excelx.new(MDES_XLSX)

# This is the array of hashes that will eventually be serialized to
# disposition_codes.yml.
dispositions = []

current_event = nil
current_category_code = nil
1.upto(book.last_row(SHEET_NAME)) do |row_number|
  a, b = %w(A B).collect { |col| book.cell(row_number, col, SHEET_NAME) }
  if a =~ /Category\s+(\d)\s+\((.*?)\)\s+Disposition\s+Codes/
    current_event = normalize_whitespace $2
    current_category_code = $1.to_i
    puts "Collecting for category #{current_event} (#{current_category_code})"
  elsif b =~ /\S/ && a !~ /FINAL/
    disposition_hash = MAPPED_COLUMNS.inject({}) do |h, (col, key)|
      h[key] = normalize_whitespace(book.cell(row_number, col, SHEET_NAME)); h
    end
    disposition_hash['event'] = current_event
    disposition_hash['category_code'] = current_category_code
    disposition_hash['interim_code'], disposition_hash['final_code'] =
      normalize_whitespace(book.cell(row_number, 'D', SHEET_NAME)).split('/')

    puts disposition_hash.inspect if disposition_hash['interim_code'] =~ /^02/
    dispositions << disposition_hash
  end
end

File.open(TARGET, 'w') { |f| f.write dispositions.to_yaml }

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
ncs_mdes-0.13.0 documents/2.1/extract_disposition_codes.rb
ncs_mdes-0.12.0 documents/2.1/extract_disposition_codes.rb
ncs_mdes-0.11.0 documents/2.1/extract_disposition_codes.rb
ncs_mdes-0.10.0 documents/2.1/extract_disposition_codes.rb
ncs_mdes-0.9.0 documents/2.1/extract_disposition_codes.rb
ncs_mdes-0.8.1 documents/2.1/extract_disposition_codes.rb
ncs_mdes-0.8.0 documents/2.1/extract_disposition_codes.rb
ncs_mdes-0.7.0 documents/2.1/extract_disposition_codes.rb
ncs_mdes-0.6.1 documents/2.1/extract_disposition_codes.rb
ncs_mdes-0.6.0 documents/2.1/extract_disposition_codes.rb