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 +4 -4
- data/.github/workflows/ci.yml +2 -14
- data/README.md +25 -0
- data/lib/userializer/base_serializer.rb +7 -7
- data/lib/userializer/composite_serializer.rb +1 -1
- data/lib/userializer/raw_serializer.rb +29 -0
- data/lib/userializer/version.rb +1 -1
- data/lib/userializer.rb +1 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f45f6aeeabcbbd48bcd12a6f8c5943c83afda5d6e6c7d22fb86e4d7957c53610
|
4
|
+
data.tar.gz: d9b87f22c3ab5ea6c75f332532615b63b6f29ac38355769e31aaae65ddf78cb0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d8536db93c95b3ecd708479f71fa45f075461bcdae1a4165e9d7850909749e636e0712cfcbcbed968866f2f962f9e1ad43f40671ca2543c82a2c8223a6ae1b2
|
7
|
+
data.tar.gz: c1f76f0fae861831efebe32459db52cd0d6ea16aa269c4f058ab226dad2b54b588c154a38086e97666a5a23923ea717af25af7e17fc3707a4e9e1a3770230739
|
data/.github/workflows/ci.yml
CHANGED
@@ -9,17 +9,5 @@ on:
|
|
9
9
|
jobs:
|
10
10
|
test:
|
11
11
|
name: Run Tests
|
12
|
-
|
13
|
-
|
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
|
-
|
59
|
-
|
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
|
-
|
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
|
103
|
-
@
|
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
|
109
|
-
@
|
108
|
+
def _relations
|
109
|
+
@_relations ||= (self.class.relations || {}).values.select do |rel|
|
110
110
|
allow?(rel.key)
|
111
111
|
end
|
112
112
|
end
|
@@ -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
|
data/lib/userializer/version.rb
CHANGED
data/lib/userializer.rb
CHANGED
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
|
+
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:
|
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.
|
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.
|