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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 211551a4a7fc66702f544d35f053e4e9f65d1fe1
4
- data.tar.gz: b6f715a19f9b32fae1b4c025ae195ed1643faf65
3
+ metadata.gz: f69cd6e3d6c2859c98e3b06bc321cad0bec7bb3f
4
+ data.tar.gz: c5a9ced0a23efc1a5e48a5a4d69e044d164e1413
5
5
  SHA512:
6
- metadata.gz: 1557c13cf41e2924453083053dd5de8b08b258cbdca01a2f55cf9d72f2f1a6ab048bb94fe564467346daea9292f8a436fd932db6dca4fc989d4b1a8aebf601ab
7
- data.tar.gz: 133145cfb6bca06af9fd920eca885aa16707ca2af8b49e7c4c059112d65052232af87ab6a7e6049d701497c143e15008a22ae8be8fe5a6b1e18e756518e63a2e
6
+ metadata.gz: d35467a882797f1b5601493a04359b1f3fd0daf8de59784d51ff18bdb561dce5409e80527bfa935c98535fadf89e44bfd1b75db0ba5753e656e9d1cd3850918c
7
+ data.tar.gz: 740af8f5e9a39a3f64329389c169898c7771df48b018ac586d44b29459f3d02feeed898540539b25d6f2f5d8782ba470d30613887335a4ddbf845ea7e53fa9a5
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- primalize (0.2.0)
4
+ primalize (0.2.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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. But 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:
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
- caller.grep(Regexp.new(Rails.root))
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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Primalize
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
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.0
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-09 00:00:00.000000000 Z
11
+ date: 2017-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler