RFC7159 7159.04 → 7159.05

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cde0201e044e37ab14b2426a4211058753632042
4
- data.tar.gz: 19b26b85d1f3f455b8dee3b591c403c729f88ed9
3
+ metadata.gz: b662d283a8723554682d016119ce8459a88a1acc
4
+ data.tar.gz: eda2d8e84dd1824e1eb698f1a428ebc00924af1d
5
5
  SHA512:
6
- metadata.gz: 54d23d27b59f817e73fe5da58b35f59ca86f1f4cee5527221c9831efbaf6a606427f8783a63df4d268a1793c14734661420442132cd11acb075111f4cef73687
7
- data.tar.gz: 6ef984c3d3b5569fb5c2c9dbc708b714c2d9678f77be69b3023c31be5a82a0bfb5036c9fe4aa2cfff483e4782b6a3f6eee3054b5953f06691154b51aa6108043
6
+ metadata.gz: cfb49eb59408e63948ad0c574de89b0339dbcb26ccf983a01c67f23f6feb7b5c7aad238d27a18df1058ca0bfb41164c7063ad60e6b639989dafd0c5933fcc5b3
7
+ data.tar.gz: 3e0df175d36f92735ed1d1dd70b38ad385888121fbc01742e54ec7eb3270328852afcaca39d58535c984a2c6d5e6fe4c25d0d8c2c2334899bc1152cad4a715ab
@@ -68,6 +68,18 @@ module RFC7159
68
68
  bag.start_dump obj
69
69
  return port
70
70
  end
71
+
72
+ # (Experimental) Sometimes JSON is required somewhere inside of a
73
+ # pretty-print, and you might want to inherit the indentation(s) of
74
+ # surrounding PP structure. In order to do so here we propose a dump
75
+ # variant that takes a PP object rather than destination IO.
76
+ # @param [::Object] obj The input (should be JSONable)
77
+ # @param [PP] pp The PP.
78
+ def self.pp_object pp, obj, indent=4
79
+ bag = RFC7159::Dumper.new nil, indent, nil, pp
80
+ bag.start_dump obj
81
+ return nil
82
+ end
71
83
  end
72
84
 
73
85
  #
@@ -108,8 +108,10 @@ class RFC7159::Array < RFC7159::Value
108
108
  def pretty_print pp
109
109
  hdr = sprintf '#<%p:%#016x', self.class, self.object_id << 1
110
110
  pp.group 1, hdr, '>' do
111
- pp.breakable
112
- @array.pretty_print pp
111
+ pp.text ' '
112
+ RFC7159::Dumper.kandr pp, 1, @array.each, '[', ']' do |i|
113
+ i.pretty_print pp
114
+ end
113
115
  end
114
116
  end
115
117
 
@@ -37,12 +37,30 @@ require 'prettyprint'
37
37
  # Dumps ruby object into JSON string
38
38
  class RFC7159::Dumper
39
39
 
40
+ # much like PP#object_group, except that it indents like K&R.
41
+ def self.kandr pp, indent, enum, open, close
42
+ pp.text open
43
+ pp.group_sub do
44
+ pp.nest indent do
45
+ enum.with_index do |a, i|
46
+ if i > 0
47
+ pp.text ','
48
+ end
49
+ pp.breakable ' '
50
+ yield a
51
+ end
52
+ end
53
+ pp.breakable ' '
54
+ end
55
+ pp.text close
56
+ end
57
+
40
58
  # @param [#<<] port output destination
41
- def initialize port, indent = 4, width = 79
59
+ def initialize port, indent = 4, width = 79, pp = PrettyPrint.new(port, width)
42
60
  @port = port
43
61
  @bag = Hash.new
44
62
  @indent = indent
45
- @pp = PrettyPrint.new @port, width
63
+ @pp = pp
46
64
  @bag.compare_by_identity
47
65
  end
48
66
 
@@ -125,21 +143,10 @@ class RFC7159::Dumper
125
143
  # much like PP#object_group, except that it indents like K&R.
126
144
  def kandr obj, method, open, close
127
145
  ensure_unique obj do
128
- enum = obj.send method
129
- @pp.text open
130
- @pp.group_sub do
131
- @pp.nest @indent do
132
- enum.with_index do |a, i|
133
- if i > 0
134
- @pp.text ','
135
- end
136
- @pp.breakable ' '
137
- yield a
138
- end
139
- end
140
- @pp.breakable ' '
146
+ enum = obj.enum_for method
147
+ RFC7159::Dumper.kandr @pp, @indent, enum, open, close do |obj|
148
+ yield obj
141
149
  end
142
- @pp.text close
143
150
  end
144
151
  end
145
152
 
@@ -62,7 +62,7 @@ class RFC7159::Object < RFC7159::Value
62
62
 
63
63
  # iterates over the pairs.
64
64
  # @yield [key, value] the pair.
65
- def each_pair
65
+ def each_pair &b
66
66
  e = Enumerator.new do |y|
67
67
  @assoc.each do |a|
68
68
  y << a
@@ -106,14 +106,11 @@ class RFC7159::Object < RFC7159::Value
106
106
  def pretty_print pp
107
107
  hdr = sprintf '#<%p:%#016x', self.class, self.object_id << 1
108
108
  pp.group 1, hdr, '>' do
109
- pp.breakable
110
- pp.group 1, '{', '}' do
111
- @assoc.each_with_index do |(k, v), i|
112
- pp.breakable ',' if i.nonzero?
113
- k.to_s.pretty_print pp
114
- pp.text ': '
115
- v.pretty_print pp
116
- end
109
+ pp.text ' '
110
+ RFC7159::Dumper.kandr pp, 1, @assoc.each, '{', '}' do |(i, j)|
111
+ i.pretty_print pp
112
+ pp.text ': '
113
+ j.pretty_print pp
117
114
  end
118
115
  end
119
116
  end
@@ -61,7 +61,7 @@ class RFC7159::String < RFC7159::Value
61
61
  def pretty_print pp
62
62
  hdr = sprintf '#<%p:%#016x', self.class, self.object_id << 1
63
63
  pp.group 1, hdr, '>' do
64
- pp.breakable
64
+ pp.text ' '
65
65
  @str.pretty_print pp
66
66
  end
67
67
  end
@@ -32,7 +32,7 @@
32
32
  # POSSIBILITY OF SUCH DAMAGE.
33
33
 
34
34
  # The version
35
- RFC7159::VERSION = 7159.04
35
+ RFC7159::VERSION = 7159.05
36
36
 
37
37
  #
38
38
  # Local Variables:
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: RFC7159
3
3
  version: !ruby/object:Gem::Version
4
- version: '7159.04'
4
+ version: '7159.05'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Urabe,
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-03-22 00:00:00.000000000 Z
12
+ date: 2015-04-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler