Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added :preserve_order option

  • Loading branch information...
commit c31bd307b38ce3f078e9a6cd8101075485bee303 1 parent 1ef6b62
@klobuczek authored
Showing with 22 additions and 0 deletions.
  1. +5 −0 README.rdoc
  2. +2 −0  lib/ya2yaml.rb
  3. +15 −0 test/test.rb
View
5 README.rdoc
@@ -67,6 +67,7 @@ A String which contains any non-UTF8 character will be regarded as "binary" and
:printable_with_syck => true,
:escape_b_specific => true,
:escape_as_utf8 => true
+ :preserve_order => true
)
# or simply set this for a safe roundtrip with Syck.
@@ -98,6 +99,10 @@ A String which contains any non-UTF8 character will be regarded as "binary" and
- default: false
- When set to true, Ya2YAML uses Ruby-like escape sequences in UTF8 code instead of "\uXXXX" style in UCS code. It also suppresses use of "\L" and "\P" (escape sequences for LS and PS).
+- :preserve_order
+ - default: false
+ - When set to true, the order of keys in hashes will be the natural hash order rather than sorted alphabetically or explicitelly (usefull for syck/psych roundtrip and ruby >= 1.9.2)
+
- :syck_compatible
- default: false
- When set to true, These options are set to true at once. You have to set this to false when you set them individually.
View
2  lib/ya2yaml.rb
@@ -54,6 +54,8 @@ def emit(obj, level)
o = (x_order <=> y_order)
(o != 0) ? o : (x.to_s <=> y.to_s)
}
+ elsif @options[:preserve_order]
+ hash_keys = obj.keys
else
hash_keys = obj.keys.sort {|x, y| x.to_s <=> y.to_s }
end
View
15 test/test.rb
@@ -137,6 +137,21 @@ def test_hash_order
}
end
+ def test_preserve_order
+ h = {}
+ h['a'] = 1
+ h['c'] = 3
+ h['b'] = 2
+ y = h.ya2yaml(
+ :preserve_order => true
+ )
+ assert_equal(
+ "--- \na: 1\nc: 3\nb: 2\n",
+ y,
+ 'the natural hash order should be preserved'
+ )
+ end if RUBY_VERSION >= "1.9"
+
def test_normalize_line_breaks
[
["\n\n\n\n", "--- \"\\n\\n\\n\\n\"\n"],
Please sign in to comment.
Something went wrong with that request. Please try again.