Skip to content

Instantly share code, notes, and snippets.

@tarot
Last active December 1, 2015 04:31
Show Gist options
  • Save tarot/7f1358ac3f05eb2544d1 to your computer and use it in GitHub Desktop.
Save tarot/7f1358ac3f05eb2544d1 to your computer and use it in GitHub Desktop.
require 'json'
require 'json/pure/generator'
module JSON::Pure::Generator::GeneratorMethods
%i(Fixnum Bignum).each do |constant|
const_set constant, Integer
end
module Array
alias_method :pure_to_json, :to_json
def to_json(state = nil, *)
empty? ? '[]' : pure_to_json(state)
end
end
module Hash
alias_method :pure_to_json, :to_json
def to_json(state = nil, *)
empty? ? '{}' : sort.to_h.pure_to_json(state)
end
end
remove_const :Integer
end
namespace :normalize do
desc 'normalize herokuconnect.json'
task :json do
filename = 'herokuconnect.json'
JSON.generator = JSON::Pure::Generator
src = File.open(filename) { |io| JSON.load(io) }
src.delete('connection')
src['mappings'].sort_by! { |e| e['object_name'] }
File.open(filename, 'w') { |io| io.write(JSON.pretty_generate(src)) }
end
desc 'normalize Schemafile'
task :schemafile do
filename = 'Schemafile'
src = File.open(filename) { |io| io.read }
File.open(filename, 'w') do |io|
src.each_line.with_object([]) do |line, columns|
if line.match(/\A\x20\x20/)
next columns.push(line)
end
if line.match(/\Aend/)
io.puts columns.sort_by { |e| e.split(/"/)[1] }
columns.clear
end
io.puts line
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment