primalize 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e36f2c4e228676c3b667327dd6ac82cb5a35d6eb
4
- data.tar.gz: f3787f3ea335597dc61dd7fe3b461c9e7076f3db
3
+ metadata.gz: 3e7e19ac6dc1dae6887c09d7c6a3388a29532895
4
+ data.tar.gz: 497da52eccc3c157d0ca413b8cf5ce132a2e5e4d
5
5
  SHA512:
6
- metadata.gz: feac9b17d4d74fc748c9e1289b9173ec8782fc3b8a31e2a859c3d5c2281e03deddf02acfb173860c8eab36c0aac23a1e7966dd795b8c0cb50339baac7d1f8214
7
- data.tar.gz: b04714ce166db915086c6da5905ea69e568780015ce7234d02821128c89bb9893c97407f05e4ed89e0dbe13f50f1ee4971a0d68ddf99ca9e7ae14cb46c287ba4
6
+ metadata.gz: 29b4efb1d23fd3ccc909b133bd8e18c25e46e770bb0104a5b5e7fab414ebc8545d77b7f7b142513ee15f1823ea3158301f5221c1ab015ec05221e8bba777d567
7
+ data.tar.gz: 62a1dd9738ccacbd45b723a1b56e360db4a73fd7e3cb4837c78a5de519c785585ff1d14a29c2e00a1d29743f42e1e740efb6e638efa7d4872fd5b92d47e518f1
@@ -208,8 +208,6 @@ module Primalize
208
208
  end
209
209
 
210
210
  class Array
211
- include Type
212
-
213
211
  def initialize types, &coercion
214
212
  @types = types
215
213
  @coercion = coercion
@@ -222,6 +220,25 @@ module Primalize
222
220
  end
223
221
  end
224
222
 
223
+ def coerce array
224
+ if @coercion
225
+ return @coercion.call(array)
226
+ end
227
+
228
+ if array.respond_to? :map
229
+ array.map do |item|
230
+ type = @types.find { |type| type === item }
231
+ if type.respond_to? :coerce
232
+ type.coerce(item)
233
+ else
234
+ item
235
+ end
236
+ end
237
+ else
238
+ array
239
+ end
240
+ end
241
+
225
242
  def inspect
226
243
  "array(#{@types.map(&:inspect).join(', ')})"
227
244
  end
@@ -249,8 +266,6 @@ module Primalize
249
266
  end
250
267
 
251
268
  class Object
252
- include Type
253
-
254
269
  def initialize types, &coercion
255
270
  @types = types
256
271
  @coercion = coercion
@@ -264,6 +279,26 @@ module Primalize
264
279
  end
265
280
  end
266
281
 
282
+ def coerce hash
283
+ if @coercion
284
+ return @coercion.call(hash)
285
+ end
286
+
287
+ if hash.respond_to? :map
288
+ hash.each_with_object({}) do |(key, value), h|
289
+ _, type = @types.find { |_, type| type === value }
290
+
291
+ h[key] = if type.respond_to? :coerce
292
+ type.coerce(value)
293
+ else
294
+ value
295
+ end
296
+ end
297
+ else
298
+ hash
299
+ end
300
+ end
301
+
267
302
  def inspect
268
303
  "object(#{@types.map { |attr, type| "#{attr}: #{type.inspect}" }.join(', ')})"
269
304
  end
@@ -295,7 +330,8 @@ module Primalize
295
330
  @coercion = proc do |obj|
296
331
  # FIXME: this is dumb
297
332
  begin
298
- primalizer.new((coercion || DEFAULT_COERCION).call(obj)).call
333
+ coerced = (coercion || DEFAULT_COERCION).call(obj)
334
+ primalizer.new(coerced).call
299
335
  rescue ArgumentError => e
300
336
  raise TypeError.new(e)
301
337
  end
@@ -329,8 +365,6 @@ module Primalize
329
365
  end
330
366
 
331
367
  class Any
332
- include Type
333
-
334
368
  def initialize types, &coercion
335
369
  @types = types
336
370
  @coercion = coercion
@@ -340,6 +374,19 @@ module Primalize
340
374
  @types.empty? || @types.any? { |type| type === value }
341
375
  end
342
376
 
377
+ def coerce value
378
+ if @coercion
379
+ return @coercion.call(value)
380
+ end
381
+
382
+ type = @types.find { |type| type === value }
383
+ if type.respond_to? :coerce
384
+ type.coerce(value)
385
+ else
386
+ value
387
+ end
388
+ end
389
+
343
390
  def inspect
344
391
  params = "(#{@types.map(&:inspect).join(', ')})"
345
392
  "any#{@types.empty? ? nil : params}"
@@ -1,3 +1,3 @@
1
1
  module Primalize
2
- VERSION = "0.3.2"
2
+ VERSION = "0.3.3"
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.3.2
4
+ version: 0.3.3
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-12-28 00:00:00.000000000 Z
11
+ date: 2018-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler