schema_serializer 0.2.2 → 0.2.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
  SHA256:
3
- metadata.gz: 76b379e699bae38405975eea703631cd6cd03af861424edba441a465359bde1f
4
- data.tar.gz: 372da7064e69db3513ae8299ee32e416b0bef725bf3af67028574bc937b1397d
3
+ metadata.gz: 8a88a43d37245072aabcdb80815179a1b33a0214f9663f55333a1d493b09b940
4
+ data.tar.gz: 0f599ae0884b584751310a3350876ba066f510966c4028499d98f3b4d59bd986
5
5
  SHA512:
6
- metadata.gz: 791ccb7c5e8c3fa602e1c1051abfd54ed7aa1de11c89107deee57e9bd0a39ddf997d93dd4b98b9435c34c37c2ba259c075012020a56d280f9a80e2779b951dc9
7
- data.tar.gz: 4ff317962923d97df1db5d037057b5ea87be3c6c7b1bf47de043bd7d6997d055f4cee190eb1ca520a1098770201c45133fcd983b7ad67a3ae95ad65a6b66e822
6
+ metadata.gz: b67835d5764c9b898979fd6143baea0d8f2f53b8ee80ea47de47fe3424c710e80d3b61a25947a87908badff2d4d215c8101488be8b65220b39eeb79d41a65892
7
+ data.tar.gz: 9d6a75ff1f6ad70b7ff54a82bf0269bd8853f281cfbca86d5c6788c69b5cb02eda5484cdc847a4bf6852b67d3afd92025f32ef3b52791201bc66e558777b3472
@@ -0,0 +1,23 @@
1
+ class SchemaSerializer
2
+ class Configuration
3
+ VALID_OPTIONS = [
4
+ :raise_on_null,
5
+ ].freeze
6
+
7
+ attr_accessor *VALID_OPTIONS
8
+
9
+ def initialize(options = {})
10
+ reset
11
+ merge(options)
12
+ end
13
+
14
+ def merge(options)
15
+ options.keys.each { |key| send("#{key}=", options[key]) }
16
+ self
17
+ end
18
+
19
+ def reset
20
+ @raise_on_null = true
21
+ end
22
+ end
23
+ end
@@ -4,7 +4,7 @@ class SchemaSerializer
4
4
 
5
5
  def initialize(hash)
6
6
  @schemas = hash.each_with_object({}) { |(name, schema), obj|
7
- obj[name.to_s] = Schema.new(schema)
7
+ obj[name.to_s] = Schema.new(name, schema)
8
8
  }
9
9
  end
10
10
 
@@ -2,4 +2,5 @@ class SchemaSerializer
2
2
  class BaseError < StandardError; end
3
3
  class SchemaNotFound < BaseError; end
4
4
  class RequiredNotDefined < BaseError; end
5
+ class NullValue < BaseError; end
5
6
  end
@@ -1,8 +1,9 @@
1
1
  class SchemaSerializer
2
2
  class Schema
3
- attr_reader :type, :nullable, :items, :required, :properties
3
+ attr_reader :key, :type, :nullable, :items, :required, :properties
4
4
 
5
- def initialize(hash = {})
5
+ def initialize(key, hash = {})
6
+ @key = key
6
7
  @type = hash["type"]
7
8
  @nullable = !hash["nullable"].nil?
8
9
 
@@ -18,7 +19,10 @@ class SchemaSerializer
18
19
  end
19
20
 
20
21
  def serialize(object)
21
- return nil if nullable && object.nil?
22
+ if object.nil?
23
+ return nil if nullable
24
+ raise NullValue, "#{key} is not allowed to be null" if SchemaSerializer.config.raise_on_null
25
+ end
22
26
 
23
27
  case type
24
28
  when "integer"
@@ -1,3 +1,3 @@
1
1
  class SchemaSerializer
2
- VERSION = "0.2.2".freeze
2
+ VERSION = "0.2.3".freeze
3
3
  end
@@ -2,6 +2,7 @@ require "active_support"
2
2
  require "active_support/core_ext"
3
3
  require "yaml_ext"
4
4
 
5
+ require "schema_serializer/configuration"
5
6
  require "schema_serializer/definition"
6
7
  require "schema_serializer/schema"
7
8
  require "schema_serializer/serializable"
@@ -27,6 +28,10 @@ class SchemaSerializer
27
28
  def definition=(define)
28
29
  @definition = define.is_a?(SchemaSerializer::Definition) ? define : SchemaSerializer::Definition.new(define)
29
30
  end
31
+
32
+ def config
33
+ @config ||= Configuration.new
34
+ end
30
35
  end
31
36
 
32
37
  def initialize(object, options = {})
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - i2bskn
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-11 00:00:00.000000000 Z
11
+ date: 2019-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -175,6 +175,7 @@ files:
175
175
  - lib/generators/schema_serializer/serializer/serializer_generator.rb
176
176
  - lib/generators/schema_serializer/serializer/templates/serializer.rb.erb
177
177
  - lib/schema_serializer.rb
178
+ - lib/schema_serializer/configuration.rb
178
179
  - lib/schema_serializer/definition.rb
179
180
  - lib/schema_serializer/errors.rb
180
181
  - lib/schema_serializer/railtie.rb