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
data/lib/serega/plan.rb CHANGED
@@ -5,152 +5,111 @@ class Serega
5
5
  # Constructs plan - list of serialized attributes.
6
6
  # We will traverse this plan to construct serialized response.
7
7
  #
8
- # @private
9
- class SeregaPlan
10
- #
11
- # SeregaPlan class methods
12
- #
13
- # @private
14
- module ClassMethods
15
- #
16
- # Constructs plan of attributes that should be serialized.
17
- #
18
- # @param opts Serialization parameters
19
- # @option opts [Hash] :only The only attributes to serialize
20
- # @option opts [Hash] :except Attributes to hide
21
- # @option opts [Hash] :with Attributes (usually marked `hide: true`) to serialize additionally
22
- #
23
- # @return [SeregaPlan] Serialization plan
24
- #
25
- def call(opts)
26
- max_cache_size = serializer_class.config.max_cached_plans_per_serializer_count
27
- return new(nil, opts) if max_cache_size.zero?
28
-
29
- cached_plan_for(opts, max_cache_size)
30
- end
31
-
32
- #
33
- # Returns (and caches) the Data class for the given set of field names.
34
- # Uses the Array as cache key so the same Data class is reused across
35
- # all plan instances with identical fields.
36
- #
37
- # @param point_names [Array<Symbol>] Attribute names for the Data members
38
- # @return [Class] Subclass of Data
39
- #
40
- def data_class_for(point_names)
41
- (@data_classes ||= {})[point_names] ||= Data.define(*point_names)
42
- end
43
-
44
- private
45
-
46
- def cached_plan_for(opts, max_cache_size)
47
- @cache ||= {}
48
- cache_key = construct_cache_key(opts)
49
-
50
- plan = @cache[cache_key] ||= new(nil, opts)
51
- @cache.shift if @cache.length > max_cache_size
52
- plan
53
- end
54
-
55
- def construct_cache_key(opts, cache_key = nil)
56
- return nil if opts.empty?
8
+ class SeregaPlan # :nodoc:
9
+ # Serializer of the plan
10
+ attr_reader :serializer_class
11
+
12
+ # Parent plan point
13
+ attr_reader :parent_plan_point
14
+
15
+ # Serialization points
16
+ attr_reader :points
17
+
18
+ # Serialization points with child plans
19
+ attr_reader :relation_points
20
+
21
+ # Whether the root objects of a class are collections
22
+ attr_reader :collection_classes
23
+
24
+ # Presenter class that wraps the sources in the Ractor of the plan, `nil`
25
+ # when no point reads the sources through the presenter
26
+ attr_reader :presenter
27
+
28
+ # Levels of child plans the depth limit of the root serializer allows
29
+ # below this plan, `nil` without a limit. See DepthLimit.
30
+ attr_reader :depth_left
31
+
32
+ # Instantiate new serialization plan of the selected attributes of the
33
+ # selection's serializer, with the result builder class of the
34
+ # selection. A child plan gets the point of its parent plan. Raises
35
+ # DepthLimitError when the plan nests deeper than the depth limit of the
36
+ # root serializer.
37
+ def initialize(selection, parent_plan_point = nil)
38
+ serializer_class = selection.serializer_class
39
+ serializer_class.lock
40
+ @serializer_class = serializer_class
41
+ @parent_plan_point = parent_plan_point
42
+ @depth_left = DepthLimit.depth_left(serializer_class, parent_plan_point)
43
+ @builder_class = selection.builder_class
44
+ @points = selected_points(selection)
45
+ @presenter = points_presenter(serializer_class)
46
+ @relation_points = points.select(&:child_plan).freeze
47
+ @preload_points = points.select(&:preloads).freeze
48
+ @batch_points = points.any? { |point| !point.batch_loaders.empty? }
49
+ @flat = relation_points.empty? && preload_points.empty? && !@batch_points && !@presenter
50
+ @result_builders = {}
51
+ @collection_classes = {}.compare_by_identity
52
+ direct = parent_plan_point.nil? && @flat && !serializer_class.prepare_initial_objects_signature && @builder_class.equal?(SeregaResultBuilder)
53
+ @direct_hash_builder = direct ? result_builder(:hash) : nil
54
+ # :nocov:
55
+ rescue *RACTOR_ISOLATION_ERRORS
56
+ raise if serializer_class.shareable?
57
+
58
+ raise SeregaError, "#{serializer_class} can not serialize in other Ractors. Call `make_shareable!` at the end of its definition"
59
+ # :nocov:
60
+ end
57
61
 
58
- cache_key ||= +""
62
+ # Serialization points with preloads
63
+ attr_reader :preload_points
59
64
 
60
- opts.each do |key, nested_opts|
61
- cache_key.insert(-1, SeregaUtils::SymbolName.call(key))
62
- cache_key.insert(-1, "-")
63
- construct_cache_key(nested_opts, cache_key)
64
- end
65
+ # Hash result builder of a flat root plan whose serializer prepares no
66
+ # initial objects and whose builder class is SeregaResultBuilder.
67
+ # `Serega#call` builds one source with it directly.
68
+ attr_reader :direct_hash_builder
65
69
 
66
- cache_key
67
- end
70
+ # Whether any point uses batch loaders
71
+ def batch_points?
72
+ @batch_points
68
73
  end
69
74
 
70
- #
71
- # SeregaPlan instance methods
72
- #
73
- # @private
74
- module InstanceMethods
75
- # Parent plan point
76
- # @return [SeregaPlanPoint, nil]
77
- attr_reader :parent_plan_point
78
-
79
- # Serialization points
80
- # @return [Array<SeregaPlanPoint>] points to serialize
81
- attr_reader :points
82
-
83
- # Named serialization points
84
- # @return [Hash] Named points
85
- attr_reader :points_hash
86
-
87
- #
88
- # Instantiate new serialization plan
89
- #
90
- # Patched by
91
- # - depth_limit plugin, which checks depth limit is not exceeded when adding new plan
92
- #
93
- # @param parent_plan_point [SeregaPlanPoint, nil] Parent plan_point
94
- # @param modifiers [Hash] Serialization parameters
95
- # @option modifiers [Hash] :only The only attributes to serialize
96
- # @option modifiers [Hash] :except Attributes to hide
97
- # @option modifiers [Hash] :with Hidden attributes to serialize additionally
98
- #
99
- # @return [SeregaPlan] Serialization plan
100
- #
101
- def initialize(parent_plan_point, modifiers)
102
- @parent_plan_point = parent_plan_point
103
- @points = attributes_points(modifiers)
104
- @points_hash = points.to_h { |point| [point.name, point] }
105
- end
106
-
107
- #
108
- # Serializer class of current plan
109
- #
110
- def serializer_class
111
- self.class.serializer_class
112
- end
75
+ # A flat plan has no relations, preloads, batch loaders and presenter.
76
+ # Its sources need no source groups.
77
+ def flat?
78
+ @flat
79
+ end
113
80
 
114
- # Returns the Data class whose members match this plan's serialized fields.
115
- # Delegates to the class-level cache so identical field sets share one Data class.
116
- #
117
- # @return [Class] Subclass of Data
118
- #
119
- def data_class
120
- @data_class ||= self.class.data_class_for(point_names)
81
+ # Plan that starts the serialization: this plan, or the root plan of
82
+ # its parent point
83
+ def root_plan
84
+ plan = self
85
+ while (point = plan.parent_plan_point)
86
+ plan = point.plan
121
87
  end
88
+ plan
89
+ end
122
90
 
123
- private
124
-
125
- def attributes_points(modifiers)
126
- only = modifiers[:only] || FROZEN_EMPTY_HASH
127
- except = modifiers[:except] || FROZEN_EMPTY_HASH
128
- with = modifiers[:with] || FROZEN_EMPTY_HASH
129
- points = []
130
-
131
- serializer_class.attributes.each_value do |attribute|
132
- next unless attribute.visible?(only: only, except: except, with: with)
91
+ # Result builder of this plan in the serialization mode, made with the
92
+ # builder class of the selection
93
+ def result_builder(mode)
94
+ @result_builders[mode] ||= @builder_class.new(serializer_class, mode, points)
95
+ end
133
96
 
134
- child_fields =
135
- if attribute.relation?
136
- name = attribute.name
137
- {only: only[name], with: with[name], except: except[name]}
138
- end
97
+ private
139
98
 
140
- point = serializer_class::SeregaPlanPoint.new(self, attribute, child_fields)
141
- points << point.freeze
142
- end
99
+ # Presenter class of the Ractor, when a point reads the sources through it
100
+ def points_presenter(serializer_class)
101
+ presenter = serializer_class.local_presenter
102
+ presenter if presenter && points.any? { |point| point.attribute.reads_presenter? }
103
+ end
143
104
 
144
- points.freeze
105
+ # Makes a point of each selected attribute, with the preloads and child
106
+ # selection the selection gives it
107
+ def selected_points(selection)
108
+ points = selection.attributes.map do |attribute|
109
+ Point.new(self, attribute, selection.preloads(attribute), selection.child(attribute)).freeze
145
110
  end
146
111
 
147
- def point_names
148
- @point_names ||= points.map(&:name)
149
- end
112
+ points.freeze
150
113
  end
151
-
152
- extend ClassMethods
153
- include InstanceMethods
154
- extend SeregaHelpers::SerializerClassHelper
155
114
  end
156
115
  end
@@ -2,97 +2,30 @@
2
2
 
3
3
  class Serega
4
4
  module SeregaPlugins
5
+ # `plugin :activerecord_preloads` loads every `:preload` of the serialized
6
+ # fields once, with `ActiveRecord::Associations::Preloader`
5
7
  #
6
- # Plugin :activerecord_preloads
7
- #
8
- # Automatically preloads associations to serialized objects
9
- #
10
- # Every association declared with `:preload` is loaded once during serialization using
11
- # ActiveRecord::Associations::Preloader, so there are no N+1 queries.
12
- #
13
- # @example
14
- # class AppSerializer < Serega
15
- # config.auto_preload = true
16
- # plugin :activerecord_preloads
17
- # end
18
- #
19
- # class AlbumSerializer < AppSerializer
20
- # # no preloads
21
- # attribute :title
22
- #
23
- # # preloads :downloads, as manually specified
24
- # attribute :downloads_count, preload: :downloads, value: proc { |album| album.downloads.count }
25
- # end
26
- #
27
- # class UserSerializer < AppSerializer
28
- # # no preloads
29
- # attribute :username
30
- #
31
- # # preloads :user_stats, as auto_preload is enabled for :delegate attributes
32
- # attribute :comments_count, delegate: { to: :user_stats }
33
- #
34
- # # preloads :albums, as auto_preload is enabled for :serializer attributes
35
- # attribute :albums, serializer: AlbumSerializer
36
- # end
37
- #
38
- # UserSerializer.to_h(users)
39
- # # 1 query to load :user_stats for all users
40
- # # + 1 query to load :albums for all users
41
- # # + 1 query to load :downloads for all albums
42
- # # = 3 queries total, regardless of how many users/albums are serialized
8
+ # ```ruby
9
+ # class AppSerializer < Serega
10
+ # plugin :activerecord_preloads
11
+ # config.auto_preload = true
12
+ # end
13
+ # ```
43
14
  #
15
+ # See [ActiveRecord preloads](https://github.com/andreyruby/serega/blob/master/docs/preloads.md#activerecord).
44
16
  module ActiverecordPreloads
45
- #
46
- # @return [Symbol] Plugin name
47
- #
48
- # @private
49
- def self.plugin_name
50
- :activerecord_preloads
51
- end
52
-
53
- # Checks requirements to load plugin
54
- #
55
- # @param serializer_class [Class<Serega>] Current serializer class
56
- # @param opts [Hash] plugin options
57
- #
58
- # @return [void]
59
- #
60
- # @private
61
- def self.before_load_plugin(serializer_class, **opts)
62
- opts.each_key do |key|
63
- raise SeregaError, "Plugin #{plugin_name.inspect} does not accept the #{key.inspect} option. No options are allowed"
17
+ # Adds the plugin to the serializer
18
+ def self.apply(serializer_class, **opts) # :nodoc:
19
+ unless opts.empty?
20
+ raise SeregaError, "Plugin :activerecord_preloads does not accept the #{opts.keys.first.inspect} option. No options are allowed"
64
21
  end
65
- end
66
22
 
67
- #
68
- # Applies plugin code to specific serializer
69
- #
70
- # @param serializer_class [Class<Serega>] Current serializer class
71
- # @param _opts [Hash] Plugin options
72
- #
73
- # @return [void]
74
- #
75
- # @private
76
- def self.load_plugin(serializer_class, **_opts)
77
23
  require_relative "lib/preloader"
78
- end
79
24
 
80
- #
81
- # Registers the ActiveRecord preload handler
82
- #
83
- # @param serializer_class [Class<Serega>] Current serializer class
84
- # @param _opts [Hash] Plugin options
85
- #
86
- # @return [void]
87
- #
88
- # @private
89
- def self.after_load_plugin(serializer_class, **_opts)
90
25
  serializer_class.preload_with do |objects, preloads|
91
26
  Preloader.preload(objects, preloads)
92
27
  end
93
28
  end
94
29
  end
95
-
96
- register_plugin(ActiverecordPreloads.plugin_name, ActiverecordPreloads)
97
30
  end
98
31
  end
@@ -4,16 +4,9 @@ class Serega
4
4
  module SeregaPlugins
5
5
  module ActiverecordPreloads
6
6
  # Handles preloads for different types of initial records
7
- # @private
8
- class Preloader
7
+ class Preloader # :nodoc:
9
8
  class << self
10
9
  # Preloads associations to records
11
- #
12
- # @param object [Object] record(s)
13
- # @param preloads [Hash] associations names
14
- #
15
- # @return [Object] provided object with preloaded associations
16
- #
17
10
  def preload(object, preloads)
18
11
  return object if object.nil? || (object.is_a?(Array) && object.empty?)
19
12
 
@@ -23,9 +16,7 @@ class Serega
23
16
  preload_handler.preload(object, preloads)
24
17
  end
25
18
 
26
- # Returns handlers which will try to check if serialized object fits for preloading using this handler
27
- #
28
- # @return [Array] Registered preload adapters for different types of initial records
19
+ # Returns handlers which will try to check if the object to serialize fits for preloading using this handler
29
20
  def handlers
30
21
  @handlers ||= [
31
22
  ActiverecordRelation,
@@ -38,47 +29,22 @@ class Serega
38
29
  end
39
30
 
40
31
  # Associations loader for prepared records
41
- # @private
42
- class Loader
43
- # :nocov: We can check only one version of activerecord
44
-
32
+ class Loader # :nodoc:
45
33
  # Preloads associations to records
46
- #
47
- # @param records [Array<ActiveRecord::Base>] records
48
- # @param associations [Hash] associations names
49
- #
50
- # @return [void]
51
- #
52
34
  def self.call(records, associations)
53
- if ActiveRecord::VERSION::MAJOR >= 7
54
- ActiveRecord::Associations::Preloader.new(records: records, associations: associations).call
55
- else
56
- ActiveRecord::Associations::Preloader.new.preload(records, associations)
57
- end
35
+ ActiveRecord::Associations::Preloader.new(records: records, associations: associations).call
58
36
  end
59
- # :nocov:
60
37
  end
61
38
 
62
39
  # Preloader adapter for ActiveRecord object
63
- # @private
64
- class ActiverecordObject
40
+ class ActiverecordObject # :nodoc:
65
41
  class << self
66
- #
67
42
  # Checks object is kind of ActiveRecord::Base
68
- #
69
- # @param object [Object] object
70
- #
71
- # @return [Boolean] whether object is kind of ActiveRecord::Base
72
43
  def fit?(object)
73
44
  object.is_a?(ActiveRecord::Base)
74
45
  end
75
46
 
76
- #
77
47
  # Preloads associations to ActiveRecord::Base record
78
- #
79
- # @param record [ActiveRecord::Base] record
80
- #
81
- # @return [Object] provided record with preloaded associations
82
48
  def preload(record, preloads)
83
49
  Loader.call([record], preloads)
84
50
  record
@@ -87,25 +53,14 @@ class Serega
87
53
  end
88
54
 
89
55
  # Preloader adapter for ActiveRecord::Relation
90
- # @private
91
- class ActiverecordRelation
56
+ class ActiverecordRelation # :nodoc:
92
57
  class << self
93
- #
94
58
  # Checks object is kind of ActiveRecord::Relation
95
- #
96
- # @param object [Object] object to check
97
- #
98
- # @return [Boolean] whether object is kind of ActiveRecord::Relation
99
59
  def fit?(object)
100
60
  object.is_a?(ActiveRecord::Relation)
101
61
  end
102
62
 
103
- #
104
63
  # Preloads associations to ActiveRecord::Relation
105
- #
106
- # @param scope [ActiveRecord::Relation] scope
107
- #
108
- # @return [ActiveRecord::Relation] provided scope with preloaded associations
109
64
  def preload(scope, preloads)
110
65
  scope.load
111
66
  Loader.call(scope.to_a, preloads)
@@ -115,27 +70,16 @@ class Serega
115
70
  end
116
71
 
117
72
  # Preloader adapter for Array of ActiveRecord objects
118
- # @private
119
- class ActiverecordArray
73
+ class ActiverecordArray # :nodoc:
120
74
  class << self
121
- #
122
75
  # Checks object is an array of ActiveRecord::Base objects
123
- #
124
- # @param object [Object] object
125
- #
126
- # @return [Boolean] whether object is an array with ActiveRecord objects (and all objects have same class)
127
76
  def fit?(object)
128
77
  object.is_a?(Array) &&
129
78
  ActiverecordObject.fit?(object.first) &&
130
79
  same_kind?(object)
131
80
  end
132
81
 
133
- #
134
82
  # Preloads associations to array with ActiveRecord::Base objects
135
- #
136
- # @param records [Array<ActiveRecord::Base>] ActiveRecord records
137
- #
138
- # @return [Array<ActiveRecord::Base>] provided records with preloaded associations
139
83
  def preload(records, preloads)
140
84
  Loader.call(records, preloads)
141
85
  records
@@ -151,26 +95,15 @@ class Serega
151
95
  end
152
96
 
153
97
  # Preloader adapter for Enumerator with ActiveRecord objects
154
- # @private
155
- class ActiverecordEnumerator
98
+ class ActiverecordEnumerator # :nodoc:
156
99
  class << self
157
- #
158
100
  # Checks object is an Enumerator with each value is a ActiveRecord::Base object
159
- #
160
- # @param object [Object] object
161
- #
162
- # @return [Boolean] whether object is an Enumerator with each value is a ActiveRecord::Base object
163
101
  def fit?(object)
164
102
  object.is_a?(Enumerator) &&
165
103
  ActiverecordArray.fit?(object.to_a)
166
104
  end
167
105
 
168
- #
169
106
  # Preloads associations to Enumerator with ActiveRecord::Base objects
170
- #
171
- # @param enum [Enumerator<ActiveRecord::Base>] enum
172
- #
173
- # @return [Enumerator<ActiveRecord::Base>] provided enumerator with preloaded associations
174
107
  def preload(enum, preloads)
175
108
  ActiverecordArray.preload(enum.to_a, preloads)
176
109
  enum