protip 0.14.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/protip/standard_converter.rb +22 -2
- data/test/unit/protip/standard_converter_test.rb +23 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f2bcfe95ab05331d80a72680b824507b1183e50d
|
4
|
+
data.tar.gz: d5bc2008b39cbc556793156b50d92dc455673860
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d5d2a3761f37f72cb7fef48d87195f286ad3da0ae7cd725c3a04c2fbf57fdff9ccde25f64a8eca5f9c36e85f3088cbd34d080d046ccd72da8ff7f96c47c66477
|
7
|
+
data.tar.gz: 4b9f94b329d0c8f16c5ef2eba2b7a82dfbf5ae1d4bbdaf15c6a26f5cac24370b89d1dc4a5725003f9d22781306e7a8b5d47f396e3886d550a55d38a55b529360
|
@@ -2,11 +2,13 @@ require 'protip/converter'
|
|
2
2
|
|
3
3
|
require 'protip/messages/types'
|
4
4
|
require 'google/protobuf'
|
5
|
-
|
6
5
|
module Protip
|
7
6
|
class StandardConverter
|
8
7
|
include Protip::Converter
|
9
8
|
|
9
|
+
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON']
|
10
|
+
FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF']
|
11
|
+
|
10
12
|
class << self
|
11
13
|
attr_reader :conversions
|
12
14
|
end
|
@@ -22,7 +24,7 @@ module Protip
|
|
22
24
|
}
|
23
25
|
|
24
26
|
## Standard wrappers
|
25
|
-
%w(Int64 Int32 UInt64 UInt32 Double Float
|
27
|
+
%w(Int64 Int32 UInt64 UInt32 Double Float String Bytes).map{|type| "google.protobuf.#{type}Value"}.each do |name|
|
26
28
|
@conversions[name] = {
|
27
29
|
to_object: ->(message) { message.value },
|
28
30
|
to_message: lambda do |value, message_class|
|
@@ -31,6 +33,13 @@ module Protip
|
|
31
33
|
}
|
32
34
|
end
|
33
35
|
|
36
|
+
conversions['google.protobuf.BoolValue'] = {
|
37
|
+
to_object: ->(message) { message.value },
|
38
|
+
to_message: lambda do |value, message_class|
|
39
|
+
message_class.new value: value_to_boolean(value)
|
40
|
+
end
|
41
|
+
}
|
42
|
+
|
34
43
|
def convertible?(message_class)
|
35
44
|
self.class.conversions.has_key?(message_class.descriptor.name)
|
36
45
|
end
|
@@ -42,5 +51,16 @@ module Protip
|
|
42
51
|
def to_message(object, message_class)
|
43
52
|
self.class.conversions[message_class.descriptor.name][:to_message].call(object, message_class)
|
44
53
|
end
|
54
|
+
|
55
|
+
class << self
|
56
|
+
# Similar to Rails 3 value_to_boolean
|
57
|
+
def value_to_boolean(value)
|
58
|
+
return true if TRUE_VALUES.include?(value)
|
59
|
+
return false if FALSE_VALUES.include?(value)
|
60
|
+
# If we don't get a truthy/falsey value, use the original value (which should raise an
|
61
|
+
# exception)
|
62
|
+
value
|
63
|
+
end
|
64
|
+
end
|
45
65
|
end
|
46
66
|
end
|
@@ -83,5 +83,28 @@ describe Protip::StandardConverter do
|
|
83
83
|
assert_equal 7, date.day # Sanity check argument order
|
84
84
|
assert_equal ::Protip::Messages::Date.new(year: 2012, month: 5, day: 7), converter.to_message(date, ::Protip::Messages::Date)
|
85
85
|
end
|
86
|
+
|
87
|
+
it 'converts truthy values to booleans' do
|
88
|
+
[true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON'].each do |truth_value|
|
89
|
+
assert_equal Google::Protobuf::BoolValue.new(value: true),
|
90
|
+
converter.to_message(truth_value, Google::Protobuf::BoolValue)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'converts falsey values to booleans' do
|
95
|
+
[false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF'].each do |false_value|
|
96
|
+
assert_equal Google::Protobuf::BoolValue.new(value: false),
|
97
|
+
converter.to_message(false_value, Google::Protobuf::BoolValue)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'raises an exception if non-boolean values passed to boolean field' do
|
102
|
+
[nil, 'test', Object.new, 2, {}, []].each do |bad_value|
|
103
|
+
assert_raises TypeError do
|
104
|
+
converter.to_message(bad_value, Google::Protobuf::BoolValue)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
86
109
|
end
|
87
110
|
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.
|
4
|
+
version: 0.15.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- AngelList
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -138,6 +138,8 @@ description:
|
|
138
138
|
email:
|
139
139
|
- team@angel.co
|
140
140
|
- k2@angel.co
|
141
|
+
- brian@angel.co
|
142
|
+
- paul@angel.co
|
141
143
|
executables: []
|
142
144
|
extensions: []
|
143
145
|
extra_rdoc_files: []
|