Skip to content

Commit c70b605

Browse files
committed
add size alias for length
starting to make `RepeatedField` quack like an array additional changes: * make sure gemspec gets all ruby code files * add homepage in gem spec removes one of the warnings, and the gem spec authors are pushing everyone to include a homepage in the gem * remove excess whitespace in test suite to bring formatting inline with the rest of the file
1 parent c881159 commit c70b605

File tree

5 files changed

+57
-3
lines changed

5 files changed

+57
-3
lines changed

ruby/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ lib/google/protobuf_java.jar
55
protobuf-jruby.iml
66
target/
77
pkg/
8+
tmp/

ruby/google-protobuf.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ Gem::Specification.new do |s|
44
s.licenses = ["BSD"]
55
s.summary = "Protocol Buffers"
66
s.description = "Protocol Buffers are Google's data interchange format."
7+
s.homepage = "https://developers.google.com/protocol-buffers"
78
s.authors = ["Protobuf Authors"]
89
s.email = "[email protected]"
910
s.require_paths = ["lib"]
10-
s.files = ["lib/google/protobuf.rb"]
11+
s.files = `git ls-files -z`.split("\x0").find_all{|f| f =~ /lib\/.+\.rb/}
1112
unless RUBY_PLATFORM == "java"
1213
s.files += `git ls-files "*.c" "*.h" extconf.rb Makefile`.split
1314
s.extensions= ["ext/google/protobuf_c/extconf.rb"]

ruby/lib/google/protobuf.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@
3434
else
3535
require 'google/protobuf_c'
3636
end
37+
38+
require 'google/protobuf/repeated_field'
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Protocol Buffers - Google's data interchange format
2+
# Copyright 2008 Google Inc. All rights reserved.
3+
# https://developers.google.com/protocol-buffers/
4+
#
5+
# Redistribution and use in source and binary forms, with or without
6+
# modification, are permitted provided that the following conditions are
7+
# met:
8+
#
9+
# * Redistributions of source code must retain the above copyright
10+
# notice, this list of conditions and the following disclaimer.
11+
# * Redistributions in binary form must reproduce the above
12+
# copyright notice, this list of conditions and the following disclaimer
13+
# in the documentation and/or other materials provided with the
14+
# distribution.
15+
# * Neither the name of Google Inc. nor the names of its
16+
# contributors may be used to endorse or promote products derived from
17+
# this software without specific prior written permission.
18+
#
19+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
31+
# add syntatic sugar on top of the core library
32+
module Google
33+
module Protobuf
34+
class RepeatedField
35+
36+
alias_method :size, :length
37+
38+
end
39+
end
40+
end

ruby/tests/basic.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,18 @@ def test_rptfield_initialize
377377
end
378378
end
379379

380+
def test_rptfield_array_ducktyping
381+
l = Google::Protobuf::RepeatedField.new(:int32)
382+
length_methods = %w(count length size)
383+
length_methods.each do |lm|
384+
assert l.send(lm) == 0
385+
end
386+
l.push 4
387+
length_methods.each do |lm|
388+
assert l.send(lm) == 1
389+
end
390+
end
391+
380392
def test_map_basic
381393
# allowed key types:
382394
# :int32, :int64, :uint32, :uint64, :bool, :string, :bytes.
@@ -827,7 +839,6 @@ def test_bad_field_names
827839
assert m['a.b'] == 4
828840
end
829841

830-
831842
def test_int_ranges
832843
m = TestMessage.new
833844

@@ -933,7 +944,6 @@ def test_int_ranges
933944
assert_raise RangeError do
934945
m.optional_uint64 = 1.5
935946
end
936-
937947
end
938948

939949
def test_stress_test

0 commit comments

Comments
 (0)