userializer 0.3.8 → 0.3.9

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: 82b55a7bfb0a68c186ad7c9026d562278cd24ec18e9a133f1aeed5e81cbb6678
4
- data.tar.gz: f5b20444ea431280de74e8cc070279004fbcc0dade3fcf65f46b13667bbe209f
3
+ metadata.gz: 6b63d4a06db2cc693bd01bc42a357d15acc79d2237e0ae718d4736a0015d0d7b
4
+ data.tar.gz: 3a940b69214c438a19c12ae76a7a8b27e449350e755cdab324fb234f16af2e14
5
5
  SHA512:
6
- metadata.gz: 34b273eeda892d83f0793765aa8c333d0aadbe9f85ef66dbf0ea1aca3f56311ebb0042cffe183729121be592ab165935937e74f9d57a81d160bc2358cd2d8427
7
- data.tar.gz: 1affe46e232fa28bc620568d9dcff8849d0115f97f2e9a39440c15684a7ca41e300b4e8cdfaa9b1da709a817aaafa433f181a6b665bb3cdf029a39f198e45c71
6
+ metadata.gz: 8fdec32d27bbd27e3b74570efe6321f96a1ff1011d79a91aaa634488cd0a78ac668bab5cc9d11a3686673d87f40cc3bd32916a18cb9da43e9709459b52958c8a
7
+ data.tar.gz: abae1754977205a4fdb8b2f776b6b3afedbfdcdc46eb7b46bbbde689bcd7417112deafca016657750a72a3885b3b4245663978427ac8e12ade5b4ec4a7fb1b52
@@ -11,3 +11,20 @@ jobs:
11
11
  name: Run Tests
12
12
  uses: upfluence/actions/.github/workflows/lib-ruby-test.yml@master
13
13
  secrets: inherit
14
+
15
+ release:
16
+ needs: test
17
+ if: github.ref == 'refs/heads/master'
18
+ runs-on: ubuntu-24.04
19
+ steps:
20
+ - name: Checkout
21
+ uses: actions/checkout@v6
22
+ with:
23
+ fetch-depth: 0
24
+ - name: Bump version
25
+ id: bump-version
26
+ uses: upfluence/actions/bump-version@master
27
+ - name: Create release
28
+ uses: upfluence/actions/create-github-release@master
29
+ with:
30
+ version: ${{ steps.bump-version.outputs.version }}
data/Makefile ADDED
@@ -0,0 +1,4 @@
1
+ .PHONY: release
2
+
3
+ release:
4
+ BUNDLE_FROZEN=false bundle exec rake release:rubygem_push
data/Rakefile CHANGED
@@ -1,6 +1,27 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
3
 
4
+ version_file = "lib/userializer/version.rb"
5
+
6
+ task :update_version do
7
+ version = (ENV['VERSION'] || `git describe --tag 2>/dev/null` || '').chomp.sub(/\Av/, '').then { |v| v.empty? ? '0.0.0' : v }
8
+ original_version = File.binread(version_file)
9
+
10
+ Bundler::GemHelper.gemspec.version = version
11
+ at_exit { File.binwrite(version_file, original_version) }
12
+
13
+ File.write(
14
+ version_file,
15
+ <<~RUBY
16
+ module USerializer
17
+ VERSION = "#{version}"
18
+ end
19
+ RUBY
20
+ )
21
+ end
22
+
23
+ Rake::Task[:build].enhance([:update_version])
24
+
4
25
  RSpec::Core::RakeTask.new(:spec)
5
26
 
6
27
  task :default => :spec
@@ -1,24 +1,22 @@
1
1
  require 'oj'
2
+ require 'userializer/payload'
2
3
 
3
4
  module USerializer
4
5
  class HeterogeneousArray < StandardError; end
5
6
 
6
7
  class ArraySerializer
7
- def initialize(objs, opts = {}, embed_empty_array: false)
8
+ def initialize(objs, opts = {})
8
9
  @objs = objs.compact
9
10
  @opts = opts
10
11
  @meta = opts[:meta]
11
- @embed_empty_array = embed_empty_array
12
+ @embed_empty_array = opts[:embed_empty_array] || false
12
13
 
13
- clss = @objs.map(&:class).uniq
14
- obj_class = clss.first
14
+ obj_class = @objs.first&.class
15
15
 
16
- raise HeterogeneousArray if clss.count > 1
16
+ raise HeterogeneousArray if @objs.any? { |o| !o.instance_of?(obj_class) }
17
17
 
18
18
  @root_key = opts[:root]&.to_sym
19
- @root_key ||= ActiveSupport::Inflector.pluralize(
20
- ActiveSupport::Inflector.underscore(obj_class.name).split('/').last
21
- ).to_sym if obj_class
19
+ @root_key ||= USerializer.infered_root_key(obj_class) if obj_class
22
20
 
23
21
  serializer = opts[:each_serializer]
24
22
 
@@ -37,14 +35,14 @@ module USerializer
37
35
  end
38
36
 
39
37
  def to_hash
40
- res = {}
38
+ res = Payload.new
41
39
 
42
40
  res[@root_key] = [] if @root_key
43
41
 
44
42
  merge_root(res, @opts)
45
43
  res[:meta] = @meta if @meta
46
44
 
47
- res
45
+ res.to_h
48
46
  end
49
47
 
50
48
  def to_json
@@ -59,7 +57,13 @@ module USerializer
59
57
  return @serializer.call(obj, opts).new(obj, @opts) if @serializer
60
58
  return obj.serialize if obj.respond_to?(:serialize)
61
59
 
62
- USerializer.infered_serializer_class(obj.class).new(obj, @opts)
60
+ infered_serializer.new(obj, @opts)
61
+ end
62
+
63
+ def infered_serializer
64
+ @infered_serializer ||= USerializer.infered_serializer_class(
65
+ @objs.first.class
66
+ )
63
67
  end
64
68
  end
65
69
  end
@@ -8,17 +8,23 @@ module USerializer
8
8
  @block = block
9
9
 
10
10
  @skip_nil = opts[:skip_nil] || false
11
- @conditional_block = opts[:if] || proc { true }
11
+ @conditional_block = opts[:if]
12
12
  end
13
13
 
14
14
  def merge_attributes(res, ser, opts)
15
- return unless @conditional_block.call(ser.object, opts)
15
+ return unless allow?(ser, opts)
16
16
 
17
- value = @block ? @block.call(ser.object, opts) : ser.send(@key)
17
+ value = @block ? @block.call(ser.object, opts) : ser.read_attribute(@key)
18
18
 
19
19
  return if value.nil? && @skip_nil
20
20
 
21
21
  res[@key] = value
22
22
  end
23
+
24
+ private
25
+
26
+ def allow?(ser, opts)
27
+ @conditional_block.nil? || @conditional_block.call(ser.object, opts)
28
+ end
23
29
  end
24
30
  end
@@ -1,8 +1,10 @@
1
+ require 'set'
1
2
  require 'oj'
2
3
  require 'active_support/inflector/methods'
3
4
  require 'userializer/has_one'
4
5
  require 'userializer/has_many'
5
6
  require 'userializer/attribute'
7
+ require 'userializer/payload'
6
8
 
7
9
  module USerializer
8
10
  class BaseSerializer
@@ -10,6 +12,26 @@ module USerializer
10
12
  def inherited(subclass)
11
13
  subclass.attrs = self.attrs.dup || { id: Attribute.new(:id, {}, nil) }
12
14
  subclass.relations = self.relations.dup || {}
15
+ subclass.serializer_methods = self.serializer_methods.dup || Set.new
16
+ end
17
+
18
+ # BaseSerializer itself has no registry, so its own methods are never
19
+ # treated as serializer methods.
20
+ def method_added(name)
21
+ super
22
+ serializer_methods&.add(name)
23
+ end
24
+
25
+ def include(*mods)
26
+ super
27
+
28
+ mods.each do |mod|
29
+ serializer_methods&.merge(
30
+ mod.instance_methods + mod.private_instance_methods
31
+ )
32
+ end
33
+
34
+ self
13
35
  end
14
36
 
15
37
  def attributes(*attrs, &block)
@@ -33,7 +55,7 @@ module USerializer
33
55
  attrs.each { |attr| @relations[attr] = HasMany.new(attr, opts) }
34
56
  end
35
57
 
36
- attr_accessor :attrs, :relations
58
+ attr_accessor :attrs, :relations, :serializer_methods
37
59
  end
38
60
 
39
61
  attr_reader :obj, :meta, :opts
@@ -44,21 +66,15 @@ module USerializer
44
66
  @obj = obj
45
67
  @opts = opts
46
68
  @meta = opts[:meta]
47
- @except = Set.new([opts[:except]].flatten.compact)
48
- @only = Set.new([opts[:only]].flatten.compact)
49
-
50
- @root_key = (opts[:root] || ActiveSupport::Inflector.underscore(
51
- obj.class.name
52
- ).split('/').last).to_sym
69
+ @except = key_filter(opts[:except])
70
+ @only = key_filter(opts[:only])
53
71
  end
54
72
 
55
73
  def serializable_hash(opts)
56
74
  res = {}
57
75
 
58
- _attributes.each { |attr| attr.merge_attributes(res, self, opts) }
59
- _relations.each do |rel|
60
- rel.merge_attributes(res, self, opts)
61
- end
76
+ _attributes { |attr| attr.merge_attributes(res, self, opts) }
77
+ _relations { |rel| rel.merge_attributes(res, self, opts) }
62
78
 
63
79
  res
64
80
  end
@@ -67,28 +83,36 @@ module USerializer
67
83
  if single
68
84
  res[key] = serializable_hash(opts)
69
85
  else
70
- res[key] ||= []
86
+ return if res.include_id?(key, @obj.id)
71
87
 
72
- id = @obj.id
73
-
74
- if res[key].detect { |v| id && v[:id] == id }
75
- return
76
- else
77
- res[key] << serializable_hash(opts)
78
- end
88
+ res.push(key, serializable_hash(opts))
79
89
  end
80
90
 
81
- _relations.each { |rel| rel.merge_root(res, self, opts) }
91
+ _relations { |rel| rel.merge_root(res, self, opts) }
82
92
  end
83
93
 
84
94
  def to_hash
85
- res = {}
95
+ res = Payload.new
86
96
 
87
- merge_root(res, @root_key, true, @opts.slice(:scope))
97
+ merge_root(res, _root_key, true, @opts.slice(:scope))
88
98
 
89
99
  res[:meta] = @meta if @meta
90
100
 
91
- res
101
+ res.to_h
102
+ end
103
+
104
+ def read_attribute(key)
105
+ return send(key) if self.class.serializer_methods.include?(key)
106
+
107
+ @obj.send(key)
108
+ end
109
+
110
+ def relation_value(key)
111
+ @_relation_values ||= {}
112
+
113
+ return @_relation_values[key] if @_relation_values.key?(key)
114
+
115
+ @_relation_values[key] = yield
92
116
  end
93
117
 
94
118
  def to_json
@@ -99,20 +123,38 @@ module USerializer
99
123
 
100
124
  private
101
125
 
126
+ def _root_key
127
+ @_root_key ||= if @opts[:root]
128
+ @opts[:root].to_sym
129
+ else
130
+ USerializer.infered_singular_root_key(@obj.class)
131
+ end
132
+ end
133
+
102
134
  def _attributes
103
- @_attributes ||= (self.class.attrs || {}).values.select do |attr|
104
- allow?(attr.key)
135
+ (self.class.attrs || {}).each_value do |attr|
136
+ yield attr if allow?(attr.key)
105
137
  end
106
138
  end
107
139
 
108
140
  def _relations
109
- @_relations ||= (self.class.relations || {}).values.select do |rel|
110
- allow?(rel.key)
141
+ (self.class.relations || {}).each_value do |rel|
142
+ yield rel if allow?(rel.key)
111
143
  end
112
144
  end
113
145
 
146
+ def key_filter(keys)
147
+ return if keys.nil?
148
+
149
+ keys = [keys].flatten.compact
150
+
151
+ Set.new(keys) unless keys.empty?
152
+ end
153
+
114
154
  def allow?(key)
115
- return @only.include?(key) if @only.any?
155
+ return @only.include?(key) if @only
156
+ return true unless @except
157
+
116
158
  !@except.include?(key)
117
159
  end
118
160
  end
@@ -1,4 +1,5 @@
1
1
  require 'oj'
2
+ require 'userializer/payload'
2
3
 
3
4
  module USerializer
4
5
  class CompositeObject
@@ -41,10 +42,10 @@ module USerializer
41
42
  end
42
43
 
43
44
  def to_hash
44
- res = {}
45
+ res = Payload.new
45
46
 
46
47
  merge_root(res, @opts)
47
- res
48
+ res.to_h
48
49
  end
49
50
 
50
51
  def serialize(*_args)
@@ -64,7 +65,7 @@ module USerializer
64
65
  if obj.is_a?(Hash) || !obj.is_a?(Enumerable)
65
66
  CompositeObject.new(obj, opts)
66
67
  else
67
- ArraySerializer.new(obj, opts, embed_empty_array: true)
68
+ ArraySerializer.new(obj, opts.merge(embed_empty_array: true))
68
69
  end
69
70
  end
70
71
  end
@@ -9,34 +9,42 @@ module USerializer
9
9
  @ids_key = opts[:ids_key] || build_ids_key(key)
10
10
 
11
11
  @embed_key = opts[:embed_key] || :id
12
- @conditional_block = opts[:if] || proc { true }
12
+ @conditional_block = opts[:if]
13
13
  end
14
14
 
15
15
  def merge_attributes(res, ser, opts)
16
- return unless @conditional_block.call(ser.object, opts)
16
+ return unless allow?(ser, opts)
17
17
 
18
- res[@ids_key] = (entities(ser) || []).compact.map do |obj|
19
- obj.nil? ? nil : obj.send(@embed_key)
20
- end.compact
18
+ res[@ids_key] = (entities(ser) || []).each_with_object([]) do |obj, ids|
19
+ id = obj&.send(@embed_key)
20
+ ids << id unless id.nil?
21
+ end
21
22
  end
22
23
 
23
24
  def merge_root(res, ser, opts)
25
+ return unless allow?(ser, opts)
26
+
24
27
  objs = entities(ser) || []
25
28
 
26
- return if objs.empty? || !@conditional_block.call(ser.object, opts)
29
+ return if objs.empty?
27
30
 
28
31
  ArraySerializer.new(objs, @opts).merge_root(res, opts)
29
32
  end
30
33
 
31
34
  def entities(ser)
32
- obj = ser.send(@key) || []
33
- return obj unless @opts[:scope]
35
+ ser.relation_value(@key) do
36
+ obj = ser.read_attribute(@key) || []
34
37
 
35
- obj.send(@opts[:scope])
38
+ @opts[:scope] ? obj.send(@opts[:scope]) : obj
39
+ end
36
40
  end
37
41
 
38
42
  private
39
43
 
44
+ def allow?(ser, opts)
45
+ @conditional_block.nil? || @conditional_block.call(ser.object, opts)
46
+ end
47
+
40
48
  def build_ids_key(key)
41
49
  "#{ActiveSupport::Inflector.singularize(key)}_ids".to_sym
42
50
  end
@@ -5,6 +5,7 @@ module USerializer
5
5
  @opts = opts
6
6
  @id_key = opts[:id_key] || "#{key}_id".to_sym
7
7
  @root_key = opts[:root]&.to_sym
8
+ @infered_serializers = {}
8
9
 
9
10
  serializer = opts[:serializer]
10
11
 
@@ -15,41 +16,51 @@ module USerializer
15
16
  end
16
17
 
17
18
  @embed_key = opts[:embed_key] || :id
18
- @conditional_block = opts[:if] || proc { true }
19
+ @conditional_block = opts[:if]
19
20
  end
20
21
 
21
22
  attr_reader :id_key, :key
22
23
 
23
24
  def merge_attributes(res, ser, opts)
24
- return unless @conditional_block.call(ser.object, opts)
25
+ return unless allow?(ser, opts)
25
26
 
26
- obj = ser.send(@key)
27
+ obj = entity(ser)
27
28
  res[@id_key] = obj.nil? ? nil : obj.send(@embed_key)
28
29
  end
29
30
 
30
31
  def merge_root(res, ser, opts)
31
- obj = ser.send(@key)
32
+ return unless allow?(ser, opts)
32
33
 
33
- return if obj.nil? || !@conditional_block.call(ser.object, opts)
34
+ obj = entity(ser)
35
+
36
+ return if obj.nil?
34
37
 
35
38
  serializer(obj, opts).merge_root(res, root_key(obj), false, opts)
36
39
  end
37
40
 
41
+ def entity(ser)
42
+ ser.relation_value(@key) { ser.read_attribute(@key) }
43
+ end
44
+
38
45
  private
39
46
 
47
+ def allow?(ser, opts)
48
+ @conditional_block.nil? || @conditional_block.call(ser.object, opts)
49
+ end
50
+
40
51
  def serializer(obj, opts)
41
52
  return @serializer.call(obj, opts).new(obj, @opts) if @serializer
42
53
  return obj.serialize if obj.respond_to?(:serialize)
43
54
 
44
- USerializer.infered_serializer_class(obj.class).new(obj, @opts)
55
+ infered_serializer(obj.class).new(obj, @opts)
45
56
  end
46
57
 
47
- def root_key(obj)
48
- return @root_key if @root_key
58
+ def infered_serializer(kls)
59
+ @infered_serializers[kls] ||= USerializer.infered_serializer_class(kls)
60
+ end
49
61
 
50
- ActiveSupport::Inflector.pluralize(
51
- ActiveSupport::Inflector.underscore(obj.class.name).split('/').last
52
- ).to_sym
62
+ def root_key(obj)
63
+ @root_key || USerializer.infered_root_key(obj.class)
53
64
  end
54
65
  end
55
66
  end
@@ -0,0 +1,28 @@
1
+ module USerializer
2
+ class Payload < Hash
3
+ def []=(key, value)
4
+ @ids&.delete(key)
5
+ super
6
+ end
7
+
8
+ def include_id?(key, id)
9
+ return false unless id
10
+
11
+ ids(key).key?(id)
12
+ end
13
+
14
+ def push(key, value)
15
+ (self[key] ||= []) << value
16
+ ids(key)[value[:id]] = true
17
+ end
18
+
19
+ private
20
+
21
+ def ids(key)
22
+ @ids ||= {}
23
+ @ids[key] ||= (self[key] || []).each_with_object({}) do |v, acc|
24
+ acc[v[:id]] = true
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,3 +1,3 @@
1
1
  module USerializer
2
- VERSION = "0.3.8"
2
+ VERSION = "0.3.9"
3
3
  end
data/lib/userializer.rb CHANGED
@@ -7,6 +7,9 @@ require 'userializer/composite_serializer'
7
7
  module USerializer
8
8
  NS_SEPARATOR = '::'.freeze
9
9
 
10
+ @root_keys = {}
11
+ @singular_root_keys = {}
12
+
10
13
  class << self
11
14
  def serializer_for(obj)
12
15
  return nil if obj.nil?
@@ -17,9 +20,27 @@ module USerializer
17
20
  def infered_serializer_class(kls)
18
21
  return nil if kls.nil?
19
22
 
20
- (kls.name + 'Serializer').split(NS_SEPARATOR).inject(Object) do |o, c|
21
- o.const_get(c)
22
- end
23
+ name = "#{kls.name}Serializer"
24
+
25
+ return Object.const_get(name) if Object.const_defined?(name)
26
+
27
+ name.split(NS_SEPARATOR).inject(Object) { |o, c| o.const_get(c) }
28
+ end
29
+
30
+ def infered_singular_root_key(kls)
31
+ name = kls.name
32
+
33
+ @singular_root_keys[name] ||= ActiveSupport::Inflector.underscore(
34
+ name
35
+ ).split('/').last.to_sym
36
+ end
37
+
38
+ def infered_root_key(kls)
39
+ name = kls.name
40
+
41
+ @root_keys[name] ||= ActiveSupport::Inflector.pluralize(
42
+ ActiveSupport::Inflector.underscore(name).split('/').last
43
+ ).to_sym
23
44
  end
24
45
  end
25
46
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: userializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.8
4
+ version: 0.3.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexis Montagne
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-07-21 00:00:00.000000000 Z
10
+ date: 2026-11-17 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: bundler
@@ -92,6 +92,7 @@ files:
92
92
  - ".rspec"
93
93
  - ".travis.yml"
94
94
  - Gemfile
95
+ - Makefile
95
96
  - README.md
96
97
  - Rakefile
97
98
  - bin/console
@@ -103,6 +104,7 @@ files:
103
104
  - lib/userializer/composite_serializer.rb
104
105
  - lib/userializer/has_many.rb
105
106
  - lib/userializer/has_one.rb
107
+ - lib/userializer/payload.rb
106
108
  - lib/userializer/raw_serializer.rb
107
109
  - lib/userializer/version.rb
108
110
  - userializer.gemspec