object-stream 0.5 → 0.6

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: 2012223a66102b4befa32c0aaecbc6559f46d089
4
- data.tar.gz: 348120adfb1bdf1a62b439fe3f4a78a3887fce0c
3
+ metadata.gz: ae62e3547b3329038fd0f88e4959b230748371b6
4
+ data.tar.gz: 1069da86dac10a83b2f2859963dc3e38e351a8b4
5
5
  SHA512:
6
- metadata.gz: 03ea4758680c9209c65d178e938dc4025bbf37f09e73fd7a70955053aa09352edcf8c600b921cd5869735e4b0ce57227bd5202747fec618408786dd467f1ece0
7
- data.tar.gz: 5afa9b0995c5f93e4055608202ba4a86551eb071c8415102a152cab179834b407c6e69310548a3e2421c0b99c07f6e71ae4bd932e226cf22bfde5a6dfcfd44c1
6
+ metadata.gz: 1361f8ee2756822763c0077f029142202cf7ae46360d947d6da770555fcd4e4209b8a84e7ecb71d46bc910071187ee2f6487188a7e6f1534312bbb82754b5ef7
7
+ data.tar.gz: f726444e47692a37a3cb02756444da1bf4df0b2a621c79f91cfaf83530a93f1d519381fdbc244f2641429c596c5544dc0a36418524775cc7a73187c69c84671d
@@ -28,7 +28,7 @@ class ObjectStreamWrapper
28
28
  def to_s
29
29
  "#<Wrapped #{@stream.class} to #{peer_name}, io=#{@stream.inspect}>"
30
30
  end
31
-
31
+
32
32
  # Set the stream state so that subsequent objects returned by read will be
33
33
  # instances of a custom class +cl+. Does not affect #consume.
34
34
  # Class +cl+ should define cl.from_serialized, plus #to_json, #to_msgpack,
@@ -36,7 +36,7 @@ class ObjectStreamWrapper
36
36
  def expect cl
37
37
  @expected_class = cl
38
38
  end
39
-
39
+
40
40
  # Turn off the custom class instantiation of #expect.
41
41
  def unexpect; expect nil; end
42
42
 
@@ -56,7 +56,7 @@ class ObjectStreamWrapper
56
56
  end
57
57
  end
58
58
  private :try_consume
59
-
59
+
60
60
  def convert_to_expected obj
61
61
  if @expected_class and not obj.kind_of? @expected_class
62
62
  @expected_class.from_serialized(obj)
@@ -81,7 +81,7 @@ class ObjectStreamWrapper
81
81
  convert_to_expected(obj)
82
82
  end
83
83
  end
84
-
84
+
85
85
  def each
86
86
  return to_enum unless block_given?
87
87
  read {|obj| yield obj} until eof
@@ -89,12 +89,12 @@ class ObjectStreamWrapper
89
89
  end
90
90
 
91
91
  def write *objects
92
- @stream.write *objects
92
+ @stream.write(*objects)
93
93
  end
94
94
  alias << write
95
95
 
96
96
  def write_to_outbox *args, &bl
97
- @stream.write_to_outbox *args, &bl
97
+ @stream.write_to_outbox(*args, &bl)
98
98
  end
99
99
 
100
100
  def eof?
@@ -9,21 +9,21 @@
9
9
  # and yielding (non-blocking) read.
10
10
  module ObjectStream
11
11
  include Enumerable
12
-
13
- VERSION = "0.5"
14
-
12
+
13
+ VERSION = "0.6"
14
+
15
15
  # The IO through which the stream reads and writes serialized object data.
16
16
  attr_reader :io
17
-
17
+
18
18
  # Number of outgoing objects that can accumulate before the outbox is
19
19
  # serialized to the byte buffer (and possibly to the io).
20
20
  attr_reader :max_outbox
21
-
21
+
22
22
  MARSHAL_TYPE = "marshal".freeze
23
23
  YAML_TYPE = "yaml".freeze
24
24
  JSON_TYPE = "json".freeze
25
25
  MSGPACK_TYPE = "msgpack".freeze
26
-
26
+
27
27
  TYPES = [
28
28
  MARSHAL_TYPE, YAML_TYPE, JSON_TYPE, MSGPACK_TYPE
29
29
  ]
@@ -60,23 +60,23 @@ module ObjectStream
60
60
  @stream_class_map[type] = cl.call
61
61
  end
62
62
  end
63
-
63
+
64
64
  def register_type type, &bl
65
65
  @stream_class_map[type] = bl
66
66
  end
67
67
  end
68
-
68
+
69
69
  def initialize io, max_outbox: DEFAULT_MAX_OUTBOX, **opts
70
70
  @io = io
71
71
  @max_outbox = max_outbox
72
72
  @inbox = nil
73
73
  @outbox = []
74
74
  end
75
-
75
+
76
76
  def to_s
77
77
  "#<#{self.class} io=#{io.inspect}>"
78
78
  end
79
-
79
+
80
80
  # If no block given, behaves just the same as #read_one. If block given,
81
81
  # reads any available data and yields it to the block. This form is non-
82
82
  # blocking, if supported by the underlying serializer (such as msgpack).
@@ -104,7 +104,7 @@ module ObjectStream
104
104
  if @inbox and not @inbox.empty?
105
105
  return @inbox.shift
106
106
  end
107
-
107
+
108
108
  have_result = false
109
109
  result = nil
110
110
  until have_result
@@ -127,11 +127,11 @@ module ObjectStream
127
127
  end
128
128
  end
129
129
  private :read_from_inbox
130
-
130
+
131
131
  # Write the given objects to the stream, first flushing any objects in the
132
132
  # outbox. Flushes the underlying byte buffer afterwards.
133
133
  def write *objects
134
- write_to_buffer *objects
134
+ write_to_buffer(*objects)
135
135
  flush_buffer
136
136
  end
137
137
  alias << write
@@ -174,7 +174,7 @@ module ObjectStream
174
174
  read {|obj| yield obj} until eof
175
175
  rescue EOFError
176
176
  end
177
-
177
+
178
178
  def eof?
179
179
  (!@inbox || @inbox.empty?) && io.eof?
180
180
  end
@@ -187,62 +187,62 @@ module ObjectStream
187
187
  flush_outbox
188
188
  io.close
189
189
  end
190
-
190
+
191
191
  def closed?
192
192
  io.closed?
193
193
  end
194
-
194
+
195
195
  # Makes it possible to use stream in a select.
196
196
  def to_io
197
197
  io
198
198
  end
199
-
199
+
200
200
  class MarshalStream
201
201
  include ObjectStream
202
-
202
+
203
203
  ObjectStream.register_type MARSHAL_TYPE do
204
204
  self
205
205
  end
206
-
206
+
207
207
  def read_from_stream
208
208
  yield Marshal.load(io)
209
209
  end
210
-
210
+
211
211
  def write_to_stream object
212
212
  Marshal.dump(object, io)
213
213
  self
214
214
  end
215
215
  end
216
-
216
+
217
217
  class YamlStream
218
218
  include ObjectStream
219
-
219
+
220
220
  ObjectStream.register_type YAML_TYPE do
221
221
  require 'yaml'
222
222
  self
223
223
  end
224
-
224
+
225
225
  def read_from_stream
226
226
  YAML.load_stream(io) do |obj|
227
227
  yield obj
228
228
  end
229
229
  end
230
-
230
+
231
231
  def write_to_stream object
232
232
  YAML.dump(object, io)
233
233
  self
234
234
  end
235
235
  end
236
-
236
+
237
237
  class JsonStream
238
238
  include ObjectStream
239
-
239
+
240
240
  ObjectStream.register_type JSON_TYPE do
241
241
  require 'yajl'
242
242
  require 'yajl/json_gem'
243
243
  self
244
244
  end
245
-
245
+
246
246
  attr_accessor :chunk_size
247
247
 
248
248
  DEFAULT_CHUNK_SIZE = 2000
@@ -260,7 +260,7 @@ module ObjectStream
260
260
  @parser.on_parse_complete = bl
261
261
  @parser << io.readpartial(chunk_size)
262
262
  end
263
-
263
+
264
264
  def write_to_stream object
265
265
  @encoder.encode object, io
266
266
  self
@@ -269,25 +269,25 @@ module ObjectStream
269
269
 
270
270
  class MsgpackStream
271
271
  include ObjectStream
272
-
272
+
273
273
  ObjectStream.register_type MSGPACK_TYPE do
274
274
  require 'msgpack'
275
275
  self
276
276
  end
277
-
277
+
278
278
  attr_accessor :chunk_size
279
279
  attr_accessor :maxbuf
280
280
 
281
281
  DEFAULT_CHUNK_SIZE = 2000
282
282
  DEFAULT_MAXBUF = 4000
283
-
283
+
284
284
  # See the discussion in examples/symbolize-keys.rb.
285
285
  def initialize io, chunk_size: DEFAULT_CHUNK_SIZE, maxbuf: DEFAULT_MAXBUF,
286
286
  symbolize_keys: false
287
287
  super
288
288
  @unpacker = MessagePack::Unpacker.new(symbolize_keys: symbolize_keys)
289
289
  # don't specify io, so don't have to read all of io in one loop
290
-
290
+
291
291
  @packer = MessagePack::Packer.new(io)
292
292
  @chunk_size = chunk_size
293
293
  @maxbuf = maxbuf
@@ -301,7 +301,7 @@ module ObjectStream
301
301
  yield obj
302
302
  end
303
303
  end
304
-
304
+
305
305
  def fill_buffer n
306
306
  @unpacker.feed(io.readpartial(n))
307
307
  end
@@ -318,12 +318,12 @@ module ObjectStream
318
318
  "Exceeded buffer limit by #{@unpacker.buffer.size - maxbuf} bytes."
319
319
  end
320
320
  end
321
-
321
+
322
322
  def write_to_stream object
323
323
  @packer.write(object).flush
324
324
  self
325
325
  end
326
-
326
+
327
327
  def write_to_buffer *objects
328
328
  flush_outbox
329
329
  objects.each do |object|
@@ -331,7 +331,7 @@ module ObjectStream
331
331
  end
332
332
  self
333
333
  end
334
-
334
+
335
335
  def flush_buffer
336
336
  @packer.flush
337
337
  self
@@ -5,7 +5,7 @@ require 'minitest/autorun'
5
5
 
6
6
  module TestBasic
7
7
  attr_reader :sio, :stream
8
-
8
+
9
9
  # supported by all types
10
10
  BASIC_OBJECTS = [
11
11
  nil,
@@ -24,18 +24,17 @@ module TestBasic
24
24
  { ["a"] => 3 },
25
25
  { {"b" => 5} => 6 }
26
26
  ]
27
-
27
+
28
28
  class Custom
29
29
  attr_reader :x, :y
30
30
  def initialize x, y
31
31
  @x, @y = x, y
32
32
  end
33
33
  def ==(other)
34
- @x == other.x
35
- @y == other.y # just enough to make test pass
34
+ @x == other.x && @y == other.y
36
35
  end
37
36
  end
38
-
37
+
39
38
  RUBY_OBJECTS = [
40
39
  :foo,
41
40
  {:foo => :bar},
@@ -43,15 +42,15 @@ module TestBasic
43
42
  File,
44
43
  Custom.new(1,2)
45
44
  ]
46
-
45
+
47
46
  def type; self.class::TYPE; end
48
47
  def objects; BASIC_OBJECTS + self.class::OBJECTS; end
49
-
48
+
50
49
  def setup
51
50
  @sio = StringIO.new
52
51
  @stream = ObjectStream.new sio, type: type
53
52
  end
54
-
53
+
55
54
  def test_write_read
56
55
  objects.each do |obj|
57
56
  sio.rewind # do not need to clear stream's buffer (if any)
@@ -67,10 +66,10 @@ module TestBasic
67
66
  end
68
67
  end
69
68
  end
70
-
69
+
71
70
  def test_batch_write
72
71
  a = ["a", "b", "c"]
73
- stream.write *a
72
+ stream.write(*a)
74
73
  sio.rewind
75
74
  dump = sio.read
76
75
  sio.rewind
@@ -81,11 +80,11 @@ module TestBasic
81
80
  objects.each do |obj|
82
81
  stream.write obj
83
82
  end
84
-
83
+
85
84
  sio.rewind
86
85
  dump = sio.read
87
86
  sio.rewind
88
-
87
+
89
88
  assert_equal(objects, stream.to_a, # <-- #each called by #to_a
90
89
  "dump is #{dump.inspect}")
91
90
  end
@@ -95,16 +94,16 @@ module TestBasic
95
94
  a.each do |i|
96
95
  stream.write [i]
97
96
  end
98
-
97
+
99
98
  sio.rewind
100
-
99
+
101
100
  a2 = []
102
101
  stream.each do |object|
103
102
  i = object[0]
104
103
  a2 << i
105
104
  break if i == 5
106
105
  end
107
-
106
+
108
107
  stream.each do |object|
109
108
  i = object[0]
110
109
  a2 << i
@@ -128,7 +127,7 @@ module TestBasic
128
127
  enum = stream.each
129
128
  assert_equal(objects, enum.to_a)
130
129
  end
131
-
130
+
132
131
  def test_read_without_block
133
132
  n = 100
134
133
  n.times do |i|
@@ -9,7 +9,7 @@ class TestConsume < Minitest::Test
9
9
  def setup
10
10
  @sio = StringIO.new
11
11
  end
12
-
12
+
13
13
  ObjectStream::TYPES.each do |type|
14
14
  define_method "test_consume_#{type}" do
15
15
  do_test_consume_for type: type
@@ -19,33 +19,33 @@ class TestConsume < Minitest::Test
19
19
  def do_test_consume_for(type: type)
20
20
  n_total = 10
21
21
  n_consumed = 5
22
-
22
+
23
23
  objects = (0...n_total).map {|i| [i]}
24
-
24
+
25
25
  stream = ObjectStreamWrapper.new(sio, type: type)
26
26
  objects.each do |object|
27
27
  stream << object
28
28
  end
29
-
29
+
30
30
  sio.rewind
31
31
  stream = ObjectStreamWrapper.new(sio, type: type)
32
-
32
+
33
33
  count = 0
34
-
34
+
35
35
  n_consumed.times do |i|
36
36
  stream.consume do |a|
37
37
  assert_equal(i, a[0])
38
38
  count += 1
39
39
  end
40
40
  end
41
-
41
+
42
42
  assert_equal(0, sio.pos)
43
-
43
+
44
44
  stream.each_with_index do |a, i|
45
45
  assert_equal i + n_consumed, a[0]
46
46
  count += 1
47
47
  end
48
-
48
+
49
49
  assert_equal(n_total, count)
50
50
  end
51
51
  end
@@ -5,12 +5,12 @@ require 'minitest/autorun'
5
5
 
6
6
  class TestExpect < Minitest::Test
7
7
  attr_reader :sio
8
-
8
+
9
9
  class A
10
10
  def initialize x, y
11
11
  @x, @y = x, y
12
12
  end
13
-
13
+
14
14
  def to_msgpack pk = nil
15
15
  case pk
16
16
  when MessagePack::Packer
@@ -18,37 +18,37 @@ class TestExpect < Minitest::Test
18
18
  pk.write @x
19
19
  pk.write @y
20
20
  return pk
21
-
21
+
22
22
  else # nil or IO
23
23
  MessagePack.pack(self, pk)
24
24
  end
25
25
  end
26
-
26
+
27
27
  def to_a
28
28
  [@x, @y]
29
29
  end
30
-
30
+
31
31
  def to_json
32
32
  to_a.to_json
33
33
  end
34
-
34
+
35
35
  def self.from_serialized ary
36
- new *ary
36
+ new(*ary)
37
37
  end
38
-
38
+
39
39
  def == other
40
40
  self.class == other.class and
41
41
  to_a == other.to_a
42
42
  end
43
43
  end
44
-
44
+
45
45
  class B < A
46
46
  end
47
47
 
48
48
  def setup
49
49
  @sio = StringIO.new
50
50
  end
51
-
51
+
52
52
  def test_expect
53
53
  objects = []
54
54
  20.times do |i|
@@ -58,12 +58,12 @@ class TestExpect < Minitest::Test
58
58
  objects << "B" << B.new(i, i.to_s)
59
59
  end
60
60
  end
61
-
61
+
62
62
  stream = ObjectStreamWrapper.new(sio, type: ObjectStream::MSGPACK_TYPE)
63
63
  objects.each do |object|
64
64
  stream << object
65
65
  end
66
-
66
+
67
67
  sio.rewind
68
68
  stream = ObjectStreamWrapper.new(sio, type: ObjectStream::MSGPACK_TYPE)
69
69
  objects2 = []
@@ -5,30 +5,30 @@ require 'minitest/autorun'
5
5
 
6
6
  class TestInbox < Minitest::Test
7
7
  attr_reader :s, :t
8
-
8
+
9
9
  def setup
10
10
  @s, @t = UNIXSocket.pair
11
11
  end
12
-
12
+
13
13
  def test_marshal
14
14
  do_test(ObjectStream::MARSHAL_TYPE)
15
15
  end
16
-
16
+
17
17
  def test_yaml
18
18
  do_test(ObjectStream::YAML_TYPE)
19
19
  end
20
-
20
+
21
21
  def test_json
22
22
  do_test(ObjectStream::JSON_TYPE)
23
23
  end
24
-
24
+
25
25
  def test_msgpack
26
26
  do_test(ObjectStream::MSGPACK_TYPE)
27
27
  end
28
-
28
+
29
29
  def do_test type
30
30
  n = 200
31
- th = Thread.new do
31
+ Thread.new do
32
32
  src = ObjectStream.new(s, type: type)
33
33
  n.times do |i|
34
34
  src << [i]
@@ -5,11 +5,11 @@ require 'minitest/autorun'
5
5
 
6
6
  class TestMaxbuf < Minitest::Test
7
7
  attr_reader :sio, :stream
8
-
8
+
9
9
  def setup
10
10
  @sio = StringIO.new
11
11
  end
12
-
12
+
13
13
  def test_maxbuf
14
14
  stream = ObjectStream.new(sio, type: ObjectStream::MSGPACK_TYPE)
15
15
  stream << "a"*20
@@ -5,7 +5,7 @@ require 'minitest/autorun'
5
5
 
6
6
  class TestOutbox < Minitest::Test
7
7
  attr_reader :sio
8
-
8
+
9
9
  def setup
10
10
  @sio = StringIO.new
11
11
  end
@@ -6,27 +6,27 @@ require 'minitest/autorun'
6
6
 
7
7
  class TestSlowSender < Minitest::Test
8
8
  attr_reader :s, :t
9
-
9
+
10
10
  def setup
11
11
  @s, @t = UNIXSocket.pair
12
12
  end
13
-
13
+
14
14
  def test_marshal
15
15
  assert_equal(:block, get_test_result(ObjectStream::MARSHAL_TYPE))
16
16
  end
17
-
17
+
18
18
  def test_yaml
19
19
  assert_equal(:block, get_test_result(ObjectStream::YAML_TYPE))
20
20
  end
21
-
21
+
22
22
  def test_json
23
23
  assert_equal(:noblock, get_test_result(ObjectStream::JSON_TYPE))
24
24
  end
25
-
25
+
26
26
  def test_msgpack
27
27
  assert_equal(:noblock, get_test_result(ObjectStream::MSGPACK_TYPE))
28
28
  end
29
-
29
+
30
30
  def get_test_result type
31
31
  pid = fork do
32
32
  sio = StringIO.new
@@ -5,7 +5,7 @@ require 'minitest/autorun'
5
5
 
6
6
  module TestSymbolizeKeys
7
7
  attr_reader :sio, :stream
8
-
8
+
9
9
  BASIC_OBJECTS = [
10
10
  nil,
11
11
  true,
@@ -23,7 +23,7 @@ module TestSymbolizeKeys
23
23
  @sio = StringIO.new
24
24
  @stream = ObjectStream.new sio, type: type, symbolize_keys: true
25
25
  end
26
-
26
+
27
27
  def test_write_read
28
28
  objects.each do |obj|
29
29
  sio.rewind # do not need to clear stream's buffer (if any)
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: object-stream
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.5'
4
+ version: '0.6'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel VanderWerf
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-17 00:00:00.000000000 Z
11
+ date: 2014-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: msgpack
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: yajl-ruby
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: Stream objects over IO using Marshal, JSON, YAML, or Msgpack.
@@ -87,9 +87,9 @@ require_paths:
87
87
  - lib
88
88
  required_ruby_version: !ruby/object:Gem::Requirement
89
89
  requirements:
90
- - - ">="
90
+ - - "~>"
91
91
  - !ruby/object:Gem::Version
92
- version: '0'
92
+ version: '2.0'
93
93
  required_rubygems_version: !ruby/object:Gem::Requirement
94
94
  requirements:
95
95
  - - ">="
@@ -97,17 +97,17 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
97
  version: '0'
98
98
  requirements: []
99
99
  rubyforge_project:
100
- rubygems_version: 2.2.2
100
+ rubygems_version: 2.4.1
101
101
  signing_key:
102
102
  specification_version: 4
103
103
  summary: Stream objects over IO using Marshal, JSON, YAML, or Msgpack
104
104
  test_files:
105
- - test/test-maxbuf.rb
106
- - test/test-outbox.rb
107
105
  - test/test-basic.rb
108
106
  - test/test-consume.rb
109
- - test/test-symbolize-keys.rb
107
+ - test/test-slow-sender.rb
110
108
  - test/test-inbox.rb
109
+ - test/test-symbolize-keys.rb
111
110
  - test/test-expect.rb
112
- - test/test-slow-sender.rb
111
+ - test/test-outbox.rb
112
+ - test/test-maxbuf.rb
113
113
  has_rdoc: