protip 0.18.4 → 0.18.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/protip/wrapper.rb +47 -44
- data/test/test_helper.rb +1 -0
- data/test/unit/protip/resource_test.rb +7 -8
- data/test/unit/protip/wrapper_test.rb +18 -3
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8aa9bb432c7fae05ac9e7f790545b1f50868bba1
|
4
|
+
data.tar.gz: d1abe3cf1d8a52ca3a13002ec89e520831cc8ed6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 207674ca4d193805cab007b1ec73303d3ec3701186f3471245cd86f366fd6d967b25ad44982d02fcc5e0b44e1257e7bac93501db953de5acc1ae2047b5ca079a
|
7
|
+
data.tar.gz: 136aca0f48b4d564370bfe6674758ea8e67d2c32b29c2cc9d922e240d70cf9ccd4400ca590a2aed8eaa234a7d9a8bcd318a49cbcc1686b514d38bfb485391750
|
data/lib/protip/wrapper.rb
CHANGED
@@ -18,34 +18,47 @@ module Protip
|
|
18
18
|
|
19
19
|
def respond_to?(name)
|
20
20
|
if super
|
21
|
-
true
|
21
|
+
return true
|
22
22
|
else
|
23
23
|
# Responds to calls to oneof groups by name
|
24
24
|
return true if message.class.descriptor.lookup_oneof(name.to_s)
|
25
25
|
|
26
26
|
# Responds to field getters, setters, and in the scalar enum case, query methods
|
27
|
-
message.class.descriptor.
|
28
|
-
|
29
|
-
|
27
|
+
field = message.class.descriptor.lookup(name.to_s.gsub(/[=?]$/, ''))
|
28
|
+
return false if !field
|
29
|
+
if name[-1, 1] == '?'
|
30
|
+
# For query methods, only respond if the field is matchable
|
31
|
+
return self.class.matchable?(field)
|
32
|
+
else
|
33
|
+
return true
|
30
34
|
end
|
31
35
|
end
|
36
|
+
false
|
32
37
|
end
|
33
38
|
|
34
39
|
def method_missing(name, *args)
|
35
40
|
descriptor = message.class.descriptor
|
41
|
+
name = name.to_s
|
42
|
+
last_char = name[-1, 1]
|
36
43
|
|
37
|
-
|
38
|
-
|
44
|
+
if last_char == '='
|
45
|
+
return method_missing_set(name, *args)
|
46
|
+
end
|
39
47
|
|
40
|
-
|
41
|
-
|
48
|
+
if last_char == '?'
|
49
|
+
return method_missing_query(name, *args)
|
50
|
+
end
|
42
51
|
|
43
|
-
field = descriptor.
|
44
|
-
|
52
|
+
field = descriptor.lookup(name)
|
53
|
+
if field
|
54
|
+
return method_missing_field(field, *args)
|
55
|
+
end
|
45
56
|
|
46
|
-
|
57
|
+
oneof = descriptor.lookup_oneof(name)
|
47
58
|
# For calls to a oneof group, return the active oneof field, or nil if there isn't one
|
48
|
-
|
59
|
+
if oneof
|
60
|
+
return method_missing_oneof(oneof, *args)
|
61
|
+
end
|
49
62
|
|
50
63
|
super
|
51
64
|
end
|
@@ -99,10 +112,8 @@ module Protip
|
|
99
112
|
# @return [NilClass]
|
100
113
|
def assign_attributes(attributes)
|
101
114
|
attributes.each do |field_name, value|
|
102
|
-
field = message.class.descriptor.
|
103
|
-
|
104
|
-
raise ArgumentError.new("Unrecognized field: #{field_name}")
|
105
|
-
end
|
115
|
+
field = message.class.descriptor.lookup(field_name.to_s) ||
|
116
|
+
(raise ArgumentError.new("Unrecognized field: #{field_name}"))
|
106
117
|
|
107
118
|
# For inconvertible nested messages, the value should be either a hash or a message
|
108
119
|
if field.type == :message && !converter.convertible?(field.subtype.msgclass)
|
@@ -235,44 +246,36 @@ module Protip
|
|
235
246
|
|
236
247
|
end
|
237
248
|
|
238
|
-
def method_missing_oneof(
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
oneof_field ? get(oneof_field) : nil
|
249
|
+
def method_missing_oneof(oneof, *args)
|
250
|
+
raise ArgumentError unless args.length == 0
|
251
|
+
field_name = message.public_send(oneof.name)
|
252
|
+
|
253
|
+
field_name ? get(message.class.descriptor.lookup(field_name.to_s)) : nil
|
244
254
|
end
|
245
255
|
|
246
256
|
def method_missing_field(field, *args)
|
247
|
-
|
248
|
-
|
249
|
-
get(field)
|
250
|
-
end
|
257
|
+
raise ArgumentError unless args.length == 0
|
258
|
+
get field
|
251
259
|
end
|
252
260
|
|
253
261
|
def method_missing_query(name, *args)
|
254
|
-
field = message.class.descriptor.
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
# this is a boolean query, e.g. `approved?`
|
262
|
-
get field
|
262
|
+
field = message.class.descriptor.lookup(name[0, name.length - 1])
|
263
|
+
raise NoMethodError if !field || !self.class.matchable?(field)
|
264
|
+
if field.type == :enum
|
265
|
+
raise ArgumentError unless args.length == 1
|
266
|
+
return matches?(field, args[0])
|
267
|
+
elsif field.type == :bool ||
|
268
|
+
(field.type == :message && field.subtype.name == 'google.protobuf.BoolValue')
|
263
269
|
else
|
264
|
-
raise
|
270
|
+
raise NoMethodError
|
265
271
|
end
|
266
272
|
end
|
267
273
|
|
268
|
-
def
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
assign_attributes attributes
|
274
|
-
return args[0] # return the input value (to match ActiveRecord behavior)
|
275
|
-
end
|
274
|
+
def method_missing_set(name, *args)
|
275
|
+
raise ArgumentError unless args.length == 1
|
276
|
+
field = message.class.descriptor.lookup(name[0, name.length - 1])
|
277
|
+
raise(NoMethodError.new) unless field
|
278
|
+
set(field, args[0])
|
276
279
|
end
|
277
280
|
end
|
278
281
|
end
|
data/test/test_helper.rb
CHANGED
@@ -126,14 +126,13 @@ module Protip::ResourceTest # Namespace for internal constants
|
|
126
126
|
|
127
127
|
it 'returns the active oneof field when a oneof group accessor is called' do
|
128
128
|
resource = resource_class.new
|
129
|
-
|
130
|
-
resource.
|
131
|
-
|
132
|
-
resource.
|
133
|
-
|
134
|
-
resource.
|
135
|
-
resource.
|
136
|
-
assert_equal resource.oneof_string1, resource.oneof_group
|
129
|
+
resource.oneof_string1 = 'foo'
|
130
|
+
assert_equal 'foo', resource.oneof_group
|
131
|
+
resource.oneof_string2 = 'bar'
|
132
|
+
assert_equal 'bar', resource.oneof_group
|
133
|
+
resource.oneof_string2 = 'bar'
|
134
|
+
resource.oneof_string1 = 'foo'
|
135
|
+
assert_equal 'foo', resource.oneof_group
|
137
136
|
end
|
138
137
|
|
139
138
|
it 'sets fields on the underlying message when simple setters are called' do
|
@@ -404,6 +404,22 @@ module Protip::WrapperTest # namespace for internal constants
|
|
404
404
|
end
|
405
405
|
end
|
406
406
|
|
407
|
+
describe 'attribute reader' do # generated via method_missing?
|
408
|
+
it 'returns the underlying assigned value for oneof fields' do
|
409
|
+
wrapper.oneof_string1 = 'foo'
|
410
|
+
assert_equal 'foo', wrapper.oneof_group
|
411
|
+
wrapper.oneof_string2 = 'bar'
|
412
|
+
assert_equal 'bar', wrapper.oneof_group
|
413
|
+
wrapper.oneof_string2 = 'bar'
|
414
|
+
wrapper.oneof_string1 = 'foo'
|
415
|
+
assert_equal 'foo', wrapper.oneof_group
|
416
|
+
end
|
417
|
+
|
418
|
+
it 'returns nil for oneof fields that have not been set' do
|
419
|
+
assert_nil wrapper.oneof_group
|
420
|
+
end
|
421
|
+
end
|
422
|
+
|
407
423
|
describe 'attribute writer' do # generated via method_missing?
|
408
424
|
|
409
425
|
before do
|
@@ -429,8 +445,8 @@ module Protip::WrapperTest # namespace for internal constants
|
|
429
445
|
assert_equal inner_message_class.new(value: 30), wrapper.message.inner
|
430
446
|
end
|
431
447
|
|
432
|
-
it 'removes message fields when assigning nil' do
|
433
|
-
converter.expects(:convertible?).
|
448
|
+
it 'removes message fields when assigning nil, without checking with the converter' do
|
449
|
+
converter.expects(:convertible?).never
|
434
450
|
converter.expects(:to_message).never
|
435
451
|
|
436
452
|
wrapper.inner = nil
|
@@ -446,7 +462,6 @@ module Protip::WrapperTest # namespace for internal constants
|
|
446
462
|
end
|
447
463
|
|
448
464
|
it 'passes through messages without checking whether they are convertible' do
|
449
|
-
converter.expects(:convertible?).once.returns(true)
|
450
465
|
message = inner_message_class.new(value: 50)
|
451
466
|
|
452
467
|
converter.expects(:convertible?).never
|
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.18.
|
4
|
+
version: 0.18.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- AngelList
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -98,6 +98,20 @@ dependencies:
|
|
98
98
|
- - "~>"
|
99
99
|
- !ruby/object:Gem::Version
|
100
100
|
version: '5.0'
|
101
|
+
- !ruby/object:Gem::Dependency
|
102
|
+
name: minitest-debugger
|
103
|
+
requirement: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - "~>"
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '1.0'
|
108
|
+
type: :development
|
109
|
+
prerelease: false
|
110
|
+
version_requirements: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - "~>"
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '1.0'
|
101
115
|
- !ruby/object:Gem::Dependency
|
102
116
|
name: minitest-stub-const
|
103
117
|
requirement: !ruby/object:Gem::Requirement
|