ya2yaml 0.23 → 0.24

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README +5 -0
  2. data/lib/ya2yaml.rb +13 -2
  3. data/test/test.rb +32 -1
  4. metadata +2 -2
data/README CHANGED
@@ -60,6 +60,7 @@ A String which contains any non-UTF8 character will be regarded as "binary" and
60
60
  # set them individually
61
61
  obj.ya2yaml(
62
62
  :indent_size => 4,
63
+ :hash_order => ['name','age','address'],
63
64
  :minimum_block_length => 16,
64
65
  :printable_with_syck => true,
65
66
  :escape_b_specific => true,
@@ -75,6 +76,10 @@ A String which contains any non-UTF8 character will be regarded as "binary" and
75
76
  - default: 2
76
77
  - Number of indentation spaces for a level.
77
78
 
79
+ - :hash_order
80
+ - default: nil
81
+ - Array of hash keys indicating sort order (this option only works on a top-level hash).
82
+
78
83
  - :minimum_block_length
79
84
  - default: 0
80
85
  - Minimum length of a string emitted in block scalar style. If a string is shorter than this value, it will be emitted in one line.
data/lib/ya2yaml.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- # $Id: ya2yaml.rb,v 0.23 2006-09-14 05:42:04+09 funai Exp funai $
3
+ # $Id: ya2yaml.rb,v 0.24 2006-11-06 01:15:07+09 funai Exp funai $
4
4
  #
5
5
  # Author:: Akira FUNAI
6
6
  # Copyright:: Copyright (c) 2006 Akira FUNAI
@@ -45,7 +45,18 @@ class Ya2YAML
45
45
  '{}'
46
46
  else
47
47
  indent = "\n" + s_indent(level - 1)
48
- obj.keys.sort {|x,y| x.to_s <=> y.to_s }.collect {|k|
48
+ hash_order = @options[:hash_order]
49
+ if (hash_order && level == 1)
50
+ hash_keys = obj.keys.sort {|x,y|
51
+ x_order = hash_order.index(x) ? hash_order.index(x) : Float::MAX
52
+ y_order = hash_order.index(y) ? hash_order.index(y) : Float::MAX
53
+ o = (x_order <=> y_order)
54
+ (o != 0) ? o : (x.to_s <=> y.to_s)
55
+ }
56
+ else
57
+ hash_keys = obj.keys.sort {|x,y| x.to_s <=> y.to_s }
58
+ end
59
+ hash_keys.collect {|k|
49
60
  key = emit(k,level + 1)
50
61
  if (
51
62
  is_one_plain_line?(key) ||
data/test/test.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- # $Id: test.rb,v 1.2 2006-10-03 01:04:35+09 funai Exp funai $
3
+ # $Id: test.rb,v 1.3 2006-11-06 01:12:54+09 funai Exp funai $
4
4
 
5
5
  $: << (File.dirname(__FILE__) + '/../lib')
6
6
 
@@ -81,6 +81,37 @@ class TC_Ya2YAML < Test::Unit::TestCase
81
81
  }
82
82
  end
83
83
 
84
+ def test_hash_order
85
+ [
86
+ [
87
+ nil,
88
+ "--- \na: 1\nb: 2\nc: 3\n",
89
+ ],
90
+ [
91
+ [],
92
+ "--- \na: 1\nb: 2\nc: 3\n",
93
+ ],
94
+ [
95
+ ['c','b','a'],
96
+ "--- \nc: 3\nb: 2\na: 1\n",
97
+ ],
98
+ [
99
+ ['b'],
100
+ "--- \nb: 2\na: 1\nc: 3\n",
101
+ ],
102
+ ].each {|hash_order,yaml|
103
+ y = {
104
+ 'a' => 1,
105
+ 'c' => 3,
106
+ 'b' => 2,
107
+ }.ya2yaml(
108
+ :hash_order => hash_order
109
+ )
110
+ # p y
111
+ assert_equal(y,yaml)
112
+ }
113
+ end
114
+
84
115
  def test_normalize_line_breaks
85
116
  [
86
117
  ["\n\n\n\n", "--- \"\\n\\n\\n\\n\"\n",],
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: ya2yaml
5
5
  version: !ruby/object:Gem::Version
6
- version: "0.23"
7
- date: 2006-10-03 00:00:00 +09:00
6
+ version: "0.24"
7
+ date: 2006-11-06 00:00:00 +09:00
8
8
  summary: An UTF8 safe YAML dumper.
9
9
  require_paths:
10
10
  - lib