]> gitweb.fluxo.info Git - leap/leap_cli.git/commitdiff
ensure arrays are always sorted when exported to yaml
authorelijah <elijah@riseup.net>
Mon, 6 Oct 2014 05:56:20 +0000 (22:56 -0700)
committerelijah <elijah@riseup.net>
Mon, 6 Oct 2014 05:56:20 +0000 (22:56 -0700)
lib/core_ext/yaml.rb [new file with mode: 0644]
lib/leap_cli.rb
lib/leap_cli/config/object.rb

diff --git a/lib/core_ext/yaml.rb b/lib/core_ext/yaml.rb
new file mode 100644 (file)
index 0000000..bb0b5c9
--- /dev/null
@@ -0,0 +1,29 @@
+class Object
+  #
+  # ya2yaml will output hash keys in sorted order, but it outputs arrays
+  # in natural order. This new method, sorted_ya2yaml(), is the same as
+  # ya2yaml but ensures that arrays are sorted.
+  #
+  # This is important so that the .yaml files don't change each time you recompile.
+  #
+  # see https://github.com/afunai/ya2yaml/blob/master/lib/ya2yaml.rb
+  #
+  def sorted_ya2yaml(options = {})
+    # modify array
+    Array.class_eval do
+      alias_method :collect_without_sort, :collect
+      def collect(&block)
+        sorted = sort {|a,b| a.to_s <=> b.to_s}
+        sorted.collect_without_sort(&block)
+      end
+    end
+
+    # generate yaml
+    yaml_str = self.ya2yaml(options)
+
+    # restore array
+    Array.class_eval {alias_method :collect, :collect_without_sort}
+
+    return yaml_str
+  end
+end
index aa176550ed96839ec50e50790c015000c0aa58d0..fbfde593529b05a6682da831e0bd7370bea867cf 100644 (file)
@@ -17,6 +17,7 @@ require 'core_ext/boolean'
 require 'core_ext/nil'
 require 'core_ext/string'
 require 'core_ext/json'
+require 'core_ext/yaml'
 
 require 'leap_cli/log'
 require 'leap_cli/path'
index cfa07cbcb45685764d5b3704b56a717a77f5eb00..2d665815e609a1ffad06071b0682c9177537aac7 100644 (file)
@@ -40,7 +40,7 @@ module LeapCli
       #
       def dump_yaml
         evaluate(@node)
-        ya2yaml(:syck_compatible => true)
+        sorted_ya2yaml(:syck_compatible => true)
       end
 
       #