protip 0.18.6 → 0.18.7

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: a9c25de6d9f409c5bb8c1659951ecc4fa10a5aa4
4
- data.tar.gz: aab7d4182c10f0a540dd5762ea3921dac899c2b1
3
+ metadata.gz: 87a1c6e1b9c82896ae06af88f2e9b4bf27a7050a
4
+ data.tar.gz: aa8ee22934fca9dd14964a91efeea888572e3513
5
5
  SHA512:
6
- metadata.gz: 530e2c08bf013f25afea7d4be6eb254b85b192fb8ca374ceb09ef48ce3e9e703926ca07335bac5396edcacc129f29710cb2cb9e1b619508210ba39c8170335f9
7
- data.tar.gz: d65a08e07c0365afadfdf614f731bcf5939deed2e463f4e3d4159f70b0691e1d9735567f33dd1edcf0d7c893f919a128f01e0e634aceec33c4761422aa502e81
6
+ metadata.gz: 1994203036d8aaafcb5d36ee5708d97a88f2c2b16772a4b2c969515884d24a2fc7ec1b4ad00ac09bee83c18ee07c32b5514b70ac9496cee54255e166da33562d
7
+ data.tar.gz: 654378f70d1bf78adc4a02a246e425f9644347a098c43767e0d3910b31b3bf4218036fdf15f70edbe35ff05d6f4968c995c7474b4944f7a5e44843831c7ec85c
@@ -156,14 +156,17 @@ module Protip
156
156
  end
157
157
 
158
158
  class << self
159
+ # Whether the given field returns boolean values. When true, wrappers will respond to +field_name?+
160
+ # query methods.
161
+ def boolean?(field)
162
+ field.type == :bool || (field.type == :message && field.subtype.name == 'google.protobuf.BoolValue')
163
+ end
164
+
159
165
  # Semi-private check for whether a field should have an associated query method (e.g. +field_name?+).
160
166
  # @return [Boolean] Whether the field should have an associated query method on wrappers.
161
167
  def matchable?(field)
162
168
  return false if field.label == :repeated
163
-
164
- field.type == :enum ||
165
- field.type == :bool ||
166
- field.type == :message && (field.subtype.name == "google.protobuf.BoolValue")
169
+ field.type == :enum || boolean?(field)
167
170
  end
168
171
  end
169
172
 
@@ -258,8 +261,8 @@ module Protip
258
261
  if field.type == :enum
259
262
  raise ArgumentError unless args.length == 1
260
263
  return matches?(field, args[0])
261
- elsif field.type == :bool ||
262
- (field.type == :message && field.subtype.name == 'google.protobuf.BoolValue')
264
+ elsif self.class.boolean?(field)
265
+ !!get(field)
263
266
  else
264
267
  raise NoMethodError
265
268
  end
@@ -272,4 +275,4 @@ module Protip
272
275
  set(field, args[0])
273
276
  end
274
277
  end
275
- end
278
+ end
@@ -18,7 +18,7 @@ module Protip::ResourceTest # Namespace for internal constants
18
18
  add_message 'nested_message' do
19
19
  optional :number, :int64, 1
20
20
  end
21
- add_message "google.protobuf.BoolValue" do
21
+ add_message 'google.protobuf.BoolValue' do
22
22
  optional :value, :bool, 1
23
23
  end
24
24
  add_message 'resource_message' do
@@ -30,8 +30,8 @@ module Protip::ResourceTest # Namespace for internal constants
30
30
  repeated :numbers, :enum, 6, 'number'
31
31
  optional :boolean, :bool, 8
32
32
  repeated :booleans, :bool, 9
33
- optional :google_bool_value, :message, 10, "google.protobuf.BoolValue"
34
- repeated :google_bool_values, :message, 11, "google.protobuf.BoolValue"
33
+ optional :google_bool_value, :message, 10, 'google.protobuf.BoolValue'
34
+ repeated :google_bool_values, :message, 11, 'google.protobuf.BoolValue'
35
35
 
36
36
  oneof :oneof_group do
37
37
  optional :oneof_string1, :string, 12
@@ -177,13 +177,14 @@ module Protip::ResourceTest # Namespace for internal constants
177
177
  end
178
178
 
179
179
  it 'defines query methods for the booleans on its message' do
180
+ resource.boolean = true
180
181
  assert_respond_to resource, :boolean?
181
- refute resource.boolean?
182
+ assert_equal true, resource.boolean?
182
183
  end
183
184
 
184
185
  it 'defines query methods for the google.protobuf.BoolValues on its message' do
185
186
  assert_respond_to resource, :google_bool_value?
186
- refute resource.google_bool_value?
187
+ assert_equal false, resource.google_bool_value?
187
188
  end
188
189
 
189
190
  it 'does not define query methods for repeated enums' do
@@ -23,7 +23,7 @@ module Protip::WrapperTest # namespace for internal constants
23
23
  optional :value, :int64, 1
24
24
  optional :note, :string, 2
25
25
  end
26
- add_message "google.protobuf.BoolValue" do
26
+ add_message 'google.protobuf.BoolValue' do
27
27
  optional :value, :bool, 1
28
28
  end
29
29
 
@@ -42,8 +42,8 @@ module Protip::WrapperTest # namespace for internal constants
42
42
  optional :boolean, :bool, 8
43
43
  repeated :booleans, :bool, 9
44
44
 
45
- optional :google_bool_value, :message, 10, "google.protobuf.BoolValue"
46
- repeated :google_bool_values, :message, 11, "google.protobuf.BoolValue"
45
+ optional :google_bool_value, :message, 10, 'google.protobuf.BoolValue'
46
+ repeated :google_bool_values, :message, 11, 'google.protobuf.BoolValue'
47
47
 
48
48
  oneof :oneof_group do
49
49
  optional :oneof_string1, :string, 12
@@ -105,6 +105,12 @@ module Protip::WrapperTest # namespace for internal constants
105
105
  assert_respond_to wrapper, :boolean?, 'bool field should respond to query'
106
106
  assert_respond_to wrapper, :google_bool_value?, 'google.protobuf.BoolValue field should respond to query'
107
107
  end
108
+ it 'allows .boolean? to be overwritten to add additional queryable fields' do
109
+ wrapper.class.stubs(:boolean?).returns(true)
110
+ assert_respond_to wrapper, :string?
111
+ wrapper.class.stubs(:boolean?).returns(false)
112
+ refute_respond_to wrapper, :string?
113
+ end
108
114
  it 'does not add queries for repeated fields' do
109
115
  refute_respond_to wrapper, :numbers?, 'repeated enum field should not respond to query'
110
116
  refute_respond_to wrapper, :booleans?, 'repeated bool field should not respond to query'
@@ -522,6 +528,21 @@ module Protip::WrapperTest # namespace for internal constants
522
528
  end
523
529
  end
524
530
 
531
+ describe 'boolean query methods' do
532
+ it 'returns the boolean-ized version of the field' do
533
+ wrapper.class.stubs(:boolean?).returns(true)
534
+
535
+ # Sanity checks
536
+ raise 'unexpected string' unless wrapper.string == 'test'
537
+ raise 'unexpected boolean' unless wrapper.boolean == false
538
+ raise 'unexpected google_bool_value' unless wrapper.google_bool_value == nil
539
+
540
+ assert_equal true, wrapper.string?, 'did not convert string to boolean'
541
+ assert_equal false, wrapper.boolean?, 'did not pass through boolean value'
542
+ assert_equal false, wrapper.google_bool_value?, 'did not convert nil to boolean'
543
+ end
544
+ end
545
+
525
546
  describe '#matches?' do
526
547
  it 'is not defined for non-enum fields' do
527
548
  assert_raises NoMethodError do
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.6
4
+ version: 0.18.7
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-26 00:00:00.000000000 Z
11
+ date: 2016-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel