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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a67c63237e5cf69ced4be0ee3a485f7ff3fba588
4
- data.tar.gz: f92d5b559d4a2cbf322187f2f4eef1d17d7967a5
3
+ metadata.gz: b5ef8f748c40fe69ebe9bde7525c18a26227e922
4
+ data.tar.gz: c5dbba13dd9778bc27048e4f78ad80f1a81d8c80
5
5
  SHA512:
6
- metadata.gz: 6fab8fcf295c05a748678d0ed5f4770797f8b83c3d637fe5e70eedb0fd62f405f8703577a4dd2cfb7f45efb515437c8bc7064f6f993c945a2dc80c5d84db519d
7
- data.tar.gz: 311fe81e529e0f948cbefadf8d2efea6cd992897a692468c253579f50ad2606f663c890bad05d22037da850a55d76abf4dcd46ef6a879fc503b2bd527ea3f756
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
  [![Gem Version](https://badge.fury.io/rb/safe_type.svg)](https://badge.fury.io/rb/safe_type)
4
5
  [![Build Status](https://travis-ci.org/chanzuckerberg/safe_type.svg?branch=master)](https://travis-ci.org/chanzuckerberg/safe_type)
5
6
  [![Maintainability](https://api.codeclimate.com/v1/badges/7fbc9a4038b86ef639e1/maintainability)](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 FallSemester < SafeType::Date
52
- # implement validate method
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["https://API_URI"] # => #<ResponseType:0x000056005b3e7518>
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 `[]` method as well.
155
+ To apply the rule on a simple object, we can call the `coerce` method as well.
155
156
  ```ruby
156
- SafeType::Integer.default["1"] # => 1
157
- SafeType::Integer["1"] # => 1
157
+ SafeType::Integer.default.coerce("1") # => 1
158
+ SafeType::Integer.coerce("1") # => 1
158
159
  ```
159
- For the *SafeType* primitive types, apply the rule on the class itself will use the default rule.
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 `validate` to check the value after convert. This method should take the input and return `true` or `false`.
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[input] if rule.is_a?(SafeType::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|
@@ -6,7 +6,7 @@ module SafeType
6
6
  super
7
7
  end
8
8
 
9
- def validate(input)
9
+ def is_valid?(input)
10
10
  return false unless @from.nil? || input >= @from
11
11
  return false unless @to.nil? || input <= @to
12
12
  super
@@ -6,7 +6,7 @@ module SafeType
6
6
  super
7
7
  end
8
8
 
9
- def validate(input)
9
+ def is_valid?(input)
10
10
  return false unless @from.nil? || input >= @from
11
11
  return false unless @to.nil? || input <= @to
12
12
  super
@@ -6,7 +6,7 @@ module SafeType
6
6
  super
7
7
  end
8
8
 
9
- def validate(input)
9
+ def is_valid?(input)
10
10
  return false unless @min.nil? || input >= @min
11
11
  return false unless @max.nil? || input <= @max
12
12
  super
@@ -6,7 +6,7 @@ module SafeType
6
6
  super
7
7
  end
8
8
 
9
- def validate(input)
9
+ def is_valid?(input)
10
10
  return false unless @min.nil? || input >= @min
11
11
  return false unless @max.nil? || input <= @max
12
12
  super
@@ -6,7 +6,7 @@ module SafeType
6
6
  super
7
7
  end
8
8
 
9
- def validate(input)
9
+ def is_valid?(input)
10
10
  return false unless @min_length.nil? || input.length >= @min_length
11
11
  return false unless @max_length.nil? || input.length <= @max_length
12
12
  super
@@ -6,7 +6,7 @@ module SafeType
6
6
  super
7
7
  end
8
8
 
9
- def validate(input)
9
+ def is_valid?(input)
10
10
  return false unless @min_length.nil? || input.length >= @min_length
11
11
  return false unless @max_length.nil? || input.length <= @max_length
12
12
  super
@@ -6,7 +6,7 @@ module SafeType
6
6
  super
7
7
  end
8
8
 
9
- def validate(input)
9
+ def is_valid?(input)
10
10
  return false unless @from.nil? || input >= @from
11
11
  return false unless @to.nil? || input <= @to
12
12
  super
@@ -12,7 +12,7 @@ module SafeType
12
12
  @default = default
13
13
  end
14
14
 
15
- def validate(input)
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.[](input)
32
- default[input]
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 [](input)
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 validate(input)
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
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-06-13 00:00:00.000000000 Z
11
+ date: 2018-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake