serega 0.42.0 → 0.50.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 +4 -4
- data/README.md +124 -1207
- data/VERSION +1 -1
- data/lib/serega/attribute.rb +186 -131
- data/lib/serega/attribute_normalizer.rb +164 -345
- data/lib/serega/attribute_normalizers/batch.rb +45 -0
- data/lib/serega/attribute_normalizers/block.rb +81 -0
- data/lib/serega/attribute_normalizers/delegate.rb +34 -0
- data/lib/serega/attribute_normalizers/hash_access.rb +37 -0
- data/lib/serega/attribute_normalizers/preloads.rb +46 -0
- data/lib/serega/attribute_value_resolvers/batch.rb +5 -54
- data/lib/serega/attribute_value_resolvers/const.rb +2 -24
- data/lib/serega/attribute_value_resolvers/delegate.rb +17 -28
- data/lib/serega/attribute_value_resolvers/hash_access.rb +34 -52
- data/lib/serega/attribute_value_resolvers/keyword.rb +10 -25
- data/lib/serega/attribute_values.rb +46 -0
- data/lib/serega/batch_loader.rb +37 -0
- data/lib/serega/conditions.rb +56 -0
- data/lib/serega/config.rb +105 -144
- data/lib/serega/engine/pulls.rb +64 -0
- data/lib/serega/engine/run.rb +106 -0
- data/lib/serega/errors.rb +36 -8
- data/lib/serega/formatter.rb +98 -0
- data/lib/serega/plan/cache.rb +96 -0
- data/lib/serega/plan/per_ractor_cache.rb +23 -0
- data/lib/serega/plan.rb +101 -128
- data/lib/serega/plan_point.rb +44 -151
- data/lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb +13 -80
- data/lib/serega/plugins/activerecord_preloads/lib/preloader.rb +8 -75
- data/lib/serega/plugins/context_metadata/context_metadata.rb +40 -123
- data/lib/serega/plugins/metadata/check_params.rb +80 -0
- data/lib/serega/plugins/metadata/meta_attribute.rb +40 -79
- data/lib/serega/plugins/metadata/metadata.rb +40 -168
- data/lib/serega/plugins/root/root.rb +91 -235
- data/lib/serega/plugins.rb +16 -49
- data/lib/serega/presenter.rb +61 -19
- data/lib/serega/result_builder.rb +68 -0
- data/lib/serega/result_code.rb +203 -0
- data/lib/serega/shareable.rb +52 -0
- data/lib/serega/source_group.rb +187 -0
- data/lib/serega/utils/collection_detector.rb +19 -13
- data/lib/serega/utils/deep_merge.rb +26 -0
- data/lib/serega/utils/enum_deep_dup.rb +10 -16
- data/lib/serega/utils/enum_deep_freeze.rb +8 -14
- data/lib/serega/utils/method_signature.rb +6 -15
- data/lib/serega/utils/parse_string_modifiers.rb +63 -0
- data/lib/serega/utils/ractor_local.rb +36 -0
- data/lib/serega/utils/serialized_attribute_error.rb +60 -20
- data/lib/serega/utils/to_hash.rb +5 -26
- data/lib/serega/validations/attribute/check_block.rb +3 -15
- data/lib/serega/validations/attribute/check_name.rb +4 -15
- data/lib/serega/validations/attribute/check_opt_base_serializer.rb +3 -15
- data/lib/serega/validations/attribute/check_opt_batch.rb +26 -56
- data/lib/serega/validations/attribute/check_opt_condition.rb +55 -0
- data/lib/serega/validations/attribute/check_opt_delegate.rb +10 -63
- data/lib/serega/validations/attribute/check_opt_format.rb +36 -0
- data/lib/serega/validations/attribute/check_opt_hash_access.rb +11 -59
- data/lib/serega/validations/attribute/check_opt_hide.rb +3 -14
- data/lib/serega/validations/attribute/check_opt_many.rb +18 -20
- data/lib/serega/validations/attribute/check_opt_method.rb +3 -25
- data/lib/serega/validations/attribute/check_opt_preload.rb +3 -26
- data/lib/serega/validations/attribute/check_opt_serializer.rb +4 -18
- data/lib/serega/validations/attribute/check_opt_value.rb +3 -21
- data/lib/serega/validations/check_attribute_params.rb +92 -71
- data/lib/serega/validations/check_batch_loader_params.rb +43 -62
- data/lib/serega/validations/check_formatter.rb +80 -0
- data/lib/serega/validations/check_serialize_keywords.rb +23 -0
- data/lib/serega/validations/initiate/check_modifiers.rb +14 -20
- data/lib/serega/validations/utils/check_allowed_keys.rb +3 -13
- data/lib/serega/validations/utils/check_hash_access_value.rb +44 -0
- data/lib/serega/validations/utils/check_opt_is_bool.rb +3 -13
- data/lib/serega/validations/utils/check_opt_is_hash.rb +3 -13
- data/lib/serega/validations/utils/check_opt_is_string_or_symbol.rb +3 -13
- data/lib/serega/version.rb +1 -4
- data/lib/serega.rb +459 -388
- data/sig/serega/plugins.rbs +49 -0
- data/sig/serega.rbs +142 -0
- metadata +42 -38
- data/lib/serega/data_builder.rb +0 -54
- data/lib/serega/engine/level.rb +0 -59
- data/lib/serega/engine/level_queue.rb +0 -56
- data/lib/serega/engine/loader.rb +0 -70
- data/lib/serega/helpers/serializer_class_helper.rb +0 -19
- data/lib/serega/object_serializer.rb +0 -125
- data/lib/serega/plugins/camel_case/camel_case.rb +0 -213
- data/lib/serega/plugins/depth_limit/depth_limit.rb +0 -206
- data/lib/serega/plugins/explicit_many_option/explicit_many_option.rb +0 -69
- data/lib/serega/plugins/explicit_many_option/validations/check_opt_many.rb +0 -38
- data/lib/serega/plugins/formatters/formatters.rb +0 -322
- data/lib/serega/plugins/if/if.rb +0 -287
- data/lib/serega/plugins/if/validations/check_opt_if.rb +0 -72
- data/lib/serega/plugins/if/validations/check_opt_if_value.rb +0 -77
- data/lib/serega/plugins/if/validations/check_opt_unless.rb +0 -72
- data/lib/serega/plugins/if/validations/check_opt_unless_value.rb +0 -77
- data/lib/serega/plugins/metadata/validations/check_block.rb +0 -47
- data/lib/serega/plugins/metadata/validations/check_opt_const.rb +0 -39
- data/lib/serega/plugins/metadata/validations/check_opt_hide_empty.rb +0 -35
- data/lib/serega/plugins/metadata/validations/check_opt_hide_nil.rb +0 -35
- data/lib/serega/plugins/metadata/validations/check_opt_value.rb +0 -61
- data/lib/serega/plugins/metadata/validations/check_opts.rb +0 -59
- data/lib/serega/plugins/metadata/validations/check_path.rb +0 -50
- data/lib/serega/plugins/string_modifiers/parse_string_modifiers.rb +0 -111
- data/lib/serega/plugins/string_modifiers/string_modifiers.rb +0 -63
- data/lib/serega/utils/symbol_name.rb +0 -39
- data/lib/serega/validations/attribute/check_opt_const.rb +0 -40
- data/lib/serega/validations/check_initiate_params.rb +0 -57
- data/lib/serega/validations/check_serialize_params.rb +0 -54
|
@@ -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,142 @@
|
|
|
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
|
+
# Serialization context
|
|
8
|
+
type context = Hash[untyped, untyped]
|
|
9
|
+
|
|
10
|
+
extend ClassMethods
|
|
11
|
+
include InstanceMethods
|
|
12
|
+
|
|
13
|
+
module ClassMethods
|
|
14
|
+
attr_reader config: SeregaConfig
|
|
15
|
+
|
|
16
|
+
def plugin: (Symbol name, **untyped opts) -> Module
|
|
17
|
+
|
|
18
|
+
def attributes: () -> Hash[Symbol, SeregaAttribute]
|
|
19
|
+
|
|
20
|
+
def attribute: (Symbol | String name, **untyped opts) ?{ () [self: singleton(Serega)] -> void } -> SeregaAttribute
|
|
21
|
+
|
|
22
|
+
def batch: (Symbol | String name, untyped loader) -> void
|
|
23
|
+
| (Symbol | String name) { (untyped objects) -> untyped } -> void
|
|
24
|
+
| (Symbol | String name) { (untyped objects, context context) -> untyped } -> void
|
|
25
|
+
| (Symbol | String name) { (untyped objects, ctx: context) -> untyped } -> void
|
|
26
|
+
|
|
27
|
+
def formatter: (Symbol name, untyped formatter, ?allow_nil: bool) -> void
|
|
28
|
+
| (Symbol name, ?allow_nil: bool) { (untyped value) -> untyped } -> void
|
|
29
|
+
| (Symbol name, ?allow_nil: bool) { (untyped value, context context) -> untyped } -> void
|
|
30
|
+
| (Symbol name, ?allow_nil: bool) { (untyped value, ctx: context) -> untyped } -> void
|
|
31
|
+
|
|
32
|
+
def presenter: () -> singleton(SeregaPresenter)?
|
|
33
|
+
| () { () [self: singleton(SeregaPresenter)] -> void } -> singleton(SeregaPresenter)
|
|
34
|
+
|
|
35
|
+
def preload_with: () -> untyped
|
|
36
|
+
| (untyped handler) -> untyped
|
|
37
|
+
| () { (untyped objects, untyped preloads) -> void } -> untyped
|
|
38
|
+
|
|
39
|
+
def prepare_initial_objects: () -> untyped
|
|
40
|
+
| (untyped handler) -> untyped
|
|
41
|
+
| () { (untyped objects) -> untyped } -> untyped
|
|
42
|
+
| () { (untyped objects, context context) -> untyped } -> untyped
|
|
43
|
+
| () { (untyped objects, ctx: context) -> untyped } -> untyped
|
|
44
|
+
|
|
45
|
+
# Results are `untyped`: plugins change their shape. Options that plugins
|
|
46
|
+
# add at load time, such as `root:`, are not in these signatures.
|
|
47
|
+
def call: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?check_initiate_params: bool?, ?context: context?, ?many: bool?) -> untyped
|
|
48
|
+
alias to_h call
|
|
49
|
+
def to_data: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?check_initiate_params: bool?, ?context: context?, ?many: bool?) -> untyped
|
|
50
|
+
def to_struct: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?check_initiate_params: bool?, ?context: context?, ?many: bool?) -> untyped
|
|
51
|
+
def to_json: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?check_initiate_params: bool?, ?context: context?, ?many: bool?) -> String
|
|
52
|
+
|
|
53
|
+
def make_shareable!: () -> self
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
module InstanceMethods
|
|
57
|
+
def initialize: (?only: fields?, ?with: fields?, ?except: fields?, ?check_initiate_params: bool?) -> void
|
|
58
|
+
|
|
59
|
+
def call: (untyped object, ?context: context?, ?many: bool?) -> untyped
|
|
60
|
+
alias to_h call
|
|
61
|
+
def to_data: (untyped object, ?context: context?, ?many: bool?) -> untyped
|
|
62
|
+
def to_struct: (untyped object, ?context: context?, ?many: bool?) -> untyped
|
|
63
|
+
def to_json: (untyped object, ?context: context?, ?many: bool?) -> String
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Attribute of a serializer. Its methods are internal.
|
|
67
|
+
class SeregaAttribute
|
|
68
|
+
def initialize: (singleton(Serega) serializer_class, name: Symbol | String, ?opts: Hash[Symbol, untyped], ?block: Proc?, ?location: String?) -> void
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Parent class of presenters. Its methods are internal.
|
|
72
|
+
class SeregaPresenter
|
|
73
|
+
def initialize: (untyped object, ?context? ctx) -> void
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
class SeregaError < StandardError
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
class AttributeNotExist < SeregaError
|
|
80
|
+
def initialize: (?String? message, ?singleton(Serega)? serializer, ?Array[String]? attributes) -> void
|
|
81
|
+
|
|
82
|
+
attr_reader serializer: singleton(Serega)?
|
|
83
|
+
attr_reader attributes: Array[String]?
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
class DepthLimitError < SeregaError
|
|
87
|
+
def initialize: (?String? message, ?String? details) -> void
|
|
88
|
+
|
|
89
|
+
attr_reader details: String?
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
class SeregaConfig
|
|
93
|
+
include SeregaConfigInstanceMethods
|
|
94
|
+
|
|
95
|
+
module SeregaConfigInstanceMethods
|
|
96
|
+
def check_initiate_params: () -> bool
|
|
97
|
+
def check_initiate_params=: (bool value) -> bool
|
|
98
|
+
|
|
99
|
+
def check_attribute_name: () -> bool
|
|
100
|
+
def check_attribute_name=: (bool value) -> bool
|
|
101
|
+
|
|
102
|
+
def require_many: () -> bool
|
|
103
|
+
def require_many=: (bool value) -> bool
|
|
104
|
+
|
|
105
|
+
def delegate_default_allow_nil: () -> bool
|
|
106
|
+
def delegate_default_allow_nil=: (bool value) -> bool
|
|
107
|
+
|
|
108
|
+
def hide_by_default: () -> (bool | :auto)
|
|
109
|
+
def hide_by_default=: (bool | :auto value) -> (bool | :auto)
|
|
110
|
+
|
|
111
|
+
def base_serializer: () -> singleton(Serega)?
|
|
112
|
+
def base_serializer=: (singleton(Serega) value) -> singleton(Serega)
|
|
113
|
+
|
|
114
|
+
def depth_limit: () -> Integer?
|
|
115
|
+
def depth_limit=: (Integer? value) -> Integer?
|
|
116
|
+
|
|
117
|
+
def max_cached_plans_per_serializer_count: () -> Integer
|
|
118
|
+
def max_cached_plans_per_serializer_count=: (Integer value) -> Integer
|
|
119
|
+
|
|
120
|
+
def batch_id_option: () -> Symbol
|
|
121
|
+
def batch_id_option=: (Symbol value) -> Symbol
|
|
122
|
+
|
|
123
|
+
def auto_preload: () -> { has_delegate_option: bool, has_serializer_option: bool }
|
|
124
|
+
def auto_preload=: (bool | { ?has_delegate_option: bool, ?has_serializer_option: bool } value) -> { has_delegate_option: bool, has_serializer_option: bool }
|
|
125
|
+
|
|
126
|
+
def auto_preload_excluded_methods: () -> Array[Symbol]
|
|
127
|
+
def auto_preload_excluded_methods=: (Array[Symbol] value) -> Array[Symbol]
|
|
128
|
+
|
|
129
|
+
def hash_access: () -> HashAccessConfig
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
class HashAccessConfig
|
|
133
|
+
def initialize: (Hash[Symbol, untyped] opts) -> void
|
|
134
|
+
|
|
135
|
+
def default_mode: () -> (:symbol | :string)
|
|
136
|
+
def default_mode=: (:symbol | :string value) -> (:symbol | :string)
|
|
137
|
+
|
|
138
|
+
def default_allow_missing_key: () -> bool
|
|
139
|
+
def default_allow_missing_key=: (bool value) -> bool
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
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.
|
|
4
|
+
version: 0.50.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andrey
|
|
@@ -11,14 +11,13 @@ 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.
|
|
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
|
|
@@ -26,61 +25,57 @@ files:
|
|
|
26
25
|
- lib/serega.rb
|
|
27
26
|
- lib/serega/attribute.rb
|
|
28
27
|
- lib/serega/attribute_normalizer.rb
|
|
28
|
+
- lib/serega/attribute_normalizers/batch.rb
|
|
29
|
+
- lib/serega/attribute_normalizers/block.rb
|
|
30
|
+
- lib/serega/attribute_normalizers/delegate.rb
|
|
31
|
+
- lib/serega/attribute_normalizers/hash_access.rb
|
|
32
|
+
- lib/serega/attribute_normalizers/preloads.rb
|
|
29
33
|
- lib/serega/attribute_value_resolvers/batch.rb
|
|
30
34
|
- lib/serega/attribute_value_resolvers/const.rb
|
|
31
35
|
- lib/serega/attribute_value_resolvers/delegate.rb
|
|
32
36
|
- lib/serega/attribute_value_resolvers/hash_access.rb
|
|
33
37
|
- lib/serega/attribute_value_resolvers/keyword.rb
|
|
38
|
+
- lib/serega/attribute_values.rb
|
|
39
|
+
- lib/serega/batch_loader.rb
|
|
40
|
+
- lib/serega/conditions.rb
|
|
34
41
|
- lib/serega/config.rb
|
|
35
|
-
- lib/serega/
|
|
36
|
-
- lib/serega/engine/
|
|
37
|
-
- lib/serega/engine/level_queue.rb
|
|
38
|
-
- lib/serega/engine/loader.rb
|
|
42
|
+
- lib/serega/engine/pulls.rb
|
|
43
|
+
- lib/serega/engine/run.rb
|
|
39
44
|
- lib/serega/errors.rb
|
|
40
|
-
- lib/serega/
|
|
41
|
-
- lib/serega/object_serializer.rb
|
|
45
|
+
- lib/serega/formatter.rb
|
|
42
46
|
- lib/serega/plan.rb
|
|
47
|
+
- lib/serega/plan/cache.rb
|
|
48
|
+
- lib/serega/plan/per_ractor_cache.rb
|
|
43
49
|
- lib/serega/plan_point.rb
|
|
44
50
|
- lib/serega/plugins.rb
|
|
45
51
|
- lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb
|
|
46
52
|
- lib/serega/plugins/activerecord_preloads/lib/preloader.rb
|
|
47
|
-
- lib/serega/plugins/camel_case/camel_case.rb
|
|
48
53
|
- lib/serega/plugins/context_metadata/context_metadata.rb
|
|
49
|
-
- lib/serega/plugins/
|
|
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
|
|
54
|
+
- lib/serega/plugins/metadata/check_params.rb
|
|
58
55
|
- lib/serega/plugins/metadata/meta_attribute.rb
|
|
59
56
|
- 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
57
|
- 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
58
|
- lib/serega/presenter.rb
|
|
59
|
+
- lib/serega/result_builder.rb
|
|
60
|
+
- lib/serega/result_code.rb
|
|
61
|
+
- lib/serega/shareable.rb
|
|
62
|
+
- lib/serega/source_group.rb
|
|
71
63
|
- lib/serega/utils/collection_detector.rb
|
|
64
|
+
- lib/serega/utils/deep_merge.rb
|
|
72
65
|
- lib/serega/utils/enum_deep_dup.rb
|
|
73
66
|
- lib/serega/utils/enum_deep_freeze.rb
|
|
74
67
|
- lib/serega/utils/method_signature.rb
|
|
68
|
+
- lib/serega/utils/parse_string_modifiers.rb
|
|
69
|
+
- lib/serega/utils/ractor_local.rb
|
|
75
70
|
- lib/serega/utils/serialized_attribute_error.rb
|
|
76
|
-
- lib/serega/utils/symbol_name.rb
|
|
77
71
|
- lib/serega/utils/to_hash.rb
|
|
78
72
|
- lib/serega/validations/attribute/check_block.rb
|
|
79
73
|
- lib/serega/validations/attribute/check_name.rb
|
|
80
74
|
- lib/serega/validations/attribute/check_opt_base_serializer.rb
|
|
81
75
|
- lib/serega/validations/attribute/check_opt_batch.rb
|
|
82
|
-
- lib/serega/validations/attribute/
|
|
76
|
+
- lib/serega/validations/attribute/check_opt_condition.rb
|
|
83
77
|
- lib/serega/validations/attribute/check_opt_delegate.rb
|
|
78
|
+
- lib/serega/validations/attribute/check_opt_format.rb
|
|
84
79
|
- lib/serega/validations/attribute/check_opt_hash_access.rb
|
|
85
80
|
- lib/serega/validations/attribute/check_opt_hide.rb
|
|
86
81
|
- lib/serega/validations/attribute/check_opt_many.rb
|
|
@@ -90,36 +85,45 @@ files:
|
|
|
90
85
|
- lib/serega/validations/attribute/check_opt_value.rb
|
|
91
86
|
- lib/serega/validations/check_attribute_params.rb
|
|
92
87
|
- lib/serega/validations/check_batch_loader_params.rb
|
|
93
|
-
- lib/serega/validations/
|
|
94
|
-
- lib/serega/validations/
|
|
88
|
+
- lib/serega/validations/check_formatter.rb
|
|
89
|
+
- lib/serega/validations/check_serialize_keywords.rb
|
|
95
90
|
- lib/serega/validations/initiate/check_modifiers.rb
|
|
96
91
|
- lib/serega/validations/utils/check_allowed_keys.rb
|
|
92
|
+
- lib/serega/validations/utils/check_hash_access_value.rb
|
|
97
93
|
- lib/serega/validations/utils/check_opt_is_bool.rb
|
|
98
94
|
- lib/serega/validations/utils/check_opt_is_hash.rb
|
|
99
95
|
- lib/serega/validations/utils/check_opt_is_string_or_symbol.rb
|
|
100
96
|
- lib/serega/version.rb
|
|
97
|
+
- sig/serega.rbs
|
|
98
|
+
- sig/serega/plugins.rbs
|
|
101
99
|
homepage: https://github.com/andreyruby/serega
|
|
102
100
|
licenses:
|
|
103
101
|
- MIT
|
|
104
102
|
metadata:
|
|
105
103
|
source_code_uri: https://github.com/andreyruby/serega
|
|
106
|
-
documentation_uri: https://
|
|
104
|
+
documentation_uri: https://andreyruby.github.io/serega/
|
|
107
105
|
changelog_uri: https://github.com/andreyruby/serega/blob/master/CHANGELOG.md
|
|
108
|
-
rdoc_options:
|
|
106
|
+
rdoc_options:
|
|
107
|
+
- "--markup"
|
|
108
|
+
- markdown
|
|
109
|
+
- "--main"
|
|
110
|
+
- README.md
|
|
111
|
+
- "--title"
|
|
112
|
+
- Serega
|
|
109
113
|
require_paths:
|
|
110
114
|
- lib
|
|
111
115
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
112
116
|
requirements:
|
|
113
117
|
- - ">="
|
|
114
118
|
- !ruby/object:Gem::Version
|
|
115
|
-
version: 3.
|
|
119
|
+
version: 3.3.0
|
|
116
120
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
117
121
|
requirements:
|
|
118
122
|
- - ">="
|
|
119
123
|
- !ruby/object:Gem::Version
|
|
120
124
|
version: '0'
|
|
121
125
|
requirements: []
|
|
122
|
-
rubygems_version: 4.0.
|
|
126
|
+
rubygems_version: 4.0.23
|
|
123
127
|
specification_version: 4
|
|
124
128
|
summary: Ruby Serializer
|
|
125
129
|
test_files: []
|
data/lib/serega/data_builder.rb
DELETED
|
@@ -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
|
data/lib/serega/engine/level.rb
DELETED
|
@@ -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
|
data/lib/serega/engine/loader.rb
DELETED
|
@@ -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
|