Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement