beefcake 0.3.7 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0f4e5e1cfae47692b962d140248d78882d2c7f5f
4
+ data.tar.gz: 5022dd602b952ccb3bfd27e73f2932a4032e2507
5
+ SHA512:
6
+ metadata.gz: b68af37acfc3a3ec1c18222781ff3281f0fff1e5310cb18eda7748d1f58d696970f92815ada66c8e9b242b187a07393b6be3e3d7932f5cf65649bb80b97154aa
7
+ data.tar.gz: ced3ff677e2234915b09add7fda412cee0c2afc90fd440d75e79bf73300c9260c4122140520f59d538229bda358207459bf57f5ef5cd7085a3494b6b8dde791b
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  *.gem
2
+ Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - jruby-18mode
8
+ - jruby-19mode
data/README.md CHANGED
@@ -3,64 +3,90 @@
3
3
 
4
4
  # Install
5
5
 
6
- $ gem install beefcake
6
+ ```shell
7
+ $ gem install beefcake
8
+ ```
7
9
 
8
10
  # Example
9
11
 
10
- require 'beefcake'
12
+ ```ruby
13
+ require 'beefcake'
11
14
 
12
- class Variety
13
- include Beefcake::Message
15
+ class Variety
16
+ include Beefcake::Message
14
17
 
15
- # Required
16
- required :x, :int32, 1
17
- required :y, :int32, 2
18
+ # Required
19
+ required :x, :int32, 1
20
+ required :y, :int32, 2
18
21
 
19
- # Optional
20
- optional :tag, :string, 3
22
+ # Optional
23
+ optional :tag, :string, 3
21
24
 
22
- # Repeated
23
- repeated :ary, :fixed64, 4
24
- repeated :pary, :fixed64, 5, :packed => true
25
+ # Repeated
26
+ repeated :ary, :fixed64, 4
27
+ repeated :pary, :fixed64, 5, :packed => true
25
28
 
26
- # Enums - Simply use a Module (NOTE: defaults are optional)
27
- module Foonum
28
- A = 1
29
- B = 2
30
- end
29
+ # Enums - Simply use a Module (NOTE: defaults are optional)
30
+ module Foonum
31
+ A = 1
32
+ B = 2
33
+ end
31
34
 
32
- # As per the spec, defaults are only set at the end
33
- # of decoding a message, not on object creation.
34
- optional :foo, Foonum, 6, :default => Foonum::B
35
- end
35
+ # As per the spec, defaults are only set at the end
36
+ # of decoding a message, not on object creation.
37
+ optional :foo, Foonum, 6, :default => Foonum::B
38
+ end
36
39
 
37
- x = Variety.new :x => 1, :y => 2
38
- # or
39
- x = Variety.new
40
- x.x = 1
41
- x.y = 2
40
+ # You can create a new message with hash arguments:
41
+ x = Variety.new :x => 1, :y => 2
42
+
43
+ # You can set fields individually using accessor methods:
44
+ x = Variety.new
45
+ x.x = 1
46
+ x.y = 2
47
+
48
+ # And you can access fields using Hash syntax:
49
+ x[:x] # => 1
50
+ x[:y] = 4
51
+ x # => <Variety x: 1, y: 4>
52
+ ```
42
53
 
43
54
  ## Encoding
44
55
 
45
56
  Any object responding to `<<` can accept encoding
46
57
 
47
- s = ""
48
- x.encode(s)
49
- p [:s, s]
50
- # or (because encode returns the string/stream)
51
- p [:s, x.encode]
52
- # or
53
- open("x.dat") do |f|
54
- x.encode(f)
55
- end
58
+ ```ruby
59
+ x # => <Variety x: 1, y: 2>
60
+
61
+ # For example, you can encode into a String:
62
+ s = ""
63
+ x.encode(s)
64
+ s # => "\b\x01\x10\x02)\0"
65
+
66
+ # If you don't encode into anything, a new Beefcake::Buffer will be returned:
67
+ x.encode # => #<Beefcake::Buffer:0x007fbfe1867ab0 @buf="\b\x01\x10\x02)\0">
68
+
69
+ # And that buffer can be converted to a String:
70
+ x.encode.to_s # => "\b\x01\x10\x02)\0"
71
+ ```
56
72
 
57
- # decode
58
- encoded = x.encode
59
- decoded = Variety.decode(encoded)
60
- p [:x, decoded]
73
+ ### Decoding
61
74
 
62
- # decode merge
63
- Variety.decoded(more_data, decoded)
75
+ ```ruby
76
+ x # => <Variety x: 1, y: 2>
77
+
78
+ # You can decode from a Beefcake::Buffer
79
+ encoded = x.encode
80
+ Variety.decode(encoded) # => <Variety x: 1, y: 2, pary: [], foo: B(2)>
81
+
82
+ # Decoding from a String works the same way:
83
+ Variety.decode(encoded.to_s) # => <Variety x: 1, y: 2, pary: [], foo: B(2)>
84
+
85
+ # You can update a Beefcake::Message instance with new data too:
86
+ new_data = Variety.new(x: 12345, y: 2).encode
87
+ Variety.decoded(new_data, x)
88
+ x # => <Variety x: 12345, y: 2, pary: [], foo: B(2)>
89
+ ```
64
90
 
65
91
  # Why?
66
92
 
@@ -82,13 +108,17 @@ desired namespace. (i.e. App::Foo::Bar)
82
108
 
83
109
  Source:
84
110
 
85
- $ git clone git://github.com/bmizerany/beefcake
111
+ $ git clone https://github.com/protobuf-ruby/beefcake.git
86
112
 
87
113
  ## Testing:
88
114
 
89
- $ gem install turn
90
- $ cd /path/to/beefcake
91
- $ turn
115
+ $ rake test
116
+
117
+ Beefcake conducts continuous integration on [Travis CI](http://travis-ci.org).
118
+ The current build status for HEAD is [![Build Status](https://travis-ci.org/protobuf-ruby/beefcake.png)](https://travis-ci.org/protobuf-ruby/beefcake).
119
+
120
+ All pull requests automatically trigger a build request. Please ensure that
121
+ tests succeed.
92
122
 
93
123
  ## VMs:
94
124
 
@@ -97,6 +127,7 @@ Currently Beefcake is tested and working on:
97
127
  * Ruby 1.8.6
98
128
  * Ruby 1.8.7
99
129
  * Ruby 1.9.2
130
+ * Ruby 2.0.0
100
131
  * JRuby 1.5.6
101
132
  * Rubinius edge
102
133
 
@@ -110,11 +141,12 @@ Currently Beefcake is tested and working on:
110
141
  * Varint fields
111
142
  * 32-bit fields
112
143
  * 64-bit fields
113
- * Length delemited fields
114
- * Embeded Messages
144
+ * Length-delimited fields
145
+ * Embedded Messages
115
146
  * Unknown fields are ignored (as per spec)
116
147
  * Enums
117
148
  * Defaults (i.e. `optional :foo, :string, :default => "bar"`)
149
+ * Varint-encoded length-delimited message streams
118
150
 
119
151
 
120
152
  ## Future
data/RELEASE_NOTES.md ADDED
@@ -0,0 +1,15 @@
1
+ # Beefcake Release Notes
2
+
3
+ # 0.4.0 - 2013-10-10
4
+
5
+ Release 0.4.0 is the first with new maintainers.
6
+
7
+ * Modernize tests
8
+ * Add Travis CI monitoring
9
+ * Support varint-encoded length-delimited buffers
10
+ * Support generation with recursive definitions
11
+ * Support Ruby 2.0
12
+ * Support encoded buffers
13
+ * Support false but non-nil values
14
+ * Support top-level enums, added by Kim Altintop:
15
+ https://github.com/protobuf-ruby/beefcake/pull/23
data/Rakefile CHANGED
@@ -1,2 +1,12 @@
1
- require 'bundler'
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'bundler/gem_tasks'
2
4
  Bundler::GemHelper.install_tasks
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'test'
8
+ t.test_files = FileList['test/*_test.rb']
9
+ t.verbose = true
10
+ end
11
+
12
+ task :default => :test
data/beefcake.gemspec CHANGED
@@ -6,11 +6,12 @@ Gem::Specification.new do |s|
6
6
  s.name = "beefcake"
7
7
  s.version = Beefcake::VERSION
8
8
  s.platform = Gem::Platform::RUBY
9
- s.authors = ["Blake Mizerany"]
10
- s.email = ["blake.mizerany@gmail.com"]
11
- s.homepage = ""
9
+ s.authors = ["Blake Mizerany", "Matt Proud", "Bryce Kerley"]
10
+ s.email = ["blake.mizerany@gmail.com", "matt.proud@gmail.com", "bkerley@brycekerley.net"]
11
+ s.homepage = "https://github.com/protobuf-ruby/beefcake"
12
12
  s.summary = %q{A sane protobuf library for Ruby}
13
13
  s.description = %q{A sane protobuf library for Ruby}
14
+ s.license = 'MIT'
14
15
 
15
16
  s.rubyforge_project = "beefcake"
16
17
 
@@ -18,4 +19,6 @@ Gem::Specification.new do |s|
18
19
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
20
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
21
  s.require_paths = ["lib"]
22
+
23
+ s.add_development_dependency('rake', '10.1.0')
21
24
  end
data/lib/beefcake.rb CHANGED
@@ -106,6 +106,20 @@ module Beefcake
106
106
  buf
107
107
  end
108
108
 
109
+ def write_delimited(buf = Buffer.new)
110
+ if ! buf.respond_to?(:<<)
111
+ raise ArgumentError, "buf doesn't respond to `<<`"
112
+ end
113
+
114
+ if ! buf.is_a?(Buffer)
115
+ buf = Buffer.new(buf)
116
+ end
117
+
118
+ buf.append_bytes(encode)
119
+
120
+ buf
121
+ end
122
+
109
123
  def valid_enum?(mod, val)
110
124
  !!name_for(mod, val)
111
125
  end
@@ -180,6 +194,17 @@ module Beefcake
180
194
 
181
195
  o
182
196
  end
197
+
198
+ def read_delimited(buf, o=self.new)
199
+ if ! buf.is_a?(Buffer)
200
+ buf = Buffer.new(buf)
201
+ end
202
+
203
+ n = buf.read_int64
204
+ tmp = Buffer.new(buf.read(n))
205
+
206
+ decode(tmp, o)
207
+ end
183
208
  end
184
209
 
185
210
 
@@ -234,8 +259,9 @@ module Beefcake
234
259
 
235
260
  def to_hash
236
261
  fields.values.inject({}) do |h, fld|
237
- if v = self[fld.name]
238
- h[fld.name] = v
262
+ value = self[fld.name]
263
+ unless value.nil?
264
+ h[fld.name] = value
239
265
  end
240
266
  h
241
267
  end
@@ -77,6 +77,7 @@ module Beefcake
77
77
  end
78
78
 
79
79
  def <<(bytes)
80
+ bytes = bytes.force_encoding('BINARY') if bytes.respond_to? :force_encoding
80
81
  buf << bytes
81
82
  end
82
83
 
@@ -105,8 +105,14 @@ module Beefcake
105
105
  end
106
106
 
107
107
  def append_string(s)
108
- append_uint64(s.length)
109
- self << s
108
+ actual_string = s.to_s
109
+ if actual_string.respond_to? :force_encoding
110
+ encoded = actual_string.force_encoding 'binary'
111
+ else
112
+ encoded = actual_string
113
+ end
114
+ append_uint64(encoded.length)
115
+ self << encoded
110
116
  end
111
117
  alias :append_bytes :append_string
112
118
 
@@ -109,7 +109,8 @@ class CodeGeneratorRequest
109
109
  optional :name, :string, 1 # file name, relative to root of source tree
110
110
  optional :package, :string, 2 # e.g. "foo", "foo.bar", etc.
111
111
 
112
- repeated :message_type, DescriptorProto, 4;
112
+ repeated :message_type, DescriptorProto, 4;
113
+ repeated :enum_type, EnumDescriptorProto, 5;
113
114
  end
114
115
 
115
116
 
@@ -161,14 +162,6 @@ module Beefcake
161
162
  @n = 0
162
163
  end
163
164
 
164
- def file!(file)
165
- puts "## Generated from #{file.name} for #{file.package}"
166
-
167
- file.message_type.each do |mt|
168
- message!("", mt)
169
- end
170
- end
171
-
172
165
  def indent(&blk)
173
166
  @n += 1
174
167
  blk.call
@@ -179,35 +172,48 @@ module Beefcake
179
172
  @n = n
180
173
  end
181
174
 
182
- def message!(pkg, mt)
175
+ def define!(mt)
183
176
  puts
184
177
  puts "class #{mt.name}"
185
178
 
186
179
  indent do
187
180
  puts "include Beefcake::Message"
188
- puts
189
181
 
182
+ ## Enum Types
190
183
  Array(mt.enum_type).each do |et|
191
184
  enum!(et)
192
185
  end
193
186
 
187
+ ## Nested Types
188
+ Array(mt.nested_type).each do |nt|
189
+ define!(nt)
190
+ end
191
+ end
192
+ puts "end"
193
+ end
194
+
195
+ def message!(pkg, mt)
196
+ puts
197
+ puts "class #{mt.name}"
198
+
199
+ indent do
194
200
  ## Generate Types
195
201
  Array(mt.nested_type).each do |nt|
196
202
  message!(pkg, nt)
197
203
  end
198
- puts
199
204
 
200
- ## Generate fields
205
+ ## Generate Fields
201
206
  Array(mt.field).each do |f|
202
207
  field!(pkg, f)
203
208
  end
204
- puts
205
209
  end
206
210
 
207
211
  puts "end"
212
+ puts
208
213
  end
209
214
 
210
215
  def enum!(et)
216
+ puts
211
217
  puts "module #{et.name}"
212
218
  indent do
213
219
  et.value.each do |v|
@@ -229,7 +235,9 @@ module Beefcake
229
235
  # We have a type_name so we will use it after converting to a
230
236
  # Ruby friendly version
231
237
  t = f.type_name
232
- t = t.gsub(pkg, "") # Remove the leading package name
238
+ if pkg
239
+ t = t.gsub(pkg, "") # Remove the leading package name
240
+ end
233
241
  t = t.gsub(/^\.*/, "") # Remove leading `.`s
234
242
 
235
243
  t.gsub(".", "::") # Convert to Ruby namespacing syntax
@@ -267,6 +275,14 @@ module Beefcake
267
275
  puts
268
276
 
269
277
  ns!(ns) do
278
+ Array(file.enum_type).each do |et|
279
+ enum!(et)
280
+ end
281
+
282
+ file.message_type.each do |mt|
283
+ define! mt
284
+ end
285
+
270
286
  file.message_type.each do |mt|
271
287
  message!(file.package, mt)
272
288
  end
@@ -1,3 +1,3 @@
1
1
  module Beefcake
2
- VERSION = "0.3.7"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -1,3 +1,4 @@
1
+ require 'test/unit'
1
2
  require 'beefcake/buffer'
2
3
 
3
4
  class BufferDecodeTest < Test::Unit::TestCase
@@ -1,3 +1,5 @@
1
+ # encoding: ASCII-8BIT
2
+ require 'test/unit'
1
3
  require 'beefcake/buffer/encode'
2
4
 
3
5
  class BufferEncodeTest < Test::Unit::TestCase
data/test/buffer_test.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'test/unit'
1
2
  require 'beefcake/buffer'
2
3
 
3
4
  class BufferTest < Test::Unit::TestCase
@@ -1,3 +1,4 @@
1
+ require 'test/unit'
1
2
  require 'beefcake/generator'
2
3
 
3
4
  class GeneratorTest < Test::Unit::TestCase
data/test/message_test.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'test/unit'
1
2
  require 'beefcake'
2
3
 
3
4
  class NumericsMessage
@@ -165,6 +166,20 @@ class MessageTest < Test::Unit::TestCase
165
166
  assert_equal "\b{", str
166
167
  end
167
168
 
169
+ def test_delimited_end_to_end
170
+ msg = SimpleMessage.new :a => 123, :b => "hi mom!"
171
+ str = ""
172
+
173
+ 1000.times do
174
+ msg.write_delimited(str)
175
+ end
176
+
177
+ 1000.times do
178
+ dec = SimpleMessage.read_delimited(str)
179
+ assert_equal msg, dec
180
+ end
181
+ end
182
+
168
183
  def test_encode_enum
169
184
  buf = Beefcake::Buffer.new
170
185
  buf.append(:int32, 2, 1)
@@ -375,4 +390,14 @@ class MessageTest < Test::Unit::TestCase
375
390
  assert_equal(exp, msg.to_hash)
376
391
  end
377
392
 
393
+ def test_bool_to_hash
394
+ true_message = BoolMessage.new :bool => true
395
+ true_expectation = { :bool => true }
396
+ assert_equal true_expectation, true_message.to_hash
397
+
398
+ false_message = BoolMessage.new :bool => false
399
+ false_expectation = { :bool => false }
400
+ assert_equal false_expectation, false_message.to_hash
401
+ end
402
+
378
403
  end
metadata CHANGED
@@ -1,38 +1,47 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: beefcake
3
- version: !ruby/object:Gem::Version
4
- hash: 29
5
- prerelease:
6
- segments:
7
- - 0
8
- - 3
9
- - 7
10
- version: 0.3.7
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Blake Mizerany
8
+ - Matt Proud
9
+ - Bryce Kerley
14
10
  autorequire:
15
11
  bindir: bin
16
12
  cert_chain: []
17
-
18
- date: 2011-09-29 00:00:00 -07:00
19
- default_executable:
20
- dependencies: []
21
-
13
+ date: 2013-10-10 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 10.1.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - '='
27
+ - !ruby/object:Gem::Version
28
+ version: 10.1.0
22
29
  description: A sane protobuf library for Ruby
23
- email:
30
+ email:
24
31
  - blake.mizerany@gmail.com
25
- executables:
32
+ - matt.proud@gmail.com
33
+ - bkerley@brycekerley.net
34
+ executables:
26
35
  - protoc-gen-beefcake
27
36
  extensions: []
28
-
29
37
  extra_rdoc_files: []
30
-
31
- files:
38
+ files:
32
39
  - .gitignore
40
+ - .travis.yml
33
41
  - Gemfile
34
42
  - LICENSE
35
43
  - README.md
44
+ - RELEASE_NOTES.md
36
45
  - Rakefile
37
46
  - beefcake.gemspec
38
47
  - bench/simple.rb
@@ -50,41 +59,31 @@ files:
50
59
  - test/buffer_test.rb
51
60
  - test/generator_test.rb
52
61
  - test/message_test.rb
53
- has_rdoc: true
54
- homepage: ""
55
- licenses: []
56
-
62
+ homepage: https://github.com/protobuf-ruby/beefcake
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
57
66
  post_install_message:
58
67
  rdoc_options: []
59
-
60
- require_paths:
68
+ require_paths:
61
69
  - lib
62
- required_ruby_version: !ruby/object:Gem::Requirement
63
- none: false
64
- requirements:
65
- - - ">="
66
- - !ruby/object:Gem::Version
67
- hash: 3
68
- segments:
69
- - 0
70
- version: "0"
71
- required_rubygems_version: !ruby/object:Gem::Requirement
72
- none: false
73
- requirements:
74
- - - ">="
75
- - !ruby/object:Gem::Version
76
- hash: 3
77
- segments:
78
- - 0
79
- version: "0"
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
80
  requirements: []
81
-
82
81
  rubyforge_project: beefcake
83
- rubygems_version: 1.5.2
82
+ rubygems_version: 2.1.4
84
83
  signing_key:
85
- specification_version: 3
84
+ specification_version: 4
86
85
  summary: A sane protobuf library for Ruby
87
- test_files:
86
+ test_files:
88
87
  - test/buffer_decode_test.rb
89
88
  - test/buffer_encode_test.rb
90
89
  - test/buffer_test.rb