protip 0.20.3 → 0.20.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/protip/standard_converter.rb +1 -2
- data/lib/protip/wrapper.rb +13 -3
- data/test/unit/protip/wrapper_test.rb +52 -7
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13a134ef5d4a02a13ede10452e95e7971d3a983a
|
4
|
+
data.tar.gz: f00fe632579adf72c55660f62256a8277a847e2c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be84e24f6e21db21420d04a623ce090759bd56f6dd9e28d438f145dafac76944c87761c88e568da2e256e595aa01fafb7a54a9b406b51606415538f34b505fcc
|
7
|
+
data.tar.gz: 18817a7340e471b6b3ba3319bac499711b85baf114cdf86b977b2e1269afe09b8db1aaaebca091dc9c4ba7d7200dde66d029d7139e11e71dfdcac2c27fa98325
|
data/lib/protip/wrapper.rb
CHANGED
@@ -162,11 +162,21 @@ module Protip
|
|
162
162
|
field.type == :bool || (field.type == :message && field.subtype.name == 'google.protobuf.BoolValue')
|
163
163
|
end
|
164
164
|
|
165
|
+
def enum_for_field(field)
|
166
|
+
if field.type == :enum
|
167
|
+
field.subtype
|
168
|
+
elsif field.type == :message && field.subtype.name == 'protip.messages.EnumValue'
|
169
|
+
Protip::StandardConverter.enum_for_field(field)
|
170
|
+
else
|
171
|
+
nil
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
165
175
|
# Semi-private check for whether a field should have an associated query method (e.g. +field_name?+).
|
166
176
|
# @return [Boolean] Whether the field should have an associated query method on wrappers.
|
167
177
|
def matchable?(field)
|
168
178
|
return false if field.label == :repeated
|
169
|
-
|
179
|
+
(nil != enum_for_field(field)) || boolean?(field)
|
170
180
|
end
|
171
181
|
end
|
172
182
|
|
@@ -231,7 +241,7 @@ module Protip
|
|
231
241
|
end
|
232
242
|
|
233
243
|
def matches?(field, value)
|
234
|
-
enum = field
|
244
|
+
enum = Protip::Wrapper.enum_for_field(field)
|
235
245
|
if value.is_a?(Fixnum)
|
236
246
|
sym = enum.lookup_value(value)
|
237
247
|
else
|
@@ -258,7 +268,7 @@ module Protip
|
|
258
268
|
def method_missing_query(name, *args)
|
259
269
|
field = message.class.descriptor.lookup(name[0, name.length - 1])
|
260
270
|
raise NoMethodError if !field || !self.class.matchable?(field)
|
261
|
-
if
|
271
|
+
if nil != Protip::Wrapper.enum_for_field(field)
|
262
272
|
raise ArgumentError unless args.length == 1
|
263
273
|
return matches?(field, args[0])
|
264
274
|
elsif self.class.boolean?(field)
|
@@ -26,6 +26,9 @@ module Protip::WrapperTest # namespace for internal constants
|
|
26
26
|
add_message 'google.protobuf.BoolValue' do
|
27
27
|
optional :value, :bool, 1
|
28
28
|
end
|
29
|
+
add_message 'protip.messages.EnumValue' do
|
30
|
+
optional :value, :int32, 1
|
31
|
+
end
|
29
32
|
|
30
33
|
add_message 'message' do
|
31
34
|
optional :inner, :message, 1, 'inner_message'
|
@@ -38,16 +41,17 @@ module Protip::WrapperTest # namespace for internal constants
|
|
38
41
|
|
39
42
|
optional :number, :enum, 6, 'number'
|
40
43
|
repeated :numbers, :enum, 7, 'number'
|
44
|
+
optional :number_message, :message, 8, 'protip.messages.EnumValue'
|
41
45
|
|
42
|
-
optional :boolean, :bool,
|
43
|
-
repeated :booleans, :bool,
|
46
|
+
optional :boolean, :bool, 9
|
47
|
+
repeated :booleans, :bool, 10
|
44
48
|
|
45
|
-
optional :google_bool_value, :message,
|
46
|
-
repeated :google_bool_values, :message,
|
49
|
+
optional :google_bool_value, :message, 11, 'google.protobuf.BoolValue'
|
50
|
+
repeated :google_bool_values, :message, 12, 'google.protobuf.BoolValue'
|
47
51
|
|
48
52
|
oneof :oneof_group do
|
49
|
-
optional :oneof_string1, :string,
|
50
|
-
optional :oneof_string2, :string,
|
53
|
+
optional :oneof_string1, :string, 13
|
54
|
+
optional :oneof_string2, :string, 14
|
51
55
|
end
|
52
56
|
end
|
53
57
|
end
|
@@ -59,6 +63,9 @@ module Protip::WrapperTest # namespace for internal constants
|
|
59
63
|
pool.lookup(name).msgclass
|
60
64
|
end
|
61
65
|
end
|
66
|
+
let(:enum_message_class) do
|
67
|
+
pool.lookup('protip.messages.EnumValue').msgclass
|
68
|
+
end
|
62
69
|
let (:inner_message_field) { message_class.descriptor.lookup('inner') }
|
63
70
|
|
64
71
|
# Stubbed API client
|
@@ -87,6 +94,13 @@ module Protip::WrapperTest # namespace for internal constants
|
|
87
94
|
Protip::Wrapper.new(wrapped_message, converter)
|
88
95
|
end
|
89
96
|
|
97
|
+
# Stub the wrapped-enum fetcher method - probably rethink this once the
|
98
|
+
# instance variable hack is no longer necessary
|
99
|
+
before do
|
100
|
+
Protip::StandardConverter.stubs(:enum_for_field)
|
101
|
+
.returns(pool.lookup('number') || raise('unexpected - no enum field found'))
|
102
|
+
end
|
103
|
+
|
90
104
|
describe '#respond_to?' do
|
91
105
|
it 'adds setters for message fields' do
|
92
106
|
assert_respond_to wrapper, :string=
|
@@ -103,6 +117,7 @@ module Protip::WrapperTest # namespace for internal constants
|
|
103
117
|
end
|
104
118
|
it 'adds queries for scalar matchable fields' do
|
105
119
|
assert_respond_to wrapper, :number?, 'enum field should respond to query'
|
120
|
+
assert_respond_to wrapper, :number_message?, 'enum message field should respond to query'
|
106
121
|
assert_respond_to wrapper, :boolean?, 'bool field should respond to query'
|
107
122
|
assert_respond_to wrapper, :google_bool_value?, 'google.protobuf.BoolValue field should respond to query'
|
108
123
|
end
|
@@ -346,7 +361,7 @@ module Protip::WrapperTest # namespace for internal constants
|
|
346
361
|
end
|
347
362
|
|
348
363
|
it 'contains keys for all fields of the parent message' do
|
349
|
-
keys = %i(string strings inner inners inner_blank number numbers boolean booleans
|
364
|
+
keys = %i(string strings inner inners inner_blank number numbers number_message boolean booleans
|
350
365
|
google_bool_value google_bool_values oneof_string1 oneof_string2)
|
351
366
|
assert_equal keys.sort, wrapper.to_h.keys.sort
|
352
367
|
end
|
@@ -596,6 +611,36 @@ module Protip::WrapperTest # namespace for internal constants
|
|
596
611
|
end
|
597
612
|
end
|
598
613
|
end
|
614
|
+
|
615
|
+
describe 'for a wrapped enum' do
|
616
|
+
let :enum_message do
|
617
|
+
enum_message_class.new value: 1
|
618
|
+
end
|
619
|
+
|
620
|
+
before do
|
621
|
+
converter.stubs(:convertible?).with(enum_message_class).returns true
|
622
|
+
converter.stubs(:to_object).with(enum_message, anything).returns :ONE
|
623
|
+
wrapper.message.number_message = enum_message
|
624
|
+
end
|
625
|
+
|
626
|
+
it 'returns true when its symbolized argument matches the value' do
|
627
|
+
m = mock
|
628
|
+
m.expects(:to_sym).returns :ONE
|
629
|
+
assert wrapper.number_message?(m)
|
630
|
+
end
|
631
|
+
|
632
|
+
it 'returns false when its symbolized argument does not match the value' do
|
633
|
+
m = mock
|
634
|
+
m.expects(:to_sym).returns :TWO
|
635
|
+
refute wrapper.number_message?(m)
|
636
|
+
end
|
637
|
+
|
638
|
+
it 'returns true when a Fixnum argument matches the value' do
|
639
|
+
assert wrapper.number_message?(1)
|
640
|
+
end
|
641
|
+
|
642
|
+
end
|
643
|
+
|
599
644
|
end
|
600
645
|
end
|
601
646
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: protip
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.20.
|
4
|
+
version: 0.20.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- AngelList
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|