simple-serializer 0.1.4 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/simple_serializer.rb +13 -9
  3. metadata +7 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b16bb9cbab2643eb539ea0e1dd1ea4a99dcee623e28a5147d1ea91febd1d3e54
4
- data.tar.gz: 673c7795f0c85abb4d0a3802bdfc8a20d0c4d4d2c86775397a49843abf75e00d
3
+ metadata.gz: d5491f33ddeffa78d0cc0ae1462211fb97b045423d759137e58bed7630611b6d
4
+ data.tar.gz: 5b6d104bbbd0e579168966cbeb8b77355707d75caea4191c41916fac5d403f3a
5
5
  SHA512:
6
- metadata.gz: b5d36e897bbe0256cb91693ff5863b870fb2a2b9c2dacfba496b6119e34e9f982fd397aea037beb8ba749f34ae8812fc1c684ea5315a0ae8a1c6597f58c445fa
7
- data.tar.gz: 6e936d6b429421b8c9e53b93fad589ba87ff8218dd29cf8e3c946691c83a1dba95962cc2cf9844f17db92842c4cdb8d29eff9afa981891c43f3d0ea54054dc47
6
+ metadata.gz: 5256c874b609e8a986a4f5b26526d1f30ec3df528bdd0876e579bd8adbda672992a0d1ac6adc6d5627afd288e5d9ee3ccec56346dcb180d98d38aca30b937bb5
7
+ data.tar.gz: aacc961ae145bbe1536e2c5072827e4d6fcd5ace3a2e83eaf45fac3668cf885e79755d0c860c1b7875469108bce6587e15f3e537cf3d9ca7dd25f5798980fc7e
@@ -1,9 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class SimpleSerializer
4
- VERSION = '0.1.4'
4
+ VERSION = '0.1.7'
5
5
 
6
- def initialize(object)
6
+ def initialize(object, opts = {})
7
+ @include = opts[:include] || []
8
+ @exclude = opts[:exclude] || []
7
9
  @object = object
8
10
  end
9
11
 
@@ -25,9 +27,9 @@ class SimpleSerializer
25
27
 
26
28
  Module.new.tap do |m|
27
29
  m.define_singleton_method(:included) do |base|
28
- base.define_method(:to_h) { serializer.to_h(self) }
30
+ base.define_method(:to_h) { |o = {}| serializer.to_h(self, o) }
29
31
 
30
- base.define_method(:to_json) { serializer.to_json(self) }
32
+ base.define_method(:to_json) { |o = {}| serializer.to_json(self, o) }
31
33
 
32
34
  base.define_method(:serializer_class) { serializer }
33
35
  end
@@ -38,12 +40,12 @@ class SimpleSerializer
38
40
  to_h(object)
39
41
  end
40
42
 
41
- def to_h(object)
42
- new(object).to_h
43
+ def to_h(object, opts = {})
44
+ new(object, opts).to_h
43
45
  end
44
46
 
45
- def to_json(object)
46
- to_h(object).to_json
47
+ def to_json(object, opts = {})
48
+ to_h(object, opts).to_json
47
49
  end
48
50
 
49
51
  def attributes(*fields, **named, &block)
@@ -64,7 +66,9 @@ class SimpleSerializer
64
66
  private
65
67
 
66
68
  def to_h_single(object)
67
- self.class.fields&.reduce({}) do |h, (k, v)|
69
+ self.class.fields&.merge(Hash[@include.zip])&.reduce({}) do |h, (k, v)|
70
+ next h if @exclude.include?(k)
71
+
68
72
  h.merge!(serialize_key_val(object, k, v))
69
73
  end
70
74
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikhail Slyusarev
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-14 00:00:00.000000000 Z
11
+ date: 2022-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -38,7 +38,7 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0.60'
41
- description:
41
+ description:
42
42
  email:
43
43
  - slyusarevmikhail@gmail.com
44
44
  executables: []
@@ -51,7 +51,7 @@ homepage: https://github.com/mikhailvs/simple-serializer
51
51
  licenses:
52
52
  - MIT
53
53
  metadata: {}
54
- post_install_message:
54
+ post_install_message:
55
55
  rdoc_options: []
56
56
  require_paths:
57
57
  - lib
@@ -66,8 +66,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
66
  - !ruby/object:Gem::Version
67
67
  version: '0'
68
68
  requirements: []
69
- rubygems_version: 3.1.2
70
- signing_key:
69
+ rubygems_version: 3.3.3
70
+ signing_key:
71
71
  specification_version: 4
72
72
  summary: Simple serializer. Easily serialize objects.
73
73
  test_files: []