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
@@ -1,45 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- # @private
5
- module AttributeValueResolvers
4
+ module AttributeValueResolvers # :nodoc:
6
5
  #
7
- # Builds value resolver for attributes with the :hash_access option
6
+ # Settings of the :hash_access option
8
7
  #
9
- # @private
10
- class HashAccessResolver
8
+ class HashAccessResolver # :nodoc:
11
9
  # Allowed hash access modes
12
10
  MODES = %i[symbol string].freeze
13
-
14
- #
15
- # Creates resolver that reads a key from Hash records
16
- #
17
- # @param name [Symbol, String] hash key
18
- # @param mode [Symbol] hash access mode (:symbol, :string)
19
- # @param allow_missing_key [Boolean] whether a missing key is read via `record[key]` rather than raising
20
- #
21
- # @return [HashAccessKeyword] resolver instance
22
- #
23
- def self.get(name, mode, allow_missing_key)
24
- HashAccessKeyword.new(name, mode, allow_missing_key)
25
- end
26
11
  end
27
12
 
28
13
  #
29
14
  # Builds value resolver for attributes with the :delegate option using
30
15
  # hash access on any of its steps
31
16
  #
32
- # @private
33
- class HashAccessDelegateResolver
34
- #
17
+ class HashAccessDelegateResolver # :nodoc:
35
18
  # Creates resolver that delegates through the provided step readers
36
- #
37
- # @param to_step [#call] reader of the intermediate object
38
- # @param final_step [#call] reader of the final value
39
- # @param delegate_allow_nil [Boolean] whether a nil intermediate object resolves to nil
40
- #
41
- # @return [HashAccessDelegate, HashAccessDelegateAllowNil] resolver instance
42
- #
43
19
  def self.get(to_step, final_step, delegate_allow_nil)
44
20
  delegate_allow_nil ? HashAccessDelegateAllowNil.new(to_step, final_step) : HashAccessDelegate.new(to_step, final_step)
45
21
  end
@@ -48,75 +24,96 @@ class Serega
48
24
  #
49
25
  # Value resolver for attributes with the :hash_access option
50
26
  #
51
- # @private
52
- class HashAccessKeyword
27
+ class HashAccessKeyword < Base # :nodoc:
53
28
  def initialize(name, mode, allow_missing_key)
54
29
  @key = (mode == :symbol) ? name.to_sym : name.to_s
55
30
  @allow_missing_key = allow_missing_key
56
31
  end
57
32
 
58
- #
33
+ # Methods that the value read calls on the source
34
+ def source_methods
35
+ @allow_missing_key ? %i[[]] : %i[fetch []]
36
+ end
37
+
59
38
  # Reads the key from the record
60
- #
61
- # @param object [Object] serialized object or delegation step value
62
- # @return [Object] the value found
63
- #
64
39
  def call(object)
65
40
  return object[@key] if @allow_missing_key
66
41
 
67
42
  object.fetch(@key) do
68
43
  default = object[@key]
69
- next default unless default.nil?
70
-
71
- raise KeyError.new("key not found: #{@key.inspect}", key: @key, receiver: object)
44
+ default.nil? ? object.fetch(@key) : default
72
45
  end
73
46
  end
47
+
48
+ # Ruby code that reads the key, the same way as #call
49
+ def code(source_variable)
50
+ key = SeregaUtils::RubyLiteral.code(@key)
51
+ return "#{source_variable}[#{key}]" if @allow_missing_key
52
+
53
+ "#{source_variable}.fetch(#{key}) { (found = #{source_variable}[#{key}]).nil? ? #{source_variable}.fetch(#{key}) : found }"
54
+ end
74
55
  end
75
56
 
76
57
  #
77
58
  # Value resolver for attributes with :hash_access and :delegate (without :allow_nil) options
78
59
  #
79
- # @private
80
- class HashAccessDelegate
60
+ class HashAccessDelegate < Base # :nodoc:
81
61
  def initialize(to_step, final_step)
82
62
  @to_step = to_step
83
63
  @final_step = final_step
84
64
  end
85
65
 
86
- #
66
+ # Methods that the value read calls on the source: those of the first step
67
+ def source_methods
68
+ @to_step.source_methods
69
+ end
70
+
87
71
  # Delegates the value reading through the intermediate object
88
- #
89
- # @param object [Object] serialized object
90
- # @return [Object] the value found
91
- #
92
72
  def call(object)
93
73
  @final_step.call(@to_step.call(object))
94
74
  end
75
+
76
+ # Ruby code that delegates the value reading, the same way as #call
77
+ def code(source_variable)
78
+ to_code = @to_step.code(source_variable)
79
+ final_code = @final_step.code("intermediate")
80
+ return unless to_code && final_code
81
+
82
+ "(intermediate = #{to_code}; #{final_code})"
83
+ end
95
84
  end
96
85
 
97
86
  #
98
87
  # Value resolver for attributes with :hash_access and :delegate (with :allow_nil) options
99
88
  #
100
- # @private
101
- class HashAccessDelegateAllowNil
89
+ class HashAccessDelegateAllowNil < Base # :nodoc:
102
90
  def initialize(to_step, final_step)
103
91
  @to_step = to_step
104
92
  @final_step = final_step
105
93
  end
106
94
 
107
- #
95
+ # Methods that the value read calls on the source: those of the first step
96
+ def source_methods
97
+ @to_step.source_methods
98
+ end
99
+
108
100
  # Delegates the value reading through the intermediate object,
109
101
  # resolving a nil intermediate to nil
110
- #
111
- # @param object [Object] serialized object
112
- # @return [Object, nil] the value found
113
- #
114
102
  def call(object)
115
103
  intermediate = @to_step.call(object)
116
104
  return if intermediate.nil?
117
105
 
118
106
  @final_step.call(intermediate)
119
107
  end
108
+
109
+ # Ruby code that delegates the value reading, the same way as #call
110
+ def code(source_variable)
111
+ to_code = @to_step.code(source_variable)
112
+ final_code = @final_step.code("intermediate")
113
+ return unless to_code && final_code
114
+
115
+ "((intermediate = #{to_code}).nil? ? nil : #{final_code})"
116
+ end
120
117
  end
121
118
  end
122
119
  end
@@ -4,42 +4,34 @@ class Serega
4
4
  #
5
5
  # Attribute value resolvers
6
6
  #
7
- # @private
8
- module AttributeValueResolvers
9
- #
10
- # Builds value resolver class for attributes with :keyword option
11
- #
12
- # @private
13
- class KeywordResolver
14
- #
15
- # Creates resolver that calls method on object
16
- #
17
- # @param keyword [Symbol] method name to call on object
18
- # @return [Keyword] resolver instance
19
- #
20
- def self.get(keyword)
21
- Keyword.new(keyword)
22
- end
23
- end
24
-
7
+ module AttributeValueResolvers # :nodoc:
25
8
  #
26
9
  # Value resolver class for attributes with :keyword option
27
10
  #
28
- # @private
29
- class Keyword
11
+ class Keyword < Base # :nodoc:
30
12
  def initialize(keyword)
31
13
  @keyword = keyword
32
14
  end
33
15
 
34
- #
16
+ # Methods that the value read calls on the source: the keyword
17
+ def source_methods
18
+ [@keyword]
19
+ end
20
+
21
+ # Method of the object that gives the value: the keyword
22
+ def object_method
23
+ @keyword
24
+ end
25
+
35
26
  # Calls the keyword method on the object
36
- #
37
- # @param object [Object] the object to call method on
38
- # @return [Object] result of method call
39
- #
40
27
  def call(object)
41
28
  object.public_send(@keyword)
42
29
  end
30
+
31
+ # Ruby code that calls the keyword method
32
+ def code(source_variable)
33
+ "#{source_variable}.#{@keyword}" if SeregaResultCode::PLAIN_METHOD_NAME.match?(@keyword)
34
+ end
43
35
  end
44
36
  end
45
37
  end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ #
5
+ # Values of the attributes that generated code reads, by attribute name:
6
+ # `CONSTANTS` and `DEFAULTS` hold the `:const` and `:default` values,
7
+ # `FORMATTERS` the formatter callables and `FORMATTER_ARGS` the arguments
8
+ # of method formatters.
9
+ #
10
+ class SeregaAttributeValues # :nodoc:
11
+ # `:const` values by attribute name
12
+ CONSTANTS = {}
13
+
14
+ # `:default` values by attribute name
15
+ DEFAULTS = {}
16
+
17
+ # Formatter callables by attribute name
18
+ FORMATTERS = {}
19
+
20
+ # Arguments of method formatters by attribute name
21
+ FORMATTER_ARGS = {}
22
+
23
+ # Gives each subclass its own values
24
+ def self.inherited(subclass)
25
+ super
26
+ subclass.const_set(:CONSTANTS, {})
27
+ subclass.const_set(:DEFAULTS, {})
28
+ subclass.const_set(:FORMATTERS, {})
29
+ subclass.const_set(:FORMATTER_ARGS, {})
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ #
5
+ # Named batch loader. Loads values of many objects in one call.
6
+ #
7
+ class SeregaBatchLoader # :nodoc:
8
+ # Batch loader name
9
+ attr_reader :name
10
+
11
+ # Batch loader block
12
+ attr_reader :block
13
+
14
+ # Initializes new batch loader
15
+ def initialize(name:, block:)
16
+ SeregaValidations::CheckBatchLoaderParams.new(name, block).validate
17
+
18
+ @name = name.to_sym
19
+ @block = block
20
+ @signature = SeregaUtils::MethodSignature.call(block, pos_limit: 2, keyword_args: [:ctx])
21
+ end
22
+
23
+ # Loads values for objects
24
+ def load(objects, context)
25
+ case signature
26
+ when "1" then block.call(objects)
27
+ when "2" then block.call(objects, context)
28
+ else block.call(objects, ctx: context) # "1_ctx"
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ # Batch loader block signature
35
+ attr_reader :signature
36
+ end
37
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ #
5
+ # The :if, :unless, :if_value and :unless_value conditions of one attribute.
6
+ # Each object defines `#satisfy?(object, context)`, which checks :if and
7
+ # :unless, and `#satisfy_value?(value, context)`, which checks :if_value
8
+ # and :unless_value. Each method is one line of code at the file and line
9
+ # of the attribute, and calls the conditions directly, for example:
10
+ #
11
+ # def satisfy?(object, context) = (begin; object.active; rescue => error; ...; end)
12
+ #
13
+ class SeregaConditions # :nodoc:
14
+ # Makes the conditions of the attribute. A Symbol condition calls the
15
+ # method of this name.
16
+ def initialize(attribute, conditions)
17
+ @attribute = attribute
18
+ @conditions = conditions.transform_values do |condition|
19
+ condition.is_a?(Symbol) ? AttributeValueResolvers::Keyword.new(condition) : condition
20
+ end
21
+ @object_conditions = @conditions.key?(:if) || @conditions.key?(:unless)
22
+ @value_conditions = @conditions.key?(:if_value) || @conditions.key?(:unless_value)
23
+ satisfy_code = "def satisfy?(object, context) = #{check_code(:if, :unless)}"
24
+ satisfy_value_code = "def satisfy_value?(object, context) = #{check_code(:if_value, :unless_value)}"
25
+ attribute.define_methods(singleton_class, "#{satisfy_code}; #{satisfy_value_code}")
26
+ end
27
+
28
+ # Whether there is an :if or :unless condition
29
+ def object_conditions?
30
+ @object_conditions
31
+ end
32
+
33
+ # Whether there is an :if_value or :unless_value condition
34
+ def value_conditions?
35
+ @value_conditions
36
+ end
37
+
38
+ # Methods that the :if and :unless conditions call on the object, nil
39
+ # when one of them is a callable, which gets the object
40
+ def source_methods
41
+ conditions = @conditions.values_at(:if, :unless).compact
42
+ return unless conditions.all?(AttributeValueResolvers::Keyword)
43
+
44
+ conditions.flat_map(&:source_methods)
45
+ end
46
+
47
+ private
48
+
49
+ # Code that is true when the object passes the :if and does not match the
50
+ # :unless condition
51
+ def check_code(if_name, unless_name)
52
+ checks = []
53
+ checks << rescued_call_code(if_name) if @conditions.key?(if_name)
54
+ checks << "!#{rescued_call_code(unless_name)}" if @conditions.key?(unless_name)
55
+ checks.empty? ? "true" : checks.join(" && ")
56
+ end
57
+
58
+ # Code that calls the condition. An error raised there gets the condition
59
+ # name.
60
+ def rescued_call_code(name)
61
+ "(begin; #{call_code(name)}; rescue => error; " \
62
+ "Serega::SeregaUtils::SerializedAttributeError.condition(error, @attribute, #{name.inspect}); end)"
63
+ end
64
+
65
+ # Code that calls the condition with the parameters of its signature
66
+ def call_code(name)
67
+ condition = @conditions[name]
68
+ keyword_code = condition.code("object") if condition.is_a?(AttributeValueResolvers::Keyword)
69
+ return keyword_code if keyword_code
70
+
71
+ callable = "@conditions[#{name.inspect}]"
72
+ case SeregaUtils::MethodSignature.call(condition, pos_limit: 2, keyword_args: [:ctx])
73
+ when "1" then "#{callable}.call(object)"
74
+ when "2" then "#{callable}.call(object, context)"
75
+ when "1_ctx" then "#{callable}.call(object, ctx: context)"
76
+ when "2_ctx" then "#{callable}.call(object, context, ctx: context)"
77
+ else "#{callable}.call" # "0"
78
+ end
79
+ end
80
+ end
81
+ end