object-stream 0.4 → 0.5

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: 5dc85fe1bb49ec72198087e251a3d2f82236944b
4
- data.tar.gz: d7b30be75c5360a6916b6d4d7108c4c20971532a
3
+ metadata.gz: 2012223a66102b4befa32c0aaecbc6559f46d089
4
+ data.tar.gz: 348120adfb1bdf1a62b439fe3f4a78a3887fce0c
5
5
  SHA512:
6
- metadata.gz: 243bb0063699a7419ff4f21982351c212702e68181650886efd5f513f54d77d35d8656d54777a1c75b599983a60192d3411187bc05a3639ddeb52d0b8c8328a4
7
- data.tar.gz: bbec7c970e8f0e0840185b52a8f051d9c12be78327b4b77aeb7fbd71a6bec16fac471cf9bff78ab84ccbad5991e443aa545abda637fe6948bce87e064884092b
6
+ metadata.gz: 03ea4758680c9209c65d178e938dc4025bbf37f09e73fd7a70955053aa09352edcf8c600b921cd5869735e4b0ce57227bd5202747fec618408786dd467f1ece0
7
+ data.tar.gz: 5afa9b0995c5f93e4055608202ba4a86551eb071c8415102a152cab179834b407c6e69310548a3e2421c0b99c07f6e71ae4bd932e226cf22bfde5a6dfcfd44c1
@@ -0,0 +1,38 @@
1
+ # The marshal and yaml serializers preserve the difference between strings and
2
+ # symbols, but msgpack and json cannot: the formats only allow strings. However,
3
+ # sometimes we prefer to write programs that work with symbols even though the
4
+ # serialized data contain strings. On the write end of a stream, that is already
5
+ # possible: symbols are converted to strings as they are written.
6
+ #
7
+ # The symbolize keys feature solves the problem on the read end of a stream. It
8
+ # configures msgpack and json (yajl) to de-serialize strings as symbols. This
9
+ # can be more efficient, since it generates fewer string objects. It's also
10
+ # efficient and convenient to avoid conversions when connecting to other
11
+ # libraries that expect symbols, such as the Sequel database interface.
12
+ #
13
+ # Keep in mind that Ruby, as of version 2.1, does not garbage collect symbols
14
+ # (though this may be coming in 2.2). So this feature can lead to unbounded
15
+ # memory use as new, different symbols keep arriving. Only use it if (a) the set
16
+ # of symbols is bounded (and small), or (b) the program has a short lifespan, or
17
+ # (c) you can monitor the program's memory use and restart it as needed.
18
+ #
19
+ # It's safe for one end of a stream to use this option even if the other end
20
+ # does not: the stream itself is not affected, only the interface changes.
21
+
22
+ type = ARGV.shift || "msgpack"
23
+
24
+ case type
25
+ when "marshal", "yaml", "json", "msgpack"
26
+ else
27
+ abort "Usage: #$0 marshal|yaml|json|msgpack"
28
+ end
29
+
30
+ require 'object-stream'
31
+ require 'stringio'
32
+
33
+ sio = StringIO.new
34
+ stream = ObjectStream.new(sio, type: type, symbolize_keys: true)
35
+ stream << {x: 1, y: 2, z: 3}
36
+
37
+ sio.rewind
38
+ p stream.read
@@ -10,7 +10,7 @@
10
10
  module ObjectStream
11
11
  include Enumerable
12
12
 
13
- VERSION = "0.4"
13
+ VERSION = "0.5"
14
14
 
15
15
  # The IO through which the stream reads and writes serialized object data.
16
16
  attr_reader :io
@@ -247,9 +247,10 @@ module ObjectStream
247
247
 
248
248
  DEFAULT_CHUNK_SIZE = 2000
249
249
 
250
- def initialize io, chunk_size: DEFAULT_CHUNK_SIZE
250
+ # See the discussion in examples/symbolize-keys.rb.
251
+ def initialize io, chunk_size: DEFAULT_CHUNK_SIZE, symbolize_keys: false
251
252
  super
252
- @parser = Yajl::Parser.new
253
+ @parser = Yajl::Parser.new(symbolize_keys: symbolize_keys)
253
254
  @encoder = Yajl::Encoder.new
254
255
  @chunk_size = chunk_size
255
256
  end
@@ -280,9 +281,11 @@ module ObjectStream
280
281
  DEFAULT_CHUNK_SIZE = 2000
281
282
  DEFAULT_MAXBUF = 4000
282
283
 
283
- def initialize io, chunk_size: DEFAULT_CHUNK_SIZE, maxbuf: DEFAULT_MAXBUF
284
+ # See the discussion in examples/symbolize-keys.rb.
285
+ def initialize io, chunk_size: DEFAULT_CHUNK_SIZE, maxbuf: DEFAULT_MAXBUF,
286
+ symbolize_keys: false
284
287
  super
285
- @unpacker = MessagePack::Unpacker.new
288
+ @unpacker = MessagePack::Unpacker.new(symbolize_keys: symbolize_keys)
286
289
  # don't specify io, so don't have to read all of io in one loop
287
290
 
288
291
  @packer = MessagePack::Packer.new(io)
@@ -0,0 +1,56 @@
1
+ require 'object-stream'
2
+ require 'stringio'
3
+
4
+ require 'minitest/autorun'
5
+
6
+ module TestSymbolizeKeys
7
+ attr_reader :sio, :stream
8
+
9
+ BASIC_OBJECTS = [
10
+ nil,
11
+ true,
12
+ false,
13
+ "The quick brown fox jumped over the lazy dog's back.",
14
+ [-5, "foo", [4]],
15
+ {a: 1, b: 2},
16
+ {top: [ {middle: [bottom: "turtle"]} ] }
17
+ ]
18
+
19
+ def type; self.class::TYPE; end
20
+ def objects; BASIC_OBJECTS + self.class::OBJECTS; end
21
+
22
+ def setup
23
+ @sio = StringIO.new
24
+ @stream = ObjectStream.new sio, type: type, symbolize_keys: true
25
+ end
26
+
27
+ def test_write_read
28
+ objects.each do |obj|
29
+ sio.rewind # do not need to clear stream's buffer (if any)
30
+ sio.truncate 0
31
+ stream.write obj
32
+
33
+ sio.rewind
34
+ dump = sio.read
35
+ sio.rewind
36
+
37
+ stream.read do |obj2|
38
+ assert_equal(obj, obj2, "dump is #{dump.inspect}")
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ class TestSymbolizeKeysJson < Minitest::Test
45
+ include TestSymbolizeKeys
46
+
47
+ TYPE = ObjectStream::JSON_TYPE
48
+ OBJECTS = []
49
+ end
50
+
51
+ class TestSymbolizeKeysMsgpack < Minitest::Test
52
+ include TestSymbolizeKeys
53
+
54
+ TYPE = ObjectStream::MSGPACK_TYPE
55
+ OBJECTS = []
56
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: object-stream
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.4'
4
+ version: '0.5'
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-01-30 00:00:00.000000000 Z
11
+ date: 2014-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: msgpack
@@ -58,6 +58,7 @@ files:
58
58
  - examples/read-without-block.rb
59
59
  - examples/slow-sender.rb
60
60
  - examples/socket.rb
61
+ - examples/symbolize-keys.rb
61
62
  - examples/udp.rb
62
63
  - lib/object-stream-wrapper.rb
63
64
  - lib/object-stream.rb
@@ -68,6 +69,7 @@ files:
68
69
  - test/test-maxbuf.rb
69
70
  - test/test-outbox.rb
70
71
  - test/test-slow-sender.rb
72
+ - test/test-symbolize-keys.rb
71
73
  homepage: https://github.com/vjoel/object-stream
72
74
  licenses:
73
75
  - BSD
@@ -95,16 +97,17 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
97
  version: '0'
96
98
  requirements: []
97
99
  rubyforge_project:
98
- rubygems_version: 2.2.1
100
+ rubygems_version: 2.2.2
99
101
  signing_key:
100
102
  specification_version: 4
101
103
  summary: Stream objects over IO using Marshal, JSON, YAML, or Msgpack
102
104
  test_files:
103
- - test/test-basic.rb
105
+ - test/test-maxbuf.rb
104
106
  - test/test-outbox.rb
107
+ - test/test-basic.rb
105
108
  - test/test-consume.rb
106
- - test/test-maxbuf.rb
109
+ - test/test-symbolize-keys.rb
107
110
  - test/test-inbox.rb
108
- - test/test-slow-sender.rb
109
111
  - test/test-expect.rb
112
+ - test/test-slow-sender.rb
110
113
  has_rdoc: