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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b7871c4e08b4d4cdadd1a2282403f76d02c41682
4
- data.tar.gz: 907e4f9fd8f6683ee95c16837a3758b9ab525772
3
+ metadata.gz: 8aa9bb432c7fae05ac9e7f790545b1f50868bba1
4
+ data.tar.gz: d1abe3cf1d8a52ca3a13002ec89e520831cc8ed6
5
5
  SHA512:
6
- metadata.gz: f69694b7ece927b37a851a5d88a96d4170325a45e6c69240707a46c1dfd5bc967314a7ba8ad3993ea3d6576e078d91a8851b10f3811698b033b59e0841866d60
7
- data.tar.gz: 7f6d85a802d9483abc354ccfd5d25b565d4a7affa0a7075e00f6a394af50716f60eaaedcae280b3583f11c0f9115384e16c57ad5d49d44bd07d6c8be4a837493
6
+ metadata.gz: 207674ca4d193805cab007b1ec73303d3ec3701186f3471245cd86f366fd6d967b25ad44982d02fcc5e0b44e1257e7bac93501db953de5acc1ae2047b5ca079a
7
+ data.tar.gz: 136aca0f48b4d564370bfe6674758ea8e67d2c32b29c2cc9d922e240d70cf9ccd4400ca590a2aed8eaa234a7d9a8bcd318a49cbcc1686b514d38bfb485391750
@@ -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.any? do |field|
28
- regex = /^#{field.name}[=#{self.class.matchable?(field) ? '\\?' : ''}]?$/
29
- name.to_s =~ regex
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
- is_setter_method = name =~ /=$/
38
- return method_missing_setter(name, *args) if is_setter_method
44
+ if last_char == '='
45
+ return method_missing_set(name, *args)
46
+ end
39
47
 
40
- is_query_method = name =~ /\?$/
41
- return method_missing_query(name, *args) if is_query_method
48
+ if last_char == '?'
49
+ return method_missing_query(name, *args)
50
+ end
42
51
 
43
- field = descriptor.detect{|field| field.name.to_sym == name}
44
- return method_missing_field(field, *args) if field
52
+ field = descriptor.lookup(name)
53
+ if field
54
+ return method_missing_field(field, *args)
55
+ end
45
56
 
46
- oneof_descriptor = descriptor.lookup_oneof(name.to_s)
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
- return method_missing_oneof(oneof_descriptor) if oneof_descriptor
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.detect{|field| field.name == field_name.to_s}
103
- if !field
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(oneof_descriptor)
239
- oneof_field_name = message.send(oneof_descriptor.name)
240
- return if oneof_field_name.nil?
241
- oneof_field_name = oneof_field_name.to_s
242
- oneof_field = oneof_descriptor.detect {|field| field.name == oneof_field_name}
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
- if field
248
- raise ArgumentError unless args.length == 0
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.detect do |field|
255
- self.class.matchable?(field) && :"#{field.name}?" == name
256
- end
257
- if args.length == 1
258
- # this is an enum query, e.g. `state?(:CREATED)`
259
- matches? field, args[0]
260
- elsif args.length == 0
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 ArgumentError
270
+ raise NoMethodError
265
271
  end
266
272
  end
267
273
 
268
- def method_missing_setter(name, *args)
269
- field = message.class.descriptor.detect{|field| :"#{field.name}=" == name}
270
- if field
271
- raise ArgumentError unless args.length == 1
272
- attributes = {}.tap { |hash| hash[field.name] = args[0] }
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
@@ -2,5 +2,6 @@ require 'minitest/autorun'
2
2
  require 'mocha/mini_test'
3
3
  require 'webmock/minitest'
4
4
  require 'minitest/stub/const'
5
+ require 'minitest/debugger' if ENV['DEBUG']
5
6
 
6
7
  require 'minitest/pride'
@@ -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
- foo, bar = 'foo', 'bar'
130
- resource.oneof_string1 = foo
131
- assert_equal resource.oneof_string1, resource.oneof_group
132
- resource.oneof_string2 = bar
133
- assert_equal resource.oneof_string2, resource.oneof_group
134
- resource.oneof_string2 = bar
135
- resource.oneof_string1 = foo
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?).at_least_once.with(inner_message_class).returns(false)
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
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-23 00:00:00.000000000 Z
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