Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. require 'json'
  2. require 'json/pure/generator'
  3.  
  4. module JSON::Pure::Generator::GeneratorMethods
  5. %i(Fixnum Bignum).each do |constant|
  6. const_set constant, Integer
  7. end
  8.  
  9. module Array
  10. alias_method :pure_to_json, :to_json
  11.  
  12. def to_json(state = nil, *)
  13. empty? ? '[]' : pure_to_json(state)
  14. end
  15. end
  16.  
  17. module Hash
  18. alias_method :pure_to_json, :to_json
  19.  
  20. def to_json(state = nil, *)
  21. empty? ? '{}' : sort.to_h.pure_to_json(state)
  22. end
  23. end
  24.  
  25. remove_const :Integer
  26. end
  27.  
  28. namespace :normalize do
  29. desc 'normalize herokuconnect.json'
  30. task :json do
  31. filename = 'herokuconnect.json'
  32.  
  33. JSON.generator = JSON::Pure::Generator
  34. src = File.open(filename) { |io| JSON.load(io) }
  35. src.delete('connection')
  36. src['mappings'].sort_by! { |e| e['object_name'] }
  37. File.open(filename, 'w') { |io| io.write(JSON.pretty_generate(src)) }
  38. end
  39.  
  40. desc 'normalize Schemafile'
  41. task :schemafile do
  42. filename = 'Schemafile'
  43.  
  44. src = File.open(filename) { |io| io.read }
  45. File.open(filename, 'w') do |io|
  46. src.each_line.with_object([]) do |line, columns|
  47. if line.match(/\A\x20\x20/)
  48. next columns.push(line)
  49. end
  50. if line.match(/\Aend/)
  51. io.puts columns.sort_by { |e| e.split(/"/)[1] }
  52. columns.clear
  53. end
  54. io.puts line
  55. end
  56. end
  57. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement