serega 0.42.0 → 0.51.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.
Files changed (129) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +124 -1207
  3. data/VERSION +1 -1
  4. data/lib/serega/attribute/serializer_reference.rb +27 -0
  5. data/lib/serega/attribute/value_code.rb +63 -0
  6. data/lib/serega/attribute.rb +173 -140
  7. data/lib/serega/attribute_normalizer.rb +236 -391
  8. data/lib/serega/attribute_normalizers/batch.rb +45 -0
  9. data/lib/serega/attribute_normalizers/block.rb +81 -0
  10. data/lib/serega/attribute_normalizers/delegate.rb +34 -0
  11. data/lib/serega/attribute_normalizers/hash_access.rb +37 -0
  12. data/lib/serega/attribute_normalizers/preloads.rb +47 -0
  13. data/lib/serega/attribute_value_resolvers/base.rb +35 -0
  14. data/lib/serega/attribute_value_resolvers/batch.rb +18 -54
  15. data/lib/serega/attribute_value_resolvers/callable.rb +52 -0
  16. data/lib/serega/attribute_value_resolvers/const.rb +12 -24
  17. data/lib/serega/attribute_value_resolvers/delegate.rb +27 -28
  18. data/lib/serega/attribute_value_resolvers/hash_access.rb +49 -52
  19. data/lib/serega/attribute_value_resolvers/keyword.rb +17 -25
  20. data/lib/serega/attribute_values.rb +32 -0
  21. data/lib/serega/batch_loader.rb +37 -0
  22. data/lib/serega/conditions.rb +81 -0
  23. data/lib/serega/config.rb +148 -148
  24. data/lib/serega/engine/pulls.rb +51 -0
  25. data/lib/serega/engine/run.rb +101 -0
  26. data/lib/serega/errors.rb +60 -8
  27. data/lib/serega/formatters/callable.rb +44 -0
  28. data/lib/serega/formatters/method.rb +55 -0
  29. data/lib/serega/formatters.rb +27 -0
  30. data/lib/serega/plan/cache.rb +119 -0
  31. data/lib/serega/plan/depth_limit.rb +36 -0
  32. data/lib/serega/plan/per_ractor_cache.rb +28 -0
  33. data/lib/serega/plan/point.rb +67 -0
  34. data/lib/serega/plan/presets.rb +116 -0
  35. data/lib/serega/plan/selection.rb +94 -0
  36. data/lib/serega/plan/selection_planner.rb +50 -0
  37. data/lib/serega/plan.rb +92 -133
  38. data/lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb +13 -80
  39. data/lib/serega/plugins/activerecord_preloads/lib/preloader.rb +8 -75
  40. data/lib/serega/plugins/context_metadata/context_metadata.rb +40 -123
  41. data/lib/serega/plugins/metadata/check_params.rb +80 -0
  42. data/lib/serega/plugins/metadata/meta_attribute.rb +53 -76
  43. data/lib/serega/plugins/metadata/metadata.rb +38 -175
  44. data/lib/serega/plugins/root/root.rb +94 -235
  45. data/lib/serega/plugins.rb +16 -49
  46. data/lib/serega/presenter.rb +81 -19
  47. data/lib/serega/result_builder.rb +68 -0
  48. data/lib/serega/result_code/attribute_point_code.rb +117 -0
  49. data/lib/serega/result_code/point_code.rb +54 -0
  50. data/lib/serega/result_code/relation_point_code.rb +112 -0
  51. data/lib/serega/result_code.rb +159 -0
  52. data/lib/serega/shareable.rb +52 -0
  53. data/lib/serega/source_group.rb +156 -0
  54. data/lib/serega/utils/camel_case.rb +22 -0
  55. data/lib/serega/utils/collection_detector.rb +20 -13
  56. data/lib/serega/utils/deep_merge.rb +26 -0
  57. data/lib/serega/utils/enum_deep_dup.rb +10 -16
  58. data/lib/serega/utils/enum_deep_freeze.rb +8 -14
  59. data/lib/serega/utils/method_signature.rb +6 -15
  60. data/lib/serega/utils/parse_string_selection.rb +63 -0
  61. data/lib/serega/utils/ractor_local.rb +36 -0
  62. data/lib/serega/utils/ruby_literal.rb +28 -0
  63. data/lib/serega/utils/serialized_attribute_error.rb +60 -20
  64. data/lib/serega/utils/to_hash.rb +5 -26
  65. data/lib/serega/validations/attribute/check_block.rb +3 -15
  66. data/lib/serega/validations/attribute/check_name.rb +4 -15
  67. data/lib/serega/validations/attribute/check_opt_base_serializer.rb +3 -15
  68. data/lib/serega/validations/attribute/check_opt_batch.rb +26 -56
  69. data/lib/serega/validations/attribute/check_opt_condition.rb +55 -0
  70. data/lib/serega/validations/attribute/check_opt_delegate.rb +10 -63
  71. data/lib/serega/validations/attribute/check_opt_format.rb +36 -0
  72. data/lib/serega/validations/attribute/check_opt_hash_access.rb +11 -59
  73. data/lib/serega/validations/attribute/check_opt_hide.rb +3 -14
  74. data/lib/serega/validations/attribute/check_opt_many.rb +18 -20
  75. data/lib/serega/validations/attribute/check_opt_method.rb +3 -25
  76. data/lib/serega/validations/attribute/check_opt_preload.rb +3 -26
  77. data/lib/serega/validations/attribute/check_opt_preset.rb +34 -0
  78. data/lib/serega/validations/attribute/check_opt_selection.rb +41 -0
  79. data/lib/serega/validations/attribute/check_opt_serializer.rb +4 -18
  80. data/lib/serega/validations/attribute/check_opt_value.rb +3 -21
  81. data/lib/serega/validations/check_attribute_params.rb +92 -71
  82. data/lib/serega/validations/check_batch_loader_params.rb +43 -62
  83. data/lib/serega/validations/check_definition_selection.rb +21 -0
  84. data/lib/serega/validations/check_formatter.rb +80 -0
  85. data/lib/serega/validations/check_preset_params.rb +103 -0
  86. data/lib/serega/validations/check_relation_selection.rb +31 -0
  87. data/lib/serega/validations/check_selection.rb +89 -0
  88. data/lib/serega/validations/check_serialize_keywords.rb +23 -0
  89. data/lib/serega/validations/utils/check_allowed_keys.rb +3 -13
  90. data/lib/serega/validations/utils/check_hash_access_value.rb +44 -0
  91. data/lib/serega/validations/utils/check_opt_is_bool.rb +3 -13
  92. data/lib/serega/validations/utils/check_opt_is_hash.rb +3 -13
  93. data/lib/serega/validations/utils/check_opt_is_string_or_symbol.rb +3 -13
  94. data/lib/serega/version.rb +1 -4
  95. data/lib/serega.rb +525 -390
  96. data/sig/serega/plugins.rbs +49 -0
  97. data/sig/serega.rbs +158 -0
  98. metadata +64 -40
  99. data/lib/serega/data_builder.rb +0 -54
  100. data/lib/serega/engine/level.rb +0 -59
  101. data/lib/serega/engine/level_queue.rb +0 -56
  102. data/lib/serega/engine/loader.rb +0 -70
  103. data/lib/serega/helpers/serializer_class_helper.rb +0 -19
  104. data/lib/serega/object_serializer.rb +0 -125
  105. data/lib/serega/plan_point.rb +0 -165
  106. data/lib/serega/plugins/camel_case/camel_case.rb +0 -213
  107. data/lib/serega/plugins/depth_limit/depth_limit.rb +0 -206
  108. data/lib/serega/plugins/explicit_many_option/explicit_many_option.rb +0 -69
  109. data/lib/serega/plugins/explicit_many_option/validations/check_opt_many.rb +0 -38
  110. data/lib/serega/plugins/formatters/formatters.rb +0 -322
  111. data/lib/serega/plugins/if/if.rb +0 -287
  112. data/lib/serega/plugins/if/validations/check_opt_if.rb +0 -72
  113. data/lib/serega/plugins/if/validations/check_opt_if_value.rb +0 -77
  114. data/lib/serega/plugins/if/validations/check_opt_unless.rb +0 -72
  115. data/lib/serega/plugins/if/validations/check_opt_unless_value.rb +0 -77
  116. data/lib/serega/plugins/metadata/validations/check_block.rb +0 -47
  117. data/lib/serega/plugins/metadata/validations/check_opt_const.rb +0 -39
  118. data/lib/serega/plugins/metadata/validations/check_opt_hide_empty.rb +0 -35
  119. data/lib/serega/plugins/metadata/validations/check_opt_hide_nil.rb +0 -35
  120. data/lib/serega/plugins/metadata/validations/check_opt_value.rb +0 -61
  121. data/lib/serega/plugins/metadata/validations/check_opts.rb +0 -59
  122. data/lib/serega/plugins/metadata/validations/check_path.rb +0 -50
  123. data/lib/serega/plugins/string_modifiers/parse_string_modifiers.rb +0 -111
  124. data/lib/serega/plugins/string_modifiers/string_modifiers.rb +0 -63
  125. data/lib/serega/utils/symbol_name.rb +0 -39
  126. data/lib/serega/validations/attribute/check_opt_const.rb +0 -40
  127. data/lib/serega/validations/check_initiate_params.rb +0 -57
  128. data/lib/serega/validations/check_serialize_params.rb +0 -54
  129. data/lib/serega/validations/initiate/check_modifiers.rb +0 -93
@@ -0,0 +1,49 @@
1
+ # Plugins add these modules when they are loaded, thus a type checker does
2
+ # not see them on serializers and configs.
3
+ class Serega
4
+ module SeregaPlugins
5
+ module ActiverecordPreloads
6
+ end
7
+
8
+ module ContextMetadata
9
+ module ConfigInstanceMethods
10
+ def context_metadata: () -> ContextMetadataConfig
11
+ end
12
+
13
+ class ContextMetadataConfig
14
+ def initialize: (Hash[Symbol, untyped] opts, Array[Symbol] serialize_keys) -> void
15
+
16
+ def key: () -> Symbol?
17
+ def key=: (Symbol? new_key) -> Symbol?
18
+ end
19
+ end
20
+
21
+ module Metadata
22
+ module ClassMethods
23
+ def meta_attribute: (*Symbol | String path, **untyped opts) ?{ (untyped objects, context context) -> untyped } -> void
24
+ end
25
+ end
26
+
27
+ module Root
28
+ type key = Symbol | String | nil
29
+
30
+ module ClassMethods
31
+ def root: (?key root, ?one: key, ?many: key) -> void
32
+ end
33
+
34
+ class RootConfig
35
+ def initialize: (Hash[Symbol, untyped] opts) -> void
36
+
37
+ def one: () -> key
38
+ def one=: (key value) -> key
39
+ def many: () -> key
40
+ def many=: (key value) -> key
41
+ end
42
+
43
+ module ConfigInstanceMethods
44
+ def root: () -> RootConfig
45
+ def root=: ({ one: key, many: key } value) -> void
46
+ end
47
+ end
48
+ end
49
+ end
data/sig/serega.rbs ADDED
@@ -0,0 +1,158 @@
1
+ class Serega
2
+ VERSION: String
3
+
4
+ # Fields of the `only:`, `except:` and `with:` options
5
+ type fields = Symbol | String | Array[fields] | Hash[Symbol | String, fields]
6
+
7
+ # Names of the `preset:` option
8
+ type preset_names = Symbol | String | Array[Symbol | String]
9
+
10
+ # Definition of a preset: the presets it includes and its own field selection
11
+ type preset_definition = { presets: Array[Symbol], only: Hash[Symbol, untyped], with: Hash[Symbol, untyped], except: Hash[Symbol, untyped] }
12
+
13
+ # Serialization context
14
+ type context = Hash[untyped, untyped]
15
+
16
+ extend ClassMethods
17
+ include InstanceMethods
18
+
19
+ module ClassMethods
20
+ attr_reader config: SeregaConfig
21
+
22
+ def plugin: (Symbol name, **untyped opts) -> Module
23
+
24
+ def attributes: () -> Hash[Symbol, SeregaAttribute]
25
+
26
+ def attribute: (Symbol | String name, **untyped opts) ?{ () [self: singleton(Serega)] -> void } -> SeregaAttribute
27
+
28
+ def presets: () -> Hash[Symbol, preset_definition]
29
+
30
+ def preset: (Symbol | String name, ?preset: preset_names?, ?only: fields?, ?with: fields?, ?except: fields?) -> preset_definition
31
+
32
+ def batch: (Symbol | String name, untyped loader) -> void
33
+ | (Symbol | String name) { (untyped objects) -> untyped } -> void
34
+ | (Symbol | String name) { (untyped objects, context context) -> untyped } -> void
35
+ | (Symbol | String name) { (untyped objects, ctx: context) -> untyped } -> void
36
+
37
+ def formatter: (Symbol name, untyped formatter, ?allow_nil: bool) -> void
38
+ | (Symbol name, ?allow_nil: bool) { (untyped value) -> untyped } -> void
39
+ | (Symbol name, ?allow_nil: bool) { (untyped value, context context) -> untyped } -> void
40
+ | (Symbol name, ?allow_nil: bool) { (untyped value, ctx: context) -> untyped } -> void
41
+
42
+ def presenter: () -> singleton(SeregaPresenter)?
43
+ | () { () [self: singleton(SeregaPresenter)] -> void } -> singleton(SeregaPresenter)
44
+
45
+ def preload_with: () -> untyped
46
+ | (untyped handler) -> untyped
47
+ | () { (untyped objects, untyped preloads) -> void } -> untyped
48
+
49
+ def prepare_initial_objects: () -> untyped
50
+ | (untyped handler) -> untyped
51
+ | () { (untyped objects) -> untyped } -> untyped
52
+ | () { (untyped objects, context context) -> untyped } -> untyped
53
+ | () { (untyped objects, ctx: context) -> untyped } -> untyped
54
+
55
+ # Results are `untyped`: plugins change their shape. Options that plugins
56
+ # add at load time, such as `root:`, are not in these signatures.
57
+ def call: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?preset: preset_names?, ?check_selection: bool?, ?context: context?, ?many: bool?) -> untyped
58
+ alias to_h call
59
+ def to_data: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?preset: preset_names?, ?check_selection: bool?, ?context: context?, ?many: bool?) -> untyped
60
+ def to_struct: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?preset: preset_names?, ?check_selection: bool?, ?context: context?, ?many: bool?) -> untyped
61
+ def to_json: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?preset: preset_names?, ?check_selection: bool?, ?context: context?, ?many: bool?) -> String
62
+
63
+ def make_shareable!: () -> self
64
+ end
65
+
66
+ module InstanceMethods
67
+ def initialize: (?only: fields?, ?with: fields?, ?except: fields?, ?preset: preset_names?, ?check_selection: bool?) -> void
68
+
69
+ def call: (untyped object, ?context: context?, ?many: bool?) -> untyped
70
+ alias to_h call
71
+ def to_data: (untyped object, ?context: context?, ?many: bool?) -> untyped
72
+ def to_struct: (untyped object, ?context: context?, ?many: bool?) -> untyped
73
+ def to_json: (untyped object, ?context: context?, ?many: bool?) -> String
74
+ end
75
+
76
+ # Attribute of a serializer. Its methods are internal.
77
+ class SeregaAttribute
78
+ def initialize: (singleton(Serega) serializer_class, name: Symbol | String, ?opts: Hash[Symbol, untyped], ?block: Proc?, ?location: String?) -> void
79
+ end
80
+
81
+ # Parent class of presenters. Its methods are internal.
82
+ class SeregaPresenter
83
+ def initialize: (untyped object, ?context? ctx) -> void
84
+ end
85
+
86
+ class SeregaError < StandardError
87
+ end
88
+
89
+ class AttributeNotExist < SeregaError
90
+ def initialize: (?String? message, ?singleton(Serega)? serializer, ?Array[String]? attributes) -> void
91
+
92
+ attr_reader serializer: singleton(Serega)?
93
+ attr_reader attributes: Array[String]?
94
+ end
95
+
96
+ class DepthLimitError < SeregaError
97
+ def initialize: (?String? message, ?String? details) -> void
98
+
99
+ attr_reader details: String?
100
+ end
101
+
102
+ class SeregaConfig
103
+ include SeregaConfigInstanceMethods
104
+
105
+ module SeregaConfigInstanceMethods
106
+ def check_selection: () -> bool
107
+ def check_selection=: (bool value) -> bool
108
+
109
+ def check_attribute_name: () -> bool
110
+ def check_attribute_name=: (bool value) -> bool
111
+
112
+ def require_many: () -> bool
113
+ def require_many=: (bool value) -> bool
114
+
115
+ def delegate_default_allow_nil: () -> bool
116
+ def delegate_default_allow_nil=: (bool value) -> bool
117
+
118
+ def compact: () -> bool
119
+ def compact=: (bool value) -> bool
120
+
121
+ def key_transform: () -> (:camel_case | ^(Symbol) -> (Symbol | String) | nil)
122
+ def key_transform=: (:camel_case | ^(Symbol) -> (Symbol | String) | nil value) -> (:camel_case | ^(Symbol) -> (Symbol | String) | nil)
123
+
124
+ def hide_by_default: () -> (bool | :auto)
125
+ def hide_by_default=: (bool | :auto value) -> (bool | :auto)
126
+
127
+ def base_serializer: () -> singleton(Serega)?
128
+ def base_serializer=: (singleton(Serega) value) -> singleton(Serega)
129
+
130
+ def depth_limit: () -> Integer?
131
+ def depth_limit=: (Integer? value) -> Integer?
132
+
133
+ def max_cached_plans_per_serializer_count: () -> Integer
134
+ def max_cached_plans_per_serializer_count=: (Integer value) -> Integer
135
+
136
+ def batch_id_option: () -> Symbol
137
+ def batch_id_option=: (Symbol value) -> Symbol
138
+
139
+ def auto_preload: () -> { has_delegate_option: bool, has_serializer_option: bool }
140
+ def auto_preload=: (bool | { ?has_delegate_option: bool, ?has_serializer_option: bool } value) -> { has_delegate_option: bool, has_serializer_option: bool }
141
+
142
+ def auto_preload_excluded_methods: () -> Array[Symbol]
143
+ def auto_preload_excluded_methods=: (Array[Symbol] value) -> Array[Symbol]
144
+
145
+ def hash_access: () -> HashAccessConfig
146
+ end
147
+
148
+ class HashAccessConfig
149
+ def initialize: (Hash[Symbol, untyped] opts) -> void
150
+
151
+ def default_mode: () -> (:symbol | :string)
152
+ def default_mode=: (:symbol | :string value) -> (:symbol | :string)
153
+
154
+ def default_allow_missing_key: () -> bool
155
+ def default_allow_missing_key=: (bool value) -> bool
156
+ end
157
+ end
158
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serega
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.42.0
4
+ version: 0.51.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey
@@ -11,115 +11,139 @@ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: Serializes Ruby objects into Hashes, ready for further conversion to
13
13
  JSON or other formats, with a simple DSL for selecting fields and solving N+1 queries
14
- through batch loading. Extendable through a Roda/Shrine-style plugin system, with
15
- built-in plugins for presenters, formatters, camel_case keys, depth limiting, and
16
- more. No runtime dependencies.
14
+ through batch loading. No runtime dependencies.
17
15
  email:
18
16
  - andreyruby@yandex.ru
19
17
  executables: []
20
18
  extensions: []
21
- extra_rdoc_files: []
19
+ extra_rdoc_files:
20
+ - README.md
22
21
  files:
23
22
  - LICENSE.txt
24
23
  - README.md
25
24
  - VERSION
26
25
  - lib/serega.rb
27
26
  - lib/serega/attribute.rb
27
+ - lib/serega/attribute/serializer_reference.rb
28
+ - lib/serega/attribute/value_code.rb
28
29
  - lib/serega/attribute_normalizer.rb
30
+ - lib/serega/attribute_normalizers/batch.rb
31
+ - lib/serega/attribute_normalizers/block.rb
32
+ - lib/serega/attribute_normalizers/delegate.rb
33
+ - lib/serega/attribute_normalizers/hash_access.rb
34
+ - lib/serega/attribute_normalizers/preloads.rb
35
+ - lib/serega/attribute_value_resolvers/base.rb
29
36
  - lib/serega/attribute_value_resolvers/batch.rb
37
+ - lib/serega/attribute_value_resolvers/callable.rb
30
38
  - lib/serega/attribute_value_resolvers/const.rb
31
39
  - lib/serega/attribute_value_resolvers/delegate.rb
32
40
  - lib/serega/attribute_value_resolvers/hash_access.rb
33
41
  - lib/serega/attribute_value_resolvers/keyword.rb
42
+ - lib/serega/attribute_values.rb
43
+ - lib/serega/batch_loader.rb
44
+ - lib/serega/conditions.rb
34
45
  - lib/serega/config.rb
35
- - lib/serega/data_builder.rb
36
- - lib/serega/engine/level.rb
37
- - lib/serega/engine/level_queue.rb
38
- - lib/serega/engine/loader.rb
46
+ - lib/serega/engine/pulls.rb
47
+ - lib/serega/engine/run.rb
39
48
  - lib/serega/errors.rb
40
- - lib/serega/helpers/serializer_class_helper.rb
41
- - lib/serega/object_serializer.rb
49
+ - lib/serega/formatters.rb
50
+ - lib/serega/formatters/callable.rb
51
+ - lib/serega/formatters/method.rb
42
52
  - lib/serega/plan.rb
43
- - lib/serega/plan_point.rb
53
+ - lib/serega/plan/cache.rb
54
+ - lib/serega/plan/depth_limit.rb
55
+ - lib/serega/plan/per_ractor_cache.rb
56
+ - lib/serega/plan/point.rb
57
+ - lib/serega/plan/presets.rb
58
+ - lib/serega/plan/selection.rb
59
+ - lib/serega/plan/selection_planner.rb
44
60
  - lib/serega/plugins.rb
45
61
  - lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb
46
62
  - lib/serega/plugins/activerecord_preloads/lib/preloader.rb
47
- - lib/serega/plugins/camel_case/camel_case.rb
48
63
  - lib/serega/plugins/context_metadata/context_metadata.rb
49
- - lib/serega/plugins/depth_limit/depth_limit.rb
50
- - lib/serega/plugins/explicit_many_option/explicit_many_option.rb
51
- - lib/serega/plugins/explicit_many_option/validations/check_opt_many.rb
52
- - lib/serega/plugins/formatters/formatters.rb
53
- - lib/serega/plugins/if/if.rb
54
- - lib/serega/plugins/if/validations/check_opt_if.rb
55
- - lib/serega/plugins/if/validations/check_opt_if_value.rb
56
- - lib/serega/plugins/if/validations/check_opt_unless.rb
57
- - lib/serega/plugins/if/validations/check_opt_unless_value.rb
64
+ - lib/serega/plugins/metadata/check_params.rb
58
65
  - lib/serega/plugins/metadata/meta_attribute.rb
59
66
  - lib/serega/plugins/metadata/metadata.rb
60
- - lib/serega/plugins/metadata/validations/check_block.rb
61
- - lib/serega/plugins/metadata/validations/check_opt_const.rb
62
- - lib/serega/plugins/metadata/validations/check_opt_hide_empty.rb
63
- - lib/serega/plugins/metadata/validations/check_opt_hide_nil.rb
64
- - lib/serega/plugins/metadata/validations/check_opt_value.rb
65
- - lib/serega/plugins/metadata/validations/check_opts.rb
66
- - lib/serega/plugins/metadata/validations/check_path.rb
67
67
  - lib/serega/plugins/root/root.rb
68
- - lib/serega/plugins/string_modifiers/parse_string_modifiers.rb
69
- - lib/serega/plugins/string_modifiers/string_modifiers.rb
70
68
  - lib/serega/presenter.rb
69
+ - lib/serega/result_builder.rb
70
+ - lib/serega/result_code.rb
71
+ - lib/serega/result_code/attribute_point_code.rb
72
+ - lib/serega/result_code/point_code.rb
73
+ - lib/serega/result_code/relation_point_code.rb
74
+ - lib/serega/shareable.rb
75
+ - lib/serega/source_group.rb
76
+ - lib/serega/utils/camel_case.rb
71
77
  - lib/serega/utils/collection_detector.rb
78
+ - lib/serega/utils/deep_merge.rb
72
79
  - lib/serega/utils/enum_deep_dup.rb
73
80
  - lib/serega/utils/enum_deep_freeze.rb
74
81
  - lib/serega/utils/method_signature.rb
82
+ - lib/serega/utils/parse_string_selection.rb
83
+ - lib/serega/utils/ractor_local.rb
84
+ - lib/serega/utils/ruby_literal.rb
75
85
  - lib/serega/utils/serialized_attribute_error.rb
76
- - lib/serega/utils/symbol_name.rb
77
86
  - lib/serega/utils/to_hash.rb
78
87
  - lib/serega/validations/attribute/check_block.rb
79
88
  - lib/serega/validations/attribute/check_name.rb
80
89
  - lib/serega/validations/attribute/check_opt_base_serializer.rb
81
90
  - lib/serega/validations/attribute/check_opt_batch.rb
82
- - lib/serega/validations/attribute/check_opt_const.rb
91
+ - lib/serega/validations/attribute/check_opt_condition.rb
83
92
  - lib/serega/validations/attribute/check_opt_delegate.rb
93
+ - lib/serega/validations/attribute/check_opt_format.rb
84
94
  - lib/serega/validations/attribute/check_opt_hash_access.rb
85
95
  - lib/serega/validations/attribute/check_opt_hide.rb
86
96
  - lib/serega/validations/attribute/check_opt_many.rb
87
97
  - lib/serega/validations/attribute/check_opt_method.rb
88
98
  - lib/serega/validations/attribute/check_opt_preload.rb
99
+ - lib/serega/validations/attribute/check_opt_preset.rb
100
+ - lib/serega/validations/attribute/check_opt_selection.rb
89
101
  - lib/serega/validations/attribute/check_opt_serializer.rb
90
102
  - lib/serega/validations/attribute/check_opt_value.rb
91
103
  - lib/serega/validations/check_attribute_params.rb
92
104
  - lib/serega/validations/check_batch_loader_params.rb
93
- - lib/serega/validations/check_initiate_params.rb
94
- - lib/serega/validations/check_serialize_params.rb
95
- - lib/serega/validations/initiate/check_modifiers.rb
105
+ - lib/serega/validations/check_definition_selection.rb
106
+ - lib/serega/validations/check_formatter.rb
107
+ - lib/serega/validations/check_preset_params.rb
108
+ - lib/serega/validations/check_relation_selection.rb
109
+ - lib/serega/validations/check_selection.rb
110
+ - lib/serega/validations/check_serialize_keywords.rb
96
111
  - lib/serega/validations/utils/check_allowed_keys.rb
112
+ - lib/serega/validations/utils/check_hash_access_value.rb
97
113
  - lib/serega/validations/utils/check_opt_is_bool.rb
98
114
  - lib/serega/validations/utils/check_opt_is_hash.rb
99
115
  - lib/serega/validations/utils/check_opt_is_string_or_symbol.rb
100
116
  - lib/serega/version.rb
117
+ - sig/serega.rbs
118
+ - sig/serega/plugins.rbs
101
119
  homepage: https://github.com/andreyruby/serega
102
120
  licenses:
103
121
  - MIT
104
122
  metadata:
105
123
  source_code_uri: https://github.com/andreyruby/serega
106
- documentation_uri: https://www.rubydoc.info/gems/serega
124
+ documentation_uri: https://andreyruby.github.io/serega/
107
125
  changelog_uri: https://github.com/andreyruby/serega/blob/master/CHANGELOG.md
108
- rdoc_options: []
126
+ rdoc_options:
127
+ - "--markup"
128
+ - markdown
129
+ - "--main"
130
+ - README.md
131
+ - "--title"
132
+ - Serega
109
133
  require_paths:
110
134
  - lib
111
135
  required_ruby_version: !ruby/object:Gem::Requirement
112
136
  requirements:
113
137
  - - ">="
114
138
  - !ruby/object:Gem::Version
115
- version: 3.2.0
139
+ version: 3.3.0
116
140
  required_rubygems_version: !ruby/object:Gem::Requirement
117
141
  requirements:
118
142
  - - ">="
119
143
  - !ruby/object:Gem::Version
120
144
  version: '0'
121
145
  requirements: []
122
- rubygems_version: 4.0.13
146
+ rubygems_version: 4.0.23
123
147
  specification_version: 4
124
148
  summary: Ruby Serializer
125
149
  test_files: []
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Serega
4
- #
5
- # Builds Data objects from serialized hashes.
6
- # Used by `#to_data` / `.to_data`.
7
- #
8
- # @private
9
- class SeregaDataBuilder
10
- # SeregaDataBuilder class methods
11
- # @private
12
- module SeregaDataBuilderClassMethods
13
- #
14
- # Converts serialized Hash/Array result into a tree of Ruby Data objects
15
- # following the serializer's plan structure.
16
- #
17
- # @param serializer [Serega] Serializer instance carrying the plan
18
- # @param serialized [Hash, Array, nil] Serialized output from `#to_h`
19
- #
20
- # @return [Data, Array<Data>, nil] Serialization result as Data object(s)
21
- #
22
- def call(serializer, serialized)
23
- build(serialized, serializer.plan)
24
- end
25
-
26
- private
27
-
28
- def build(serialized, plan)
29
- case serialized
30
- when Array then serialized.map { |item| hash_to_data(item, plan) }
31
- when Hash then hash_to_data(serialized, plan)
32
- else serialized
33
- end
34
- end
35
-
36
- def hash_to_data(hash, plan)
37
- hash_data = hash.to_h do |key, value|
38
- child_plan = plan.points_hash.fetch(key).child_plan
39
- value = build(value, child_plan) if child_plan
40
- [key, value]
41
- end
42
-
43
- build_data_object(plan, hash_data)
44
- end
45
-
46
- def build_data_object(plan, hash_data)
47
- plan.data_class.new(**hash_data)
48
- end
49
- end
50
-
51
- extend SeregaHelpers::SerializerClassHelper
52
- extend SeregaDataBuilderClassMethods
53
- end
54
- end
@@ -1,59 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Serega
4
- #
5
- # Batch feature main module
6
- #
7
- # @private
8
- module SeregaEngine
9
- #
10
- # One serialization level: all objects serialized under a single plan and the
11
- # result containers they fill. Objects from different parents that share a plan
12
- # (e.g. every user's posts) accumulate into one level, so a named batch loader
13
- # or preload runs once over the whole set. A deeper level (its own plan) loads
14
- # separately.
15
- #
16
- # @private
17
- class Level
18
- # @param serializer [SeregaObjectSerializer] serializer that resolves this level
19
- def initialize(serializer)
20
- @serializer = serializer
21
- @objects = []
22
- @containers = []
23
- @results = {}.compare_by_identity
24
- end
25
-
26
- # @return [Array] Objects serialized at this level
27
- attr_reader :objects
28
-
29
- # @return [Array<Hash>] Result container per object (filled in place)
30
- attr_reader :containers
31
-
32
- # Accumulates a chunk of objects into this level, creating one empty result
33
- # container per object. The containers are returned so the caller can hand
34
- # them to its parent; they are filled in place later during #process.
35
- #
36
- # @param objects [Array] objects serialized at this level
37
- # @return [Array<Hash>] the created containers, aligned with objects
38
- def add(objects)
39
- containers = Array.new(objects.size) { {} }
40
- @objects.concat(objects)
41
- @containers.concat(containers)
42
- containers
43
- end
44
-
45
- # Resolves every attribute of this level onto the containers.
46
- # @return [void]
47
- def process
48
- @serializer.process(self)
49
- end
50
-
51
- # Loads a named batch loader once for this level's objects.
52
- # @param loader [SeregaEngine::Loader] Named batch loader
53
- # @return [Object] Loaded values
54
- def fetch(loader)
55
- @results[loader] ||= loader.load(@objects, @serializer.context)
56
- end
57
- end
58
- end
59
- end
@@ -1,56 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Serega
4
- #
5
- # Batch feature main module
6
- #
7
- # @private
8
- module SeregaEngine
9
- #
10
- # Level-order queue of serialization levels for one serialization run.
11
- #
12
- # `#serialize` on the object serializer enqueues a level (objects + their result
13
- # containers) instead of resolving values inline. `#run` then processes the
14
- # queue: each level resolves its attributes and, for relations, enqueues child
15
- # levels — which the loop picks up because it re-reads the size each iteration.
16
- # Processing level-by-level lets a named batch loader and preloads run once per
17
- # level over all of its objects.
18
- #
19
- # @private
20
- class LevelQueue
21
- def initialize
22
- @levels = []
23
- @levels_by_plan = {}.compare_by_identity
24
- end
25
-
26
- # Adds objects to the level for their plan (creating it on first use), so
27
- # objects serialized under the same plan — even from different parents —
28
- # share one level and load once. The level creates a result container per
29
- # object and returns them for the caller to embed in its parent.
30
- #
31
- # @param serializer [SeregaObjectSerializer] serializer that resolves the level
32
- # @param objects [Array] objects serialized at this level
33
- #
34
- # @return [Array<Hash>] the created result containers, aligned with objects
35
- def enqueue(serializer, objects)
36
- level = @levels_by_plan[serializer.plan]
37
- unless level
38
- level = Level.new(serializer)
39
- @levels_by_plan[serializer.plan] = level
40
- @levels << level
41
- end
42
- level.add(objects)
43
- end
44
-
45
- # Processes every level, including child levels enqueued while processing.
46
- # @return [void]
47
- def run
48
- i = 0
49
- while i < @levels.size
50
- @levels[i].process
51
- i += 1
52
- end
53
- end
54
- end
55
- end
56
- end
@@ -1,70 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Serega
4
- #
5
- # Batch feature main module
6
- #
7
- # @private
8
- module SeregaEngine
9
- #
10
- # Batch loader
11
- #
12
- # @private
13
- class Loader
14
- #
15
- # BatchLoader instance methods
16
- #
17
- # @private
18
- module InstanceMethods
19
- # BatchLoader initial params
20
- # @return [Hash] BatchLoader initial params
21
- attr_reader :initials
22
-
23
- # BatchLoader name
24
- # @return [Symbol] BatchLoader name
25
- attr_reader :name
26
-
27
- # BatchLoader block
28
- # @return [#call] BatchLoader block
29
- attr_reader :block
30
-
31
- #
32
- # Initializes new batch loader
33
- #
34
- # @param name [Symbol, String] Name of attribute
35
- # @param block [#call] BatchLoader block
36
- #
37
- def initialize(name:, block:)
38
- serializer_class = self.class.serializer_class
39
- serializer_class::CheckBatchLoaderParams.new(name, block).validate
40
-
41
- @initials = SeregaUtils::EnumDeepFreeze.call(name: name, block: block)
42
- @name = name.to_sym
43
- @block = block
44
- @signature = SeregaUtils::MethodSignature.call(block, pos_limit: 2, keyword_args: [:ctx])
45
- end
46
-
47
- # Serializes values for objects
48
- # @param objects [Array] Serialized objects
49
- # @param context [Hash] Serialization context
50
- # @return [void]
51
- def load(objects, context)
52
- case signature
53
- when "1" then block.call(objects)
54
- when "2" then block.call(objects, context)
55
- else block.call(objects, ctx: context) # "1_ctx"
56
- end
57
- end
58
-
59
- private
60
-
61
- # BatchLoader block signature
62
- # @return [String] BatchLoader block signature
63
- attr_reader :signature
64
- end
65
-
66
- extend SeregaHelpers::SerializerClassHelper
67
- include InstanceMethods
68
- end
69
- end
70
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Serega
4
- #
5
- # Helpers
6
- #
7
- # @private
8
- module SeregaHelpers
9
- #
10
- # Stores link to current serializer class
11
- #
12
- # @private
13
- module SerializerClassHelper
14
- # Shows serializer class current class is namespaced under
15
- # @return [Class<Serega>] Serializer class that current class is namespaced under.
16
- attr_accessor :serializer_class
17
- end
18
- end
19
- end