Module: Serega::SeregaPlugins::Preloads
- Defined in:
- lib/serega/plugins/preloads/preloads.rb,
lib/serega/plugins/preloads/lib/preload_paths.rb,
lib/serega/plugins/preloads/lib/modules/config.rb,
lib/serega/plugins/preloads/lib/preloads_config.rb,
lib/serega/plugins/preloads/lib/modules/attribute.rb,
lib/serega/plugins/preloads/lib/modules/plan_point.rb,
lib/serega/plugins/preloads/lib/format_user_preloads.rb,
lib/serega/plugins/preloads/lib/preloads_constructor.rb,
lib/serega/plugins/preloads/validations/check_opt_preload.rb,
lib/serega/plugins/preloads/lib/modules/attribute_normalizer.rb,
lib/serega/plugins/preloads/lib/modules/check_attribute_params.rb,
lib/serega/plugins/preloads/validations/check_opt_preload_path.rb
Overview
Plugin ‘:preloads`
Allows to define ‘:preloads` to attributes and then allows to merge preloads from serialized attributes and return single associations hash.
Plugin accepts options:
-
‘auto_preload_attributes_with_delegate` - default false
-
‘auto_preload_attributes_with_serializer` - default false
-
‘auto_hide_attributes_with_preload` - default false
This options are very handy if you want to forget about finding preloads manually.
Preloads can be disabled with ‘preload: false` attribute option. Also automatically added preloads can be overwritten with manually specified `preload: :another_value`.
Some examples, **please read comments in the code below**
Defined Under Namespace
Modules: AttributeInstanceMethods, AttributeNormalizerInstanceMethods, CheckAttributeParamsInstanceMethods, ConfigInstanceMethods, InstanceMethods, PlanPointInstanceMethods Classes: CheckOptPreload, CheckOptPreloadPath, FormatUserPreloads, PreloadPaths, PreloadsConfig, PreloadsConstructor
Class Method Summary collapse
-
.after_load_plugin(serializer_class, **opts) ⇒ void
Adds config options and runs other callbacks after plugin was loaded.
-
.before_load_plugin(serializer_class, **opts) ⇒ void
Checks requirements to load plugin.
-
.load_plugin(serializer_class, **_opts) ⇒ void
Applies plugin code to specific serializer.
-
.plugin_name ⇒ Symbol
Plugin name.
Class Method Details
.after_load_plugin(serializer_class, **opts) ⇒ void
This method returns an undefined value.
Adds config options and runs other callbacks after plugin was loaded
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/serega/plugins/preloads/preloads.rb', line 131 def self.after_load_plugin(serializer_class, **opts) config = serializer_class.config config.attribute_keys << :preload << :preload_path preloads_opts = DEFAULT_CONFIG.merge(opts.slice(*DEFAULT_CONFIG.keys)) config.opts[:preloads] = {} preloads_config = config.preloads preloads_config.auto_preload_attributes_with_delegate = preloads_opts[:auto_preload_attributes_with_delegate] preloads_config.auto_preload_attributes_with_serializer = preloads_opts[:auto_preload_attributes_with_serializer] preloads_config.auto_hide_attributes_with_preload = preloads_opts[:auto_hide_attributes_with_preload] end |
.before_load_plugin(serializer_class, **opts) ⇒ void
This method returns an undefined value.
Checks requirements to load plugin
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/serega/plugins/preloads/preloads.rb', line 80 def self.before_load_plugin(serializer_class, **opts) allowed_keys = DEFAULT_CONFIG.keys opts.each_key do |key| next if allowed_keys.include?(key) raise SeregaError, "Plugin #{plugin_name.inspect} does not accept the #{key.inspect} option. Allowed options:\n" \ " - :auto_preload_attributes_with_delegate [Boolean] - Automatically adds `preload: <delegate_to>` option to attributes with :delegate option specified\n" \ " - :auto_preload_attributes_with_serializer [Boolean] - Automatically adds `preload: <attribute_name>` option to attributes with :serializer option specified\n" \ " - :auto_hide_attributes_with_preload [Boolean] - Automatically adds `hide: true` option to attributes with :preload option (specified manually or added automatically)" end end |
.load_plugin(serializer_class, **_opts) ⇒ void
This method returns an undefined value.
Applies plugin code to specific serializer
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/serega/plugins/preloads/preloads.rb', line 101 def self.load_plugin(serializer_class, **_opts) require_relative "lib/format_user_preloads" require_relative "lib/modules/attribute" require_relative "lib/modules/attribute_normalizer" require_relative "lib/modules/check_attribute_params" require_relative "lib/modules/config" require_relative "lib/modules/plan_point" require_relative "lib/preload_paths" require_relative "lib/preloads_config" require_relative "lib/preloads_constructor" require_relative "validations/check_opt_preload" require_relative "validations/check_opt_preload_path" serializer_class.include(InstanceMethods) serializer_class::SeregaAttribute.include(AttributeInstanceMethods) serializer_class::SeregaAttributeNormalizer.include(AttributeNormalizerInstanceMethods) serializer_class::SeregaConfig.include(ConfigInstanceMethods) serializer_class::SeregaPlanPoint.include(PlanPointInstanceMethods) serializer_class::CheckAttributeParams.include(CheckAttributeParamsInstanceMethods) end |
.plugin_name ⇒ Symbol
Returns Plugin name.
69 70 71 |
# File 'lib/serega/plugins/preloads/preloads.rb', line 69 def self.plugin_name :preloads end |