sorbet-coerce 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sorbet-coerce/converter.rb +2 -0
- data/spec/coerce_spec.rb +6 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 459ea1a3f906360f8a0e0d33969f49110476a1aa8d1772a1adc4dc1fc704140c
|
4
|
+
data.tar.gz: 7e2bf805fcb1d89de859312576d7e9e4eb782d03e4e7ff5929205c0fb676e377
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c75dfa39c75773bd161fc67f2ee145192bc6431f58db621a39eadd2c02948d39a117f3d1a5c8ad277338cf1ab0342a6f4153f20326bd379e28e8a734ca1d190c
|
7
|
+
data.tar.gz: d9c87f91c2586aac705089fe519d6c58e28854fe8cf87019468f46ad4421e8166d5f042c6f512c983d25b120e4b31f699f755706694a47a29c91b2d38c1a5a78
|
@@ -130,6 +130,8 @@ class TypeCoerce::Converter
|
|
130
130
|
safe_type_rule = SafeType::Boolean.strict
|
131
131
|
elsif value.is_a?(type)
|
132
132
|
return value
|
133
|
+
elsif type == BigDecimal
|
134
|
+
return BigDecimal(value)
|
133
135
|
elsif PRIMITIVE_TYPES.include?(type)
|
134
136
|
safe_type_rule = SafeType.const_get(type.name).strict
|
135
137
|
else
|
data/spec/coerce_spec.rb
CHANGED
@@ -19,6 +19,7 @@ describe TypeCoerce do
|
|
19
19
|
class Param < T::Struct
|
20
20
|
const :id, Integer
|
21
21
|
const :role, String, default: 'wizard'
|
22
|
+
const :price, BigDecimal
|
22
23
|
const :info, ParamInfo
|
23
24
|
const :opt, T.nilable(ParamInfo2)
|
24
25
|
end
|
@@ -65,6 +66,7 @@ describe TypeCoerce do
|
|
65
66
|
let!(:param) {
|
66
67
|
TypeCoerce[Param].new.from({
|
67
68
|
id: 1,
|
69
|
+
price: BigDecimal('98.76'),
|
68
70
|
info: {
|
69
71
|
name: 'mango',
|
70
72
|
lvl: 100,
|
@@ -81,6 +83,7 @@ describe TypeCoerce do
|
|
81
83
|
let!(:param2) {
|
82
84
|
TypeCoerce[Param].new.from({
|
83
85
|
id: '2',
|
86
|
+
price: '98.76',
|
84
87
|
info: {
|
85
88
|
name: 'honeydew',
|
86
89
|
lvl: '5',
|
@@ -97,6 +100,7 @@ describe TypeCoerce do
|
|
97
100
|
it 'reveals the right type' do
|
98
101
|
T.assert_type!(param, Param)
|
99
102
|
T.assert_type!(param.id, Integer)
|
103
|
+
T.assert_type!(param.price, BigDecimal)
|
100
104
|
T.assert_type!(param.info, ParamInfo)
|
101
105
|
T.assert_type!(param.info.name,String)
|
102
106
|
T.assert_type!(param.info.lvl, Integer)
|
@@ -106,6 +110,7 @@ describe TypeCoerce do
|
|
106
110
|
it 'coerces correctly' do
|
107
111
|
expect(param.id).to eql 1
|
108
112
|
expect(param.role).to eql 'wizard'
|
113
|
+
expect(param.price).to eql BigDecimal('98.76')
|
109
114
|
expect(param.info.lvl).to eql 100
|
110
115
|
expect(param.info.name).to eql 'mango'
|
111
116
|
expect(param.info.skill_ids).to eql [123, 456]
|
@@ -113,6 +118,7 @@ describe TypeCoerce do
|
|
113
118
|
expect(TypeCoerce[Param].new.from(param)).to eq(param)
|
114
119
|
|
115
120
|
expect(param2.id).to eql 2
|
121
|
+
expect(param2.price).to eql BigDecimal('98.76')
|
116
122
|
expect(param2.info.name).to eql 'honeydew'
|
117
123
|
expect(param2.info.lvl).to eql 5
|
118
124
|
expect(param2.info.skill_ids).to eql []
|