safe_type 0.0.4 → 1.0.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/README.md +15 -9
- data/lib/safe_type.rb +1 -1
- data/lib/safe_type/primitive/date.rb +1 -1
- data/lib/safe_type/primitive/date_time.rb +1 -1
- data/lib/safe_type/primitive/float.rb +1 -1
- data/lib/safe_type/primitive/integer.rb +1 -1
- data/lib/safe_type/primitive/string.rb +1 -1
- data/lib/safe_type/primitive/symbol.rb +1 -1
- data/lib/safe_type/primitive/time.rb +1 -1
- data/lib/safe_type/rule.rb +5 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b5ef8f748c40fe69ebe9bde7525c18a26227e922
|
4
|
+
data.tar.gz: c5dbba13dd9778bc27048e4f78ad80f1a81d8c80
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 18b50e3f3ffdd15d9fb98242ddd44e32777c9f7add2a9c3539177c5226e2934092a98832f284107fa9168a06ba84fcc899a05dd4c8147beb5850857c140816f3
|
7
|
+
data.tar.gz: b4a87e49171f26eff48bda4bee778780fd67c5b3e45e199ddf6f4d5fbd1189db5405ae8fccb52a512cf55ec2a7007dbb18a3b0b0bed030462c2ee97e413dbb52
|
data/README.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
+
---
|
1
2
|
SafeType
|
2
|
-
|
3
|
+
---
|
3
4
|
[](https://badge.fury.io/rb/safe_type)
|
4
5
|
[](https://travis-ci.org/chanzuckerberg/safe_type)
|
5
6
|
[](https://codeclimate.com/github/chanzuckerberg/safe_type/maintainability)
|
@@ -48,8 +49,8 @@ SAFE_ENV[:BUILD_NUM] # => 123
|
|
48
49
|
```
|
49
50
|
## Routing Parameters
|
50
51
|
```ruby
|
51
|
-
class
|
52
|
-
# implement
|
52
|
+
class FallSemesterStartDate < SafeType::Date
|
53
|
+
# implement `is_valid?` method
|
53
54
|
end
|
54
55
|
|
55
56
|
current_year = Date.today.year
|
@@ -113,7 +114,7 @@ class Response < SafeType::Rule
|
|
113
114
|
end
|
114
115
|
end
|
115
116
|
|
116
|
-
Response
|
117
|
+
Response.coerce("https://API_URI") # => #<ResponseType:0x000056005b3e7518>
|
117
118
|
```
|
118
119
|
|
119
120
|
# Overview
|
@@ -151,19 +152,24 @@ Rules can be bundled together as elements in an array or values in a hash.
|
|
151
152
|
- `SafeType::coerce!` coerces the object in place. The unspecified fields will not be modified.
|
152
153
|
Note `SafeType::coerce!` cannot be used on a simple object, otherwise it will raise `SafeType::InvalidRuleError`.
|
153
154
|
|
154
|
-
To apply the rule on a simple object, we can call `
|
155
|
+
To apply the rule on a simple object, we can call the `coerce` method as well.
|
155
156
|
```ruby
|
156
|
-
SafeType::Integer.default
|
157
|
-
SafeType::Integer
|
157
|
+
SafeType::Integer.default.coerce("1") # => 1
|
158
|
+
SafeType::Integer.coerce("1") # => 1
|
158
159
|
```
|
159
|
-
|
160
|
+
Note those two examples are equivalent:
|
161
|
+
```ruby
|
162
|
+
SafeType::coerce(ENV["PORT"], SafeType::Integer.default(3000))
|
163
|
+
SafeType::Integer.default(3000).coerce(ENV["PORT"])
|
164
|
+
```
|
165
|
+
For the *SafeType* primitive types, applying the rule on the class itself will use the default rule.
|
160
166
|
|
161
167
|
## Customized Types
|
162
168
|
We can inherit from a `SafeType::Rule` to create a customized type.
|
163
169
|
We can override following methods if needed:
|
164
170
|
- Override `initialize` to change the default values, types, or add more attributes.
|
165
171
|
- Override `before` to update the input before convert. This method should take the input and return it after processing.
|
166
|
-
- Override `
|
172
|
+
- Override `is_valid?` to check the value after convert. This method should take the input and return `true` or `false`.
|
167
173
|
- Override `after` to update the input after validate. This method should take the input and return it after processing.
|
168
174
|
- Override `handle_exceptions` to change the behavior of exceptions handling (e.g: send to the logger, or no exception)
|
169
175
|
- Override `default` or `strict` to modify the default and strict rule.
|
data/lib/safe_type.rb
CHANGED
@@ -13,7 +13,7 @@ require 'safe_type/primitive/time'
|
|
13
13
|
module SafeType
|
14
14
|
class << self
|
15
15
|
def coerce(input, rule)
|
16
|
-
return rule
|
16
|
+
return rule.coerce(input) if rule.is_a?(SafeType::Rule)
|
17
17
|
if rule.class == ::Hash
|
18
18
|
result = {}
|
19
19
|
rule.each do |key, val|
|
data/lib/safe_type/rule.rb
CHANGED
@@ -12,7 +12,7 @@ module SafeType
|
|
12
12
|
@default = default
|
13
13
|
end
|
14
14
|
|
15
|
-
def
|
15
|
+
def is_valid?(input)
|
16
16
|
true
|
17
17
|
end
|
18
18
|
|
@@ -28,8 +28,8 @@ module SafeType
|
|
28
28
|
raise SafeType::CoercionError
|
29
29
|
end
|
30
30
|
|
31
|
-
def self.
|
32
|
-
default
|
31
|
+
def self.coerce(input)
|
32
|
+
default.coerce(input)
|
33
33
|
end
|
34
34
|
|
35
35
|
def self.default
|
@@ -40,11 +40,11 @@ module SafeType
|
|
40
40
|
new(required: true)
|
41
41
|
end
|
42
42
|
|
43
|
-
def
|
43
|
+
def coerce(input)
|
44
44
|
raise SafeType::EmptyValueError if input.nil? && @required
|
45
45
|
input = before(input)
|
46
46
|
input = Converter.to_type(input, @type)
|
47
|
-
raise SafeType::ValidationError unless
|
47
|
+
raise SafeType::ValidationError unless is_valid?(input)
|
48
48
|
result = after(input)
|
49
49
|
raise SafeType::EmptyValueError if result.nil? && @required
|
50
50
|
return @default if result.nil?
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: safe_type
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Donald Dong
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-07-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|