flexserializer 1.3.1 → 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: 979100e56ed56a77928ecff0210714f950433fb8
4
- data.tar.gz: 9c132b27819571afebe6832993aefcc5958f5a7a
2
+ SHA256:
3
+ metadata.gz: c81ac4b55e3a744ecb9b87e3168bd3bf20ee991e3814509927beebe0d9330207
4
+ data.tar.gz: 92645fef924accb8d62627f80feed7fea1411eedf37b733127e2e9ecb5b632be
5
5
  SHA512:
6
- metadata.gz: 230c9d488326bf2bc667a76481bb17e4f5f9fb887d2d85185a60d6eb594a0327a22e0371ef606c370ad58491cd0ca08886bbb89a35959a02e7c02e202d87f740
7
- data.tar.gz: 728ccec0f83f07e06de5f04b79b5d9583e22a60ea03917512f5be7a95a48a090d7aa9dabcd7e8b8ab58ef2bd3c100fa6ee2270a4392704b400ce1d1ab69d7cb4
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,6 +62,32 @@ 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
 
@@ -23,10 +95,15 @@ module Flexserializer
23
95
 
24
96
  def initialize(object, options = {})
25
97
  super(object, options)
26
- @_attributes_data = {}
27
- @_reflections = {}
28
98
  @group_name = options[:group]
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,7 +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)
118
+ writable_attributes_data[key] = Attribute.new(attr, options, block)
42
119
  end
43
120
 
44
121
  def define_default_attrs
@@ -101,9 +178,24 @@ module Flexserializer
101
178
  define_group_attrs
102
179
  end
103
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
+
104
186
  def define_associate(reflection)
105
187
  key = reflection.options[:key] || reflection.name
106
- _reflections[key] = reflection
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
107
199
  end
108
200
  end
109
- end
201
+ end
@@ -1,3 +1,3 @@
1
1
  module Flexserializer
2
- VERSION = "1.3.1"
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.1
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: []