diff options
author | elijah <elijah@riseup.net> | 2014-10-05 22:56:20 -0700 |
---|---|---|
committer | elijah <elijah@riseup.net> | 2014-10-05 22:56:20 -0700 |
commit | 5ee57cffa04dafd9174a47d7f9f29e5ed16d6927 (patch) | |
tree | bc1e55484ca52ac1709a625258516fd7147303b1 | |
parent | cfd626dc6c067a6465e962f3eeaa182cda994dba (diff) |
ensure arrays are always sorted when exported to yaml
-rw-r--r-- | lib/core_ext/yaml.rb | 29 | ||||
-rw-r--r-- | lib/leap_cli.rb | 1 | ||||
-rw-r--r-- | lib/leap_cli/config/object.rb | 2 |
3 files changed, 31 insertions, 1 deletions
diff --git a/lib/core_ext/yaml.rb b/lib/core_ext/yaml.rb new file mode 100644 index 0000000..bb0b5c9 --- /dev/null +++ b/lib/core_ext/yaml.rb @@ -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 diff --git a/lib/leap_cli.rb b/lib/leap_cli.rb index aa17655..fbfde59 100644 --- a/lib/leap_cli.rb +++ b/lib/leap_cli.rb @@ -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' diff --git a/lib/leap_cli/config/object.rb b/lib/leap_cli/config/object.rb index cfa07cb..2d66581 100644 --- a/lib/leap_cli/config/object.rb +++ b/lib/leap_cli/config/object.rb @@ -40,7 +40,7 @@ module LeapCli # def dump_yaml evaluate(@node) - ya2yaml(:syck_compatible => true) + sorted_ya2yaml(:syck_compatible => true) end # |