aboutsummaryrefslogtreecommitdiff
path: root/lib/core_ext/json.rb
diff options
context:
space:
mode:
authorelijah <elijah@riseup.net>2014-11-24 22:45:27 -0800
committerelijah <elijah@riseup.net>2014-11-24 22:45:27 -0800
commitb839376a507e37a048ea2df53127ed0884310f60 (patch)
tree68ba30a77c51a3ee1a2c9ddd7187e6743cff08f1 /lib/core_ext/json.rb
parent8450768268c2bdf82cd6d6bfa9972c70bc5cdcac (diff)
downloadleap_cli-b839376a507e37a048ea2df53127ed0884310f60.tar.gz
leap_cli-b839376a507e37a048ea2df53127ed0884310f60.tar.bz2
moved core_ext and lib_ext under leap_clidevelop
Diffstat (limited to 'lib/core_ext/json.rb')
-rw-r--r--lib/core_ext/json.rb42
1 files changed, 0 insertions, 42 deletions
diff --git a/lib/core_ext/json.rb b/lib/core_ext/json.rb
deleted file mode 100644
index 1a82bd9..0000000
--- a/lib/core_ext/json.rb
+++ /dev/null
@@ -1,42 +0,0 @@
-module JSON
- #
- # Output JSON from ruby objects in such a manner that all the hashes and arrays are output in alphanumeric sorted order.
- # This is required so that our generated configs don't throw puppet or git for a tizzy fit.
- #
- # Beware: some hacky stuff ahead.
- #
- # This relies on the pure ruby implementation of JSON.generate (i.e. require 'json/pure')
- # see https://github.com/flori/json/blob/master/lib/json/pure/generator.rb
- #
- # The Oj way that we are not using: Oj.dump(obj, :mode => :compat, :indent => 2)
- #
- def self.sorted_generate(obj)
- # modify hash and array
- Array.class_eval do
- alias_method :each_without_sort, :each
- def each(&block)
- sorted = sort {|a,b| a.to_s <=> b.to_s }
- for i in 0..(sorted.length-1) do
- yield sorted[i]
- end
- end
- end
- Hash.class_eval do
- alias_method :each_without_sort, :each
- def each(&block)
- self.keys.each do |key|
- yield key, self.fetch(key) # fetch is used so we don't trigger Config::Object auto-eval
- end
- end
- end
-
- # generate json
- json_str = JSON.pretty_generate(obj)
-
- # restore hash and array
- Hash.class_eval {alias_method :each, :each_without_sort}
- Array.class_eval {alias_method :each, :each_without_sort}
-
- return json_str
- end
-end