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,58 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- # Plugins are stored here
4
+ # Built-in features, turned on with Serega::ClassMethods#plugin.
5
+ #
6
+ # See [Guides](https://github.com/andreyruby/serega/blob/master/docs/README.md).
5
7
  module SeregaPlugins
6
- @plugins = {}
8
+ # Plugin module names, by plugin name
9
+ MODULES = { # :nodoc:
10
+ root: :Root,
11
+ metadata: :Metadata,
12
+ context_metadata: :ContextMetadata,
13
+ activerecord_preloads: :ActiverecordPreloads
14
+ }.freeze
7
15
 
8
16
  class << self
9
- #
10
- # Registers given plugin to be able to load it using symbol name.
11
- #
12
- # @param name [Symbol] Plugin name
13
- # @param mod [Module] Plugin module
14
- #
15
- # @example Register plugin
16
- # SeregaPlugins.register_plugin(:plugin_name, PluginModule)
17
- #
18
- # @return [void]
19
- #
20
- # @private
21
- def register_plugin(name, mod)
22
- @plugins[name] = mod
23
- end
24
-
25
- #
26
- # Loads plugin code and returns plugin core module.
27
- #
28
- # @param name [Symbol, Module] plugin name or plugin itself
29
- #
30
- # @raise [SeregaError] Raises SeregaError when plugin was not found
31
- #
32
- # @example Find plugin when providing name
33
- # SeregaPlugins.find_plugin(:root) # => SeregaPlugins::Root
34
- #
35
- # @example Find plugin when providing plugin itself
36
- # SeregaPlugins.find_plugin(Root) # => Root
37
- #
38
- # @return [Class<Module>] Plugin core module
39
- #
40
- # @private
41
- def find_plugin(name)
42
- return name if name.is_a?(Module)
43
- return @plugins[name] if @plugins.key?(name)
44
-
45
- require_plugin(name)
46
-
47
- @plugins[name] || raise(SeregaError, "Plugin '#{name}' did not register itself correctly")
48
- end
49
-
50
- private
51
-
52
- def require_plugin(name)
53
- require "serega/plugins/#{name}/#{name}"
54
- rescue LoadError
55
- raise SeregaError, "Plugin '#{name}' does not exist"
17
+ # Loads the code of the plugin and returns its module. Raises
18
+ # SeregaError when the plugin does not exist.
19
+ def find(name) # :nodoc:
20
+ module_name = MODULES.fetch(name) { raise SeregaError, "Plugin #{name.inspect} does not exist" }
21
+ require_relative "plugins/#{name}/#{name}"
22
+ const_get(module_name)
56
23
  end
57
24
  end
58
25
  end
@@ -1,15 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "delegate"
4
- require "forwardable"
5
4
 
6
5
  class Serega
7
- #
8
- # Wraps the serialized object, so computed attribute values can be defined as
6
+ # Wraps the object to serialize, so computed attribute values can be defined as
9
7
  # methods instead of `:value` callables.
10
8
  #
11
9
  # SeregaPresenter inherits from SimpleDelegator:
12
- # - All methods of the serialized object are available directly inside presenter methods.
10
+ # - All methods of the wrapped object are available directly inside presenter methods.
13
11
  # - Methods not defined on SeregaPresenter are resolved via method_missing on the first call
14
12
  # and then defined as real delegators, so subsequent calls skip method_missing entirely.
15
13
  # - The original object is accessible via __getobj__ (standard SimpleDelegator API).
@@ -18,7 +16,6 @@ class Serega
18
16
  # The `presenter do ... end` block is evaluated inside the serializer's own
19
17
  # SeregaPresenter class, so multiple blocks accumulate.
20
18
  #
21
- # @example
22
19
  # class UserSerializer < Serega
23
20
  # attribute :name
24
21
  # attribute :role
@@ -33,13 +30,80 @@ class Serega
33
30
  # end
34
31
  # end
35
32
  # end
36
- #
37
- # @private
38
- class SeregaPresenter < SimpleDelegator
33
+ class SeregaPresenter < SimpleDelegator # :nodoc:
34
+ # Method names of delegators defined with a compiled `def`
35
+ PLAIN_METHOD_NAME = /\A[a-z_][a-zA-Z0-9_]*[?!]?\z/
36
+ private_constant :PLAIN_METHOD_NAME
37
+
39
38
  #
40
- # @param object [Object] Serialized object to wrap
41
- # @param ctx [Hash, nil] Serialization context
39
+ # Delegator to the method of the wrapped object with the name it is
40
+ # called by. Delegators with other names are its aliases.
42
41
  #
42
+ module CalleeDelegator # :nodoc:
43
+ #
44
+ # Calls the method of the wrapped object with the name of the alias
45
+ #
46
+ def __serega_delegate__(...)
47
+ __getobj__.public_send(__callee__, ...)
48
+ end
49
+ end
50
+
51
+ # Includes into each presenter class its own `Delegators` module.
52
+ # Delegators to the methods of the wrapped object are defined there after the
53
+ # first #method_missing hit. Presenter methods override them and can call
54
+ # them via `super`.
55
+ def self.inherited(subclass)
56
+ super
57
+ delegators = Module.new
58
+ delegators.include(CalleeDelegator)
59
+ subclass.const_set(:Delegators, delegators)
60
+ subclass.include(delegators)
61
+ end
62
+
63
+ # Defines a method that delegates to the wrapped object in the
64
+ # presenter class `Delegators` module
65
+ def self.define_delegator(name)
66
+ delegators = self::Delegators
67
+
68
+ if PLAIN_METHOD_NAME.match?(name)
69
+ delegators.module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
70
+ def #{name}(...)
71
+ __getobj__.#{name}(...)
72
+ end
73
+ RUBY
74
+ else
75
+ delegators.alias_method(name, :__serega_delegate__)
76
+ end
77
+ end
78
+
79
+ # Whether the presenter class answers one of the method names itself:
80
+ # it defines the method, not as a delegator to the wrapped object, or
81
+ # it defines its own `method_missing`, which can answer any name
82
+ def self.presents_any?(names)
83
+ return false if names.empty?
84
+ return true unless instance_method(:method_missing).owner.equal?(SeregaPresenter)
85
+
86
+ names.any? { |name| presents?(name) }
87
+ end
88
+
89
+ # Whether the presenter class defines the method itself, not as a
90
+ # delegator to the wrapped object
91
+ def self.presents?(name)
92
+ return false unless method_defined?(name) || private_method_defined?(name)
93
+
94
+ owner = instance_method(name).owner
95
+ !(owner.instance_of?(Module) && owner.include?(CalleeDelegator))
96
+ end
97
+ private_class_method :presents?
98
+
99
+ # Defines delegators for the method names that the presenter class does
100
+ # not define itself, thus presenter methods stay in front of them
101
+ def self.define_delegators(names)
102
+ names.each do |name|
103
+ define_delegator(name) unless method_defined?(name) || private_method_defined?(name)
104
+ end
105
+ end
106
+
43
107
  def initialize(object, ctx = nil)
44
108
  super(object)
45
109
  @__ctx__ = ctx
@@ -50,17 +114,15 @@ class Serega
50
114
  attr_reader :__ctx__
51
115
 
52
116
  #
53
- # Delegates all missing methods to serialized object.
117
+ # Delegates all missing methods to the wrapped object.
54
118
  #
55
- # Creates delegator method after first #method_missing hit to improve
56
- # performance of following serializations.
119
+ # Creates delegator method for public methods of the wrapped object after
120
+ # first #method_missing hit to improve performance of following serializations.
57
121
  #
58
- def method_missing(name, *_args, &_block) # rubocop:disable Style/MissingRespondToMissing -- base SimpleDelegator class has this method
59
- super.tap do
60
- self.class.def_delegator :__getobj__, name
61
- end
122
+ def method_missing(name, ...) # rubocop:disable Style/MissingRespondToMissing -- base SimpleDelegator class has this method
123
+ result = super
124
+ self.class.define_delegator(name) if __getobj__.respond_to?(name)
125
+ result
62
126
  end
63
-
64
- extend Forwardable
65
127
  end
66
128
  end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ #
5
+ # Builds the serialized objects (Hash, Struct or Data) of one plan in one
6
+ # serialization mode.
7
+ #
8
+ # The builder gets the code of its `#call` and `#call_one` methods from
9
+ # SeregaResultCode. `#call` builds the serialized objects of a source group
10
+ # in one loop.
11
+ #
12
+ class SeregaResultBuilder # :nodoc:
13
+ # Serialization mode
14
+ attr_reader :mode
15
+
16
+ # Makes a result builder and generates its methods:
17
+ #
18
+ # call(sources, context, batches, relations) -> Array<Hash, Struct, Data>
19
+ #
20
+ # - `batches` holds the loaded batches per point, or nil.
21
+ # - `relations` holds the relation values of all sources per relation
22
+ # point, or nil.
23
+ #
24
+ # Points without relations and batch loaders get a `#call_one` method
25
+ # too, which builds the serialized object of one source:
26
+ #
27
+ # call_one(source, context) -> Hash, Struct, Data
28
+ def initialize(serializer_class, mode, points)
29
+ @mode = mode
30
+ @points = points
31
+ @result_class = result_class(serializer_class)
32
+ # The generated code reads the `:const`, `:default` and formatter values from its constants
33
+ singleton_class.const_set(:SeregaAttributeValues, serializer_class::SeregaAttributeValues)
34
+ singleton_class.class_eval(SeregaResultCode.new(mode, points, compact: serializer_class.config.compact).to_s, SeregaUtils::SerializedAttributeError::GENERATED_CODE_FILE, 1)
35
+ end
36
+
37
+ private
38
+
39
+ def result_class(serializer_class)
40
+ return if mode == :hash
41
+
42
+ initialize_point = @points.find { |point| point.name == :initialize }
43
+ raise_member_error("Struct and Data can not have a member named initialize", initialize_point) if initialize_point
44
+
45
+ member_class(serializer_class, @points.map(&:name))
46
+ end
47
+
48
+ # Returns the Struct or Data class with the members. Raises a
49
+ # SeregaError for an attribute name that Ruby can not make a member.
50
+ def member_class(serializer_class, names)
51
+ (mode == :struct) ? serializer_class.struct_class_for(names) : serializer_class.data_class_for(names)
52
+ rescue ArgumentError, NameError => error
53
+ point = @points.find { |point| !member_name?(point.name) }
54
+ raise_member_error(error.message, point)
55
+ end
56
+
57
+ def member_name?(name)
58
+ (mode == :struct) ? Struct.new(name) : Data.define(name)
59
+ true
60
+ rescue ArgumentError, NameError
61
+ false
62
+ end
63
+
64
+ def raise_member_error(message, point)
65
+ SeregaUtils::SerializedAttributeError.call(SeregaError.new(message), point)
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ class SeregaResultCode # :nodoc:
5
+ #
6
+ # Code of a point without a child plan. A plain attribute reads its value
7
+ # with the SeregaAttribute::ValueCode#plain of its attribute, an
8
+ # attribute with a `:value` callable calls it with the
9
+ # SeregaAttribute::ValueCode#callable, a :batch attribute reads its
10
+ # loaded batch with the SeregaAttribute::ValueCode#batch, other
11
+ # attributes call SeregaAttribute#value.
12
+ #
13
+ # A relation reads its relation source with this code too, without the
14
+ # value conditions: they check the serialized relation value later.
15
+ #
16
+ class AttributePointCode < PointCode # :nodoc:
17
+ def initialize(point, index, check_value: true)
18
+ super(point, index)
19
+ @check_value = check_value
20
+ value_code = point.attribute.value_code
21
+ @batch_code = value_code.batch("batch_#{index}", "SeregaAttributeValues::")
22
+ @batches_variable = (point.batch_loaders.empty? || @batch_code) ? nil : "batches_#{index}"
23
+ @callable_code = value_code.callable("callable_#{index}", @batches_variable || "nil", "SeregaAttributeValues::")
24
+ @read_code = value_code.plain("SeregaAttributeValues::") || @callable_code || @batch_code
25
+ end
26
+
27
+ # Code that sets the local variables that the value code reads
28
+ def variables
29
+ index = @index
30
+ variables = [value_variable]
31
+ variables << "#{@batches_variable} = batches[#{index}]" if @batches_variable
32
+ variables << "conditions_#{index} = @points[#{index}].attribute.conditions" if conditional?
33
+ variables.compact
34
+ end
35
+
36
+ # Whether the value can be SKIP. Without the value check, only the
37
+ # :if and :unless conditions give SKIP.
38
+ def conditional?
39
+ return super if @check_value
40
+
41
+ conditions = @point.attribute.conditions
42
+ conditions ? conditions.object_conditions? : false
43
+ end
44
+
45
+ # Code that assigns the value of the source to `value_N`. A
46
+ # conditional attribute gets SKIP when it fails its conditions.
47
+ def assign_value
48
+ index = @index
49
+ read_code = rescued_read_code
50
+ return "value_#{index} =\n#{read_code.gsub(/^/, " ")}" unless conditional?
51
+ return conditional_read_code(read_code.gsub(/^/, " ")) unless @check_value
52
+
53
+ conditional_read_code(<<~RUBY.chomp)
54
+ value_#{index} =
55
+ #{read_code.gsub(/^/, " ")}
56
+ conditions_#{index}.satisfy_value?(value_#{index}, context) ? value_#{index} : Serega::SeregaEngine::SKIP
57
+ RUBY
58
+ end
59
+
60
+ # An attribute without batch loaders reads one source alone, thus
61
+ # `#call_one` can read it
62
+ def call_one?
63
+ @point.batch_loaders.empty?
64
+ end
65
+
66
+ private
67
+
68
+ # Code that gives the value of `read_code` when the source passes the
69
+ # :if and :unless conditions, and SKIP otherwise
70
+ def conditional_read_code(read_code)
71
+ <<~RUBY.chomp
72
+ value_#{@index} =
73
+ if conditions_#{@index}.satisfy?(source, context)
74
+ #{read_code}
75
+ else
76
+ Serega::SeregaEngine::SKIP
77
+ end
78
+ RUBY
79
+ end
80
+
81
+ # Code that sets the variable that reads the value: the callable, the
82
+ # loaded batch of the only loader, or the attribute. nil for a plain
83
+ # attribute, which reads the source alone.
84
+ def value_variable
85
+ index = @index
86
+ return "callable_#{index} = @points[#{index}].attribute.value_callable" if @callable_code
87
+ return "batch_#{index} = batches[#{index}].fetch(#{@point.batch_loaders.first.inspect})" if @batch_code
88
+
89
+ "attribute_#{index} = @points[#{index}].attribute" unless @read_code
90
+ end
91
+
92
+ # Code that reads the value. An error raised there gets the attribute
93
+ # name. Only the rescue reads the point.
94
+ def rescued_read_code
95
+ <<~RUBY.chomp
96
+ begin
97
+ #{read_value_code}
98
+ rescue => error
99
+ Serega::SeregaUtils::SerializedAttributeError.call(error, @points[#{@index}])
100
+ end
101
+ RUBY
102
+ end
103
+
104
+ # Code that reads the value: the code of the plain value, the
105
+ # callable or the loaded batch, or a SeregaAttribute#value call. The
106
+ # code reads the `:const`, `:default` and formatter values from the
107
+ # constants of SeregaAttributeValues.
108
+ def read_value_code
109
+ read_code = @read_code
110
+ return read_code if read_code
111
+ return "attribute_#{@index}.value(source, context)" unless @batches_variable
112
+
113
+ "attribute_#{@index}.value(source, context, batches: #{@batches_variable})"
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ class SeregaResultCode # :nodoc:
5
+ #
6
+ # Code of one point in the generated methods. The local variables of the
7
+ # point have its index in their names, for example `value_1`.
8
+ #
9
+ class PointCode # :nodoc:
10
+ def initialize(point, index)
11
+ @point = point
12
+ @index = index
13
+ end
14
+
15
+ # Whether the value of the point can be SKIP
16
+ def conditional?
17
+ @point.conditional?
18
+ end
19
+
20
+ # Pair of the serialized Hash literal
21
+ def hash_pair
22
+ "#{key_code} => value_#{@index}"
23
+ end
24
+
25
+ # Code that adds the value to the serialized Hash. It adds no key for a
26
+ # skipped value, and with `compact` for a `nil` value.
27
+ def hash_assign(compact)
28
+ index = @index
29
+ code = "serialized_hash[#{key_code}] = value_#{index}"
30
+ skips = []
31
+ skips << "value_#{index}.nil?" if compact
32
+ skips << "Serega::SeregaEngine::SKIP.equal?(value_#{index})" if conditional?
33
+ skips.empty? ? code : "#{code} unless #{skips.join(" || ")}"
34
+ end
35
+
36
+ # Code of the Struct or Data argument with the value, nil for a skipped
37
+ # value
38
+ def argument
39
+ return "value_#{@index}" unless conditional?
40
+
41
+ "(Serega::SeregaEngine::SKIP.equal?(value_#{@index}) ? nil : value_#{@index})"
42
+ end
43
+
44
+ private
45
+
46
+ # Code of the key. A String key is a deduplicated frozen literal, thus
47
+ # adding it to a Hash allocates no copy.
48
+ def key_code
49
+ key = @point.key
50
+ key.is_a?(String) ? "-#{key.inspect}" : key.inspect
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ class SeregaResultCode # :nodoc:
5
+ #
6
+ # Code of a point with a child plan. Its values are the relation values
7
+ # of all sources, built by the child group. A relation value skipped by
8
+ # the :if and :unless conditions is SKIP already. The :if_value and
9
+ # :unless_value conditions check the serialized relation value.
10
+ #
11
+ # The point also writes its loop of the `#collect` method: it reads the
12
+ # relation source of each source with the code of an AttributePointCode,
13
+ # appends the sources of the relation source to `child_sources_N`, and
14
+ # writes the pull of each source to `pulls_N`. See SeregaEngine::Pulls.
15
+ #
16
+ class RelationPointCode < PointCode # :nodoc:
17
+ def initialize(point, index)
18
+ super
19
+ @source_code = AttributePointCode.new(point, index, check_value: false)
20
+ conditions = point.attribute.conditions
21
+ @check_value = conditions ? conditions.value_conditions? : false
22
+ end
23
+
24
+ # Code that sets the local variables of the relation values and the
25
+ # value conditions
26
+ def variables
27
+ index = @index
28
+ variables = ["relation_values_#{index} = relations[@points[#{index}]]"]
29
+ variables << "conditions_#{index} = @points[#{index}].attribute.conditions" if @check_value
30
+ variables
31
+ end
32
+
33
+ # Code that assigns the relation value of the source to `value_N`. A
34
+ # value that fails the value conditions gets SKIP.
35
+ def assign_value
36
+ index = @index
37
+ code = "value_#{index} = relation_values_#{index}[source_index]"
38
+ return code unless @check_value
39
+
40
+ "#{code}\n" \
41
+ "value_#{index} = Serega::SeregaEngine::SKIP unless Serega::SeregaEngine::SKIP.equal?(value_#{index}) || " \
42
+ "conditions_#{index}.satisfy_value?(value_#{index}, context)"
43
+ end
44
+
45
+ # A relation needs the source group, thus no `#call_one`
46
+ def call_one?
47
+ false
48
+ end
49
+
50
+ # Code of the `#collect` loop of the point
51
+ def collect
52
+ index = @index
53
+ variables = [*@source_code.variables, "child_sources_#{index} = []"]
54
+ variables << "collection_classes_#{index} = @points[#{index}].collection_classes" unless @point.many == false
55
+ <<~RUBY.chomp
56
+ #{variables.join("\n")}
57
+ pulls_#{index} = Array.new(size)
58
+ source_index = 0
59
+ while source_index < size
60
+ source = sources[source_index]
61
+ #{@source_code.assign_value.gsub("\n", "\n ")}
62
+ pulls_#{index}[source_index] =
63
+ #{pull_code.gsub("\n", "\n ")}
64
+ source_index += 1
65
+ end
66
+ RUBY
67
+ end
68
+
69
+ # Variables that `#collect` returns for the point
70
+ def collected
71
+ "child_sources_#{@index}, pulls_#{@index}"
72
+ end
73
+
74
+ private
75
+
76
+ # Code that appends the sources of `value_N` to `child_sources_N` and
77
+ # gives its pull, as SeregaEngine::Pulls.append does
78
+ def pull_code
79
+ index = @index
80
+ <<~RUBY.chomp
81
+ #{skip_branch}if value_#{index}.nil?
82
+ nil
83
+ #{collection_branch}else
84
+ child_sources_#{index} << value_#{index}
85
+ #{@point.many ? "1" : "Serega::SeregaEngine::Pulls::SINGLE_SOURCE"}
86
+ end
87
+ RUBY
88
+ end
89
+
90
+ # First branch of the pull code of a conditional relation: SKIP is the
91
+ # pull of a skipped relation source
92
+ def skip_branch
93
+ "if Serega::SeregaEngine::SKIP.equal?(value_#{@index})\n value_#{@index}\nels" if @source_code.conditional?
94
+ end
95
+
96
+ # Branch of the pull code that appends a collection, unless the
97
+ # relation has `many: false`. An Array is checked inline, without the
98
+ # call of SeregaUtils::CollectionDetector.
99
+ def collection_branch
100
+ return if @point.many == false
101
+
102
+ index = @index
103
+ <<~RUBY
104
+ elsif value_#{index}.instance_of?(Array) || Serega::SeregaUtils::CollectionDetector.call(value_#{index}, collection_classes_#{index})
105
+ collection = value_#{index}.to_a
106
+ child_sources_#{index}.concat(collection)
107
+ collection.size
108
+ RUBY
109
+ end
110
+ end
111
+ end
112
+ end