primalize 0.2.1 → 0.3.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: f69cd6e3d6c2859c98e3b06bc321cad0bec7bb3f
4
- data.tar.gz: c5a9ced0a23efc1a5e48a5a4d69e044d164e1413
3
+ metadata.gz: 448491f1045de5e13c32b1654908a81aef3a4088
4
+ data.tar.gz: 0fabc3d5579b65702bf1b697a515ff5c7c01e41a
5
5
  SHA512:
6
- metadata.gz: d35467a882797f1b5601493a04359b1f3fd0daf8de59784d51ff18bdb561dce5409e80527bfa935c98535fadf89e44bfd1b75db0ba5753e656e9d1cd3850918c
7
- data.tar.gz: 740af8f5e9a39a3f64329389c169898c7771df48b018ac586d44b29459f3d02feeed898540539b25d6f2f5d8782ba470d30613887335a4ddbf845ea7e53fa9a5
6
+ metadata.gz: e00310261eb2d595fbab0242da634142a8ef3e31a62bcca8ddeb1b5cc71021cad5cf697a8e386ce5ad62e64d7223a12cd5ef825b5db9b6897581ff3f26735bb1
7
+ data.tar.gz: 03ed8baf130751558ae80948c6562fb0f61b2906edc9869a3f16168b88abf7f21ae6c32a541aeb246b8a3960fc8fa827ff6f44a71252472a50bdb4c4482c9eec
@@ -35,6 +35,25 @@ module Primalize
35
35
  end
36
36
  end
37
37
 
38
+ def self.optional serializer_class
39
+ Class.new(Optional) do
40
+ define_method :initialize do |object|
41
+ return if object.nil?
42
+
43
+ @object = object
44
+ @serializer = serializer_class.new(object)
45
+ end
46
+ end
47
+ end
48
+
49
+ class Optional
50
+ def call
51
+ return nil if @object.nil?
52
+
53
+ @serializer.call
54
+ end
55
+ end
56
+
38
57
  class Enumerable
39
58
  def initialize enumerable
40
59
  validate! enumerable
@@ -69,7 +88,14 @@ module Primalize
69
88
  end
70
89
 
71
90
  def validate_attributes! attributes
72
- unless self.class.attributes.each_key.all? { |key| attributes[key] }
91
+ attr_map = self.class.attributes
92
+ all_required_attributes_provided = attr_map
93
+ .each_key
94
+ .all? do |key|
95
+ attributes[key] || (attr_map[key].superclass == Optional)
96
+ end
97
+
98
+ unless all_required_attributes_provided
73
99
  non_nil_keys = attributes
74
100
  .select { |_attr, value| value }
75
101
  .map { |attr, _value| attr }
@@ -1,3 +1,3 @@
1
1
  module Primalize
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
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.1
4
+ version: 0.3.0
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-16 00:00:00.000000000 Z
11
+ date: 2017-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -109,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
109
  version: '0'
110
110
  requirements: []
111
111
  rubyforge_project:
112
- rubygems_version: 2.6.13
112
+ rubygems_version: 2.6.12
113
113
  signing_key:
114
114
  specification_version: 4
115
115
  summary: Type-checked serializers for your Ruby objects