basic_serializer 0.1.4 → 0.1.5

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.

Potentially problematic release.


This version of basic_serializer might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5bff1df65a7d8f5f4291b756123bdd69b550b7b7b81154acfac6904da08591a1
4
- data.tar.gz: 02a7c24410460c42f7a01c4b66abec77643a6753c21924c9fae01753ccadd621
3
+ metadata.gz: 92045b1fba9ae2936d58f4dc47127467a4264dd53e7e1622a410d0f75e984daf
4
+ data.tar.gz: e2c8f5bd89a2955a10c505ab88449f5c8e19e3ad83de58a0dafcb81f357ba286
5
5
  SHA512:
6
- metadata.gz: 8d9fc3f31a09ad0e253c0b8e8c3924cb9d89495e077e254b1f895c1b54e0445698e3f2e7d008607e504aa27112bd4944091552560c596163a6da504a3eb9bcb2
7
- data.tar.gz: 73c410b2132335d89762d04013a985dac5f3c52a46d55da5dc10a3de1a82e2cdfddf15b71b26d148b709120a5a929dbf687c3ed2d68ae629b9229a189fb3a92c
6
+ metadata.gz: 101db57a5015c1c6be60590dfb7c5a04d4144a30811a0a1ade238ed1492dbc156d076cd4691ad062d277d6765df4919918d532ffe320ca8544a3f92f41799217
7
+ data.tar.gz: 7202ed9ffea28ba0c26f71577b68c06fc6b66ff62da718c0af91689f1be3834b89ecc53f42f0165d5ffc05ae0ad25f6d757c3a7ec4271d4ee02f5587a04a0ddd
data/README.md CHANGED
@@ -17,21 +17,26 @@ If bundler is not being used to manage dependencies, install the gem by executin
17
17
 
18
18
  ```ruby
19
19
  class UserSerializer < BasicSerializer
20
- attribute :id, :integer
21
- attribute :name, :string
22
- attribute :email, :string
20
+ attribute :id, :integer # attribute definition
21
+ attribute :name, :string # attribute definition
22
+ attribute :email, :string # attribute definition
23
23
 
24
- format :json, pretty: true
24
+ format :json, pretty: true # output format and options
25
25
 
26
- model_name "User" # optional model name metadata
26
+ model_name "User" # optional model name metadata
27
27
  schema_ref "#/components/schemas/User" # optional swagger schema reference
28
28
  end
29
29
 
30
30
  serializer = UserSerializer.new(name: 'Foobar', email: 'email@domain.com', id: 1)
31
- serializer.to_json
32
- serializer.to_yaml
33
31
  serializer.serialize
34
32
  ```
33
+ ```json
34
+ {
35
+ "id": 1,
36
+ "name": "Foobar",
37
+ "email": "email@domain.com"
38
+ }
39
+ ```
35
40
 
36
41
  ## Development
37
42
 
@@ -3,9 +3,10 @@
3
3
  class BasicSerializer
4
4
  module Config
5
5
  OJ_FORMAT = {
6
- indent: 2,
7
- space: " ",
8
- space_before: "",
6
+ mode: :compat,
7
+ indent: " ",
8
+ space: " ", # This controls both spaces after commas AND colons
9
+ space_before: "", # This controls space before colons
9
10
  object_nl: "\n",
10
11
  array_nl: "\n"
11
12
  }.freeze
@@ -27,8 +27,7 @@ class BasicSerializer
27
27
  end
28
28
 
29
29
  def format(name, pretty: false)
30
- @format ||= name
31
- @format ||= pretty
30
+ @format ||= { name: name, pretty: pretty }
32
31
  end
33
32
  end
34
33
  end
@@ -31,7 +31,7 @@ class BasicSerializer
31
31
  alias as_json stringified_attributes
32
32
 
33
33
  def to_json(*_args)
34
- pretty = self.class.instance_variable_get(:@pretty)
34
+ pretty = self.class.instance_variable_get(:@format)&.dig(:pretty)
35
35
 
36
36
  Oj.dump(stringified_attributes, **(pretty ? Config::OJ_FORMAT : {}))
37
37
  end
@@ -41,7 +41,7 @@ class BasicSerializer
41
41
  end
42
42
 
43
43
  def serialize
44
- case self.class.instance_variable_get(:@format)
44
+ case self.class.instance_variable_get(:@format)&.dig(:name)
45
45
  when :json then to_json
46
46
  when :yaml then to_yaml
47
47
  else stringified_attributes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: basic_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Ladachowski
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-11-06 00:00:00.000000000 Z
11
+ date: 2024-11-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A lightweight and straightforward serializer for Ruby objects without
14
14
  heavy dependencies