userializer 0.3.4 → 0.3.6

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
  SHA256:
3
- metadata.gz: 6aa1e23fadfa753380b8295f2438939b47b691be108b8f9e8307d43bce6066ad
4
- data.tar.gz: 53641e65a503ca7145e9d1dd7bd98483589668696c11d098f3061b9d018f07ed
3
+ metadata.gz: f45f6aeeabcbbd48bcd12a6f8c5943c83afda5d6e6c7d22fb86e4d7957c53610
4
+ data.tar.gz: d9b87f22c3ab5ea6c75f332532615b63b6f29ac38355769e31aaae65ddf78cb0
5
5
  SHA512:
6
- metadata.gz: fa343936b8c54a8c293d2e4952f84b92629b4ed99d2bd32c2559a66718b1592c7d7c1a20b53474e684691bd31a1214c631853422ea42a809b988c239d431268d
7
- data.tar.gz: 5f47fbf1ec13d6a3155741d73d2b007c3572ce47279386e946b07cca723b0643e37f0ad7daf3c4efd151b2e1aa1ba318b5f4f25e4168f66d51f08f0763c842d4
6
+ metadata.gz: 2d8536db93c95b3ecd708479f71fa45f075461bcdae1a4165e9d7850909749e636e0712cfcbcbed968866f2f962f9e1ad43f40671ca2543c82a2c8223a6ae1b2
7
+ data.tar.gz: c1f76f0fae861831efebe32459db52cd0d6ea16aa269c4f058ab226dad2b54b588c154a38086e97666a5a23923ea717af25af7e17fc3707a4e9e1a3770230739
@@ -9,17 +9,5 @@ on:
9
9
  jobs:
10
10
  test:
11
11
  name: Run Tests
12
- runs-on: ubuntu-20.04
13
- strategy:
14
- matrix:
15
- ruby: [2.7, 3.0.3]
16
- steps:
17
- - name: Checkout
18
- uses: actions/checkout@v2
19
- - name: Install Ruby
20
- uses: ruby/setup-ruby@v1
21
- with:
22
- ruby-version: ${{ matrix.ruby }}
23
- bundler-cache: true
24
- - name: Run the Test Suite
25
- run: bundle exec rspec --color --require spec_helper --format progress spec
12
+ uses: upfluence/actions/.github/workflows/lib-ruby-test.yml@master
13
+ secrets: inherit
data/README.md CHANGED
@@ -105,6 +105,31 @@ Way nicer, right?
105
105
  Just like AMS, USerializer supports `has_one` and `has_many`
106
106
  relationships
107
107
 
108
+ ### Collection Attributes Filtering
109
+
110
+ For `has_many` relationships, USerializer allow you to serialize only
111
+ part of the collection that matches some criterias.
112
+ It relies on the ActiveRecord `scope` feature :
113
+
114
+ ```ruby
115
+ class Product < ActiveRecord::Base
116
+ has_many :variants
117
+ end
118
+
119
+ class Variant < ActiveRecord::Base
120
+ belongs_to :product
121
+
122
+ scope :available, -> { where(delete_at: nil) }
123
+ end
124
+
125
+ class ProductSerializer < USerializer::BaseSerializer
126
+ has_many :variants, scope: :available
127
+ end
128
+
129
+ class VariantSerializer < USerializer::BaseSerializer
130
+ end
131
+ ```
132
+
108
133
  ### Serialized Output
109
134
 
110
135
  The following outputs will be based an on our `Order` object in
@@ -55,8 +55,8 @@ module USerializer
55
55
  def serializable_hash(opts)
56
56
  res = {}
57
57
 
58
- attributes.each { |attr| attr.merge_attributes(res, self, opts) }
59
- relations.each do |rel|
58
+ _attributes.each { |attr| attr.merge_attributes(res, self, opts) }
59
+ _relations.each do |rel|
60
60
  rel.merge_attributes(res, self, opts)
61
61
  end
62
62
 
@@ -78,7 +78,7 @@ module USerializer
78
78
  end
79
79
  end
80
80
 
81
- relations.each { |rel| rel.merge_root(res, self, opts) }
81
+ _relations.each { |rel| rel.merge_root(res, self, opts) }
82
82
  end
83
83
 
84
84
  def to_hash
@@ -99,14 +99,14 @@ module USerializer
99
99
 
100
100
  private
101
101
 
102
- def attributes
103
- @attributes ||= (self.class.attrs || {}).values.select do |attr|
102
+ def _attributes
103
+ @_attributes ||= (self.class.attrs || {}).values.select do |attr|
104
104
  allow?(attr.key)
105
105
  end
106
106
  end
107
107
 
108
- def relations
109
- @relations ||= (self.class.relations || {}).values.select do |rel|
108
+ def _relations
109
+ @_relations ||= (self.class.relations || {}).values.select do |rel|
110
110
  allow?(rel.key)
111
111
  end
112
112
  end
@@ -61,7 +61,7 @@ module USerializer
61
61
  objs.map do |(key, obj)|
62
62
  opts = options_for(key)
63
63
 
64
- if obj.is_a? Enumerable
64
+ if obj.is_a? Array
65
65
  ArraySerializer.new(obj, opts)
66
66
  else
67
67
  CompositeObject.new(obj, opts)
@@ -0,0 +1,29 @@
1
+ require 'oj'
2
+
3
+ module USerializer
4
+ class RawSerializer
5
+ def initialize(obj, _)
6
+ @obj = obj
7
+ end
8
+
9
+ def serializable_hash(_)
10
+ @obj
11
+ end
12
+
13
+ def merge_root(res, key, _, _)
14
+ res[key] = @obj
15
+ end
16
+
17
+ def to_hash
18
+ res = {}
19
+
20
+ merge_root(res, @root_key, true, nil)
21
+
22
+ res
23
+ end
24
+
25
+ def to_json
26
+ Oj.dump(to_hash, mode: :compat)
27
+ end
28
+ end
29
+ end
@@ -1,3 +1,3 @@
1
1
  module USerializer
2
- VERSION = "0.3.4"
2
+ VERSION = "0.3.6"
3
3
  end
data/lib/userializer.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'userializer/version'
2
2
  require 'userializer/base_serializer'
3
3
  require 'userializer/array_serializer'
4
+ require 'userializer/raw_serializer'
4
5
  require 'userializer/composite_serializer'
5
6
 
6
7
  module USerializer
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: userializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexis Montagne
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-10-18 00:00:00.000000000 Z
11
+ date: 2025-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -104,6 +104,7 @@ files:
104
104
  - lib/userializer/composite_serializer.rb
105
105
  - lib/userializer/has_many.rb
106
106
  - lib/userializer/has_one.rb
107
+ - lib/userializer/raw_serializer.rb
107
108
  - lib/userializer/version.rb
108
109
  - userializer.gemspec
109
110
  homepage: https://github.com/upfluence/userializer
@@ -124,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
125
  - !ruby/object:Gem::Version
125
126
  version: '0'
126
127
  requirements: []
127
- rubygems_version: 3.2.32
128
+ rubygems_version: 3.3.7
128
129
  signing_key:
129
130
  specification_version: 4
130
131
  summary: Write a short summary, because RubyGems requires one.