primalize 0.2.0 → 0.2.1
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/Gemfile.lock +1 -1
- data/README.md +36 -4
- data/lib/primalize/single.rb +9 -0
- data/lib/primalize/version.rb +1 -1
- 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: f69cd6e3d6c2859c98e3b06bc321cad0bec7bb3f
|
4
|
+
data.tar.gz: c5a9ced0a23efc1a5e48a5a4d69e044d164e1413
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d35467a882797f1b5601493a04359b1f3fd0daf8de59784d51ff18bdb561dce5409e80527bfa935c98535fadf89e44bfd1b75db0ba5753e656e9d1cd3850918c
|
7
|
+
data.tar.gz: 740af8f5e9a39a3f64329389c169898c7771df48b018ac586d44b29459f3d02feeed898540539b25d6f2f5d8782ba470d30613887335a4ddbf845ea7e53fa9a5
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -79,7 +79,37 @@ Or install it yourself as:
|
|
79
79
|
|
80
80
|
## Usage
|
81
81
|
|
82
|
-
If you need to primalize a single object, you subclass `Primalize::Single` and specify the attributes and types of the result as in the example above.
|
82
|
+
If you need to primalize a single object, you subclass `Primalize::Single` and specify the attributes and types of the result as in the example above.
|
83
|
+
|
84
|
+
### Supported types
|
85
|
+
|
86
|
+
The complete list of supported types are:
|
87
|
+
|
88
|
+
- `integer`: whole numbers
|
89
|
+
- `float`: floating-point numbers
|
90
|
+
- `number`: any numeric value
|
91
|
+
- `string`: text
|
92
|
+
- `boolean`: explicitly `true` or `false` (not "truthy" or "falsy" values)
|
93
|
+
- `array(*types)`: an array containing values of the specified types
|
94
|
+
- Example: `array(string, integer)`
|
95
|
+
- `optional(*types)`: any of the specified types or `nil`
|
96
|
+
- Example: `optional(string)`, both `"foo"` and `nil` are acceptable values
|
97
|
+
- `enum(*values)`: must be one of the specified values
|
98
|
+
- Example: `enum('requested', 'shipped', 'delivered')`
|
99
|
+
- `timestamp`: a `Date`, `Time`, or `DateTime` value
|
100
|
+
- `any(*types)`: any value of the given types
|
101
|
+
- Example: `any(string, integer)` will only match on strings and integers
|
102
|
+
- If no types are specified, any value will match
|
103
|
+
- `primalize(YourPrimalizerClass)`: primalizes the specified attribute with the given `Primalize::Single` subclass
|
104
|
+
- Example: `primalize(OrderSerializer)`
|
105
|
+
- `object(**types)`: a hash of the specified structure
|
106
|
+
- Example: `object(id: integer, name: string)`
|
107
|
+
- Only the required keys need to be specified. The rest of the hash will pass.
|
108
|
+
- If no keys are specified, all of them are optional and it will match any hash.
|
109
|
+
|
110
|
+
### Attribute coercion
|
111
|
+
|
112
|
+
Reducing the object's attributes to a hash isn't all you do in most apps. You may also need to do some coercion. For example, if you have an object whose `city` isn't stored as a string but you need to translate it to one:
|
83
113
|
|
84
114
|
```ruby
|
85
115
|
class ShipmentSerializer < Primalize::Single
|
@@ -90,6 +120,8 @@ class ShipmentSerializer < Primalize::Single
|
|
90
120
|
end
|
91
121
|
```
|
92
122
|
|
123
|
+
### Virtual attributes
|
124
|
+
|
93
125
|
You can also generate attributes that don't exist on the object being primalized by defining methods on the primalizer:
|
94
126
|
|
95
127
|
```ruby
|
@@ -117,9 +149,9 @@ end
|
|
117
149
|
By default, subclasses of `Primalize::Single` will raise an `ArgumentError` if there is a mismatch between the types declared in its `attributes` call and what is passed in to be primalized. In production, you might not want that to happen, so you can change that in your production config:
|
118
150
|
|
119
151
|
```ruby
|
120
|
-
Primalize::Single.type_mismatch_handler = proc do |attr, type, value|
|
121
|
-
msg = "Type mismatch: #{attr} is expected to be #{type.inspect}, but is a #{value.inspect}
|
122
|
-
|
152
|
+
Primalize::Single.type_mismatch_handler = proc do |primalizer, attr, type, value|
|
153
|
+
msg = "Type mismatch: #{primalizer.name}##{attr} is expected to be #{type.inspect}, but is a #{value.inspect}\n"
|
154
|
+
msg << caller.grep(Regexp.new(Rails.root)).join("\n") # Include application stack trace
|
123
155
|
|
124
156
|
Slack.notify '#bugs', msg
|
125
157
|
end
|
data/lib/primalize/single.rb
CHANGED
@@ -174,6 +174,10 @@ module Primalize
|
|
174
174
|
class Number
|
175
175
|
include Type
|
176
176
|
|
177
|
+
def initialize &coercion
|
178
|
+
@coercion = coercion
|
179
|
+
end
|
180
|
+
|
177
181
|
def === value
|
178
182
|
::Numeric === value
|
179
183
|
end
|
@@ -266,6 +270,10 @@ module Primalize
|
|
266
270
|
|
267
271
|
TYPES = [Time, Date, DateTime].freeze
|
268
272
|
|
273
|
+
def initialize &coercion
|
274
|
+
@coercion = coercion
|
275
|
+
end
|
276
|
+
|
269
277
|
def === value
|
270
278
|
TYPES.any? { |type| type === value }
|
271
279
|
end
|
@@ -321,6 +329,7 @@ module Primalize
|
|
321
329
|
|
322
330
|
def initialize types, &coercion
|
323
331
|
@types = types
|
332
|
+
@coercion = coercion
|
324
333
|
end
|
325
334
|
|
326
335
|
def === value
|
data/lib/primalize/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: primalize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jamie Gaskins
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-09-
|
11
|
+
date: 2017-09-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|