flexserializer 1.3.0 → 1.4.0

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
- SHA1:
3
- metadata.gz: 3749f7978bc0bc289332da7da45d64ad75727c1b
4
- data.tar.gz: 40fba01616492f5037fd2adcc64169f284ef357e
2
+ SHA256:
3
+ metadata.gz: c81ac4b55e3a744ecb9b87e3168bd3bf20ee991e3814509927beebe0d9330207
4
+ data.tar.gz: 92645fef924accb8d62627f80feed7fea1411eedf37b733127e2e9ecb5b632be
5
5
  SHA512:
6
- metadata.gz: 8eebebd61086ccb38f862547533e5588ec9aeaa847d37970e23b219c7d3e7eae071813458d1a552d7b3a9c39ae807644d2f0f1d2ad15880dd208f7f3e666ea44
7
- data.tar.gz: 752a6ac3c958b0015c069fae81c272c2c2464ea324a97ad40df47b45407e670b3cf67d16babc6e3bd119af8103b9803e1029085157d6fbe25d6c544a7e229e5f
6
+ metadata.gz: 30e1884d5fd754473005987d78f1ee907e4c883e956373ddb53c34d215b7e0b3a5508aab895bc2d4346e65b5865a61f7a525ae6dfc5daa8b6a9821ed351d1a38
7
+ data.tar.gz: bdd9f45f38da3fb0c2206022f5708ef162e1e7f5aadc01bd04b7a0e25c5455f243d8e17d04ea1c32ac7d29ef56d5855508a2ec6420d4083f767364e4c8b27efa
@@ -1,14 +1,60 @@
1
1
  module Flexserializer
2
2
  class Base < ActiveModel::Serializer
3
+ Definition = Struct.new(:methods_module, :attributes_data, :reflections)
4
+
5
+ class DefinitionBuilder < Module
6
+ attr_reader :attributes_data, :reflections
7
+
8
+ def initialize
9
+ super()
10
+ @attributes_data = {}
11
+ @reflections = {}
12
+ end
13
+
14
+ def define_attributes(*attrs)
15
+ attrs = attrs.first if attrs.first.class == Array
16
+ attrs.each do |attr|
17
+ define_attribute(attr)
18
+ end
19
+ end
20
+
21
+ def define_attribute(attr, options = {}, &block)
22
+ key = options.fetch(:key, attr)
23
+ attributes_data[key] = ActiveModel::Serializer::Attribute.new(attr, options, block)
24
+ end
25
+
26
+ def define_has_many(name, options = {}, &block)
27
+ define_associate(ActiveModel::Serializer::HasManyReflection.new(name, options, block))
28
+ end
29
+
30
+ def define_belongs_to(name, options = {}, &block)
31
+ define_associate(ActiveModel::Serializer::BelongsToReflection.new(name, options, block))
32
+ end
33
+
34
+ def define_has_one(name, options = {}, &block)
35
+ define_associate(ActiveModel::Serializer::HasOneReflection.new(name, options, block))
36
+ end
37
+
38
+ private
39
+
40
+ def define_associate(reflection)
41
+ key = reflection.options[:key] || reflection.name
42
+ reflections[key] = reflection
43
+ end
44
+ end
45
+
3
46
  class << self
4
- attr_accessor :groups, :data_default_attributes
47
+ attr_accessor :groups, :data_default_attributes, :definitions
5
48
 
6
49
  def inherited(base)
50
+ super
7
51
  base.groups = {}
52
+ base.definitions = {}
8
53
  end
9
54
 
10
55
  def default_attributes(&block)
11
56
  self.data_default_attributes = block
57
+ clear_definitions
12
58
  end
13
59
 
14
60
  def group(*group_names, &block)
@@ -16,17 +62,48 @@ module Flexserializer
16
62
  self.groups[name_group] ||= []
17
63
  self.groups[name_group] << block
18
64
  end
65
+ clear_definitions
66
+ end
67
+
68
+ def definition_for(group_name)
69
+ self.definitions ||= {}
70
+ definitions[group_name] ||= build_definition(group_name)
71
+ end
72
+
73
+ private
74
+
75
+ def build_definition(group_name)
76
+ builder = DefinitionBuilder.new
77
+ builder.module_eval(&data_default_attributes) if data_default_attributes
78
+ groups[group_name]&.each do |block|
79
+ builder.module_eval(&block)
80
+ end
81
+
82
+ Definition.new(
83
+ builder,
84
+ builder.attributes_data.freeze,
85
+ builder.reflections.freeze
86
+ )
87
+ end
88
+
89
+ def clear_definitions
90
+ self.definitions = {}
19
91
  end
20
92
  end
21
93
 
22
- attr_reader :group_name, :_attributes_data, :define_options
94
+ attr_reader :group_name, :_attributes_data, :_reflections
23
95
 
24
96
  def initialize(object, options = {})
25
97
  super(object, options)
26
- @_attributes_data = {}
27
98
  @group_name = options[:group]
28
- @define_options = options.clone
29
- make_all_attributes
99
+
100
+ definition = self.class.definition_for(group_name)
101
+ extend(definition.methods_module) if definition_module_has_methods?(definition.methods_module)
102
+
103
+ @_attributes_data = definition.attributes_data
104
+ @_reflections = definition.reflections.each_with_object({}) do |(key, reflection), hash|
105
+ hash[key] = reflection.dup
106
+ end
30
107
  end
31
108
 
32
109
  def define_attributes(*attrs)
@@ -38,12 +115,7 @@ module Flexserializer
38
115
 
39
116
  def define_attribute(attr, options = {}, &block)
40
117
  key = options.fetch(:key, attr)
41
- _attributes_data[key] = Attribute.new(attr, options, block)
42
- end
43
-
44
- def make_all_attributes
45
- define_default_attrs
46
- define_group_attrs
118
+ writable_attributes_data[key] = Attribute.new(attr, options, block)
47
119
  end
48
120
 
49
121
  def define_default_attrs
@@ -57,6 +129,22 @@ module Flexserializer
57
129
  end
58
130
  end
59
131
 
132
+ def define_has_many(name, options = {}, &block)
133
+ define_associate(HasManyReflection.new(name, options, block))
134
+ end
135
+
136
+ def define_belongs_to(name, options = {}, &block)
137
+ define_associate(BelongsToReflection.new(name, options, block))
138
+ end
139
+
140
+ def define_has_one(name, options = {}, &block)
141
+ define_associate(HasOneReflection.new(name, options, block))
142
+ end
143
+
144
+ def define_options
145
+ instance_options
146
+ end
147
+
60
148
  #override serializer methods
61
149
 
62
150
  def attributes(requested_attrs = nil, reload = false)
@@ -67,5 +155,47 @@ module Flexserializer
67
155
  hash[key] = attr.value(self)
68
156
  end
69
157
  end
158
+
159
+ def associations(include_directive = ActiveModelSerializers.default_include_directive, include_slice = nil)
160
+ include_slice ||= include_directive
161
+ return Enumerator.new unless object
162
+
163
+ Enumerator.new do |y|
164
+ _reflections.each do |key, reflection|
165
+ next if reflection.excluded?(self)
166
+ next unless include_directive.key?(key)
167
+
168
+ association = reflection.build_association(self, instance_options, include_slice)
169
+ y.yield association
170
+ end
171
+ end
172
+ end
173
+
174
+ private
175
+
176
+ def make_all_attributes
177
+ define_default_attrs
178
+ define_group_attrs
179
+ end
180
+
181
+ def definition_module_has_methods?(definition_module)
182
+ definition_module.instance_methods(false).any? ||
183
+ definition_module.private_instance_methods(false).any?
184
+ end
185
+
186
+ def define_associate(reflection)
187
+ key = reflection.options[:key] || reflection.name
188
+ writable_reflections[key] = reflection
189
+ end
190
+
191
+ def writable_attributes_data
192
+ @_attributes_data = _attributes_data.dup if _attributes_data.frozen?
193
+ _attributes_data
194
+ end
195
+
196
+ def writable_reflections
197
+ @_reflections = _reflections.dup if _reflections.frozen?
198
+ _reflections
199
+ end
70
200
  end
71
- end
201
+ end
@@ -1,3 +1,3 @@
1
1
  module Flexserializer
2
- VERSION = "1.3.0"
2
+ VERSION = "1.4.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flexserializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - woodcrust
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-02 00:00:00.000000000 Z
11
+ date: 2026-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers
@@ -80,7 +80,7 @@ homepage: https://github.com/woodcrust/flexserializer
80
80
  licenses:
81
81
  - MIT
82
82
  metadata: {}
83
- post_install_message:
83
+ post_install_message:
84
84
  rdoc_options: []
85
85
  require_paths:
86
86
  - lib
@@ -95,9 +95,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  requirements: []
98
- rubyforge_project:
99
- rubygems_version: 2.6.12
100
- signing_key:
98
+ rubygems_version: 3.5.22
99
+ signing_key:
101
100
  specification_version: 4
102
101
  summary: This is gem flexserializer
103
102
  test_files: []