Module: Serega::SeregaPlugins::Formatters
- Defined in:
- lib/serega/plugins/formatters/formatters.rb
Overview
Plugin :formatters
Allows to define value formatters one time and apply them on any attributes.
Config option ‘config.formatters.add()` can be used to add formatters.
Attribute option ‘:format` now can be used with name of formatter or with callable instance.
Formatters can accept up to 2 parameters (formatted object, context)
Defined Under Namespace
Modules: AttributeInstanceMethods, AttributeNormalizerInstanceMethods, CheckAttributeParamsInstanceMethods, ConfigInstanceMethods Classes: CheckFormatter, CheckOptFormat, FormattersConfig
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 and loads additional plugins.
-
.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
92 93 94 95 96 97 |
# File 'lib/serega/plugins/formatters/formatters.rb', line 92 def self.after_load_plugin(serializer_class, **opts) config = serializer_class.config config.opts[:formatters] = {} config.formatters.add(opts[:formatters] || {}) config.attribute_keys << :format end |
.before_load_plugin(serializer_class, **opts) ⇒ void
This method returns an undefined value.
Checks requirements and loads additional plugins
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/serega/plugins/formatters/formatters.rb', line 58 def self.before_load_plugin(serializer_class, **opts) allowed_keys = %i[formatters] 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" \ " - :formatters [Hash<Symbol, #call>] - Formatters (names and according callable values)" end end |
.load_plugin(serializer_class, **_opts) ⇒ void
This method returns an undefined value.
Applies plugin code to specific serializer
77 78 79 80 81 82 |
# File 'lib/serega/plugins/formatters/formatters.rb', line 77 def self.load_plugin(serializer_class, **_opts) serializer_class::SeregaConfig.include(ConfigInstanceMethods) serializer_class::SeregaAttributeNormalizer.include(AttributeNormalizerInstanceMethods) serializer_class::SeregaAttribute.include(AttributeInstanceMethods) serializer_class::CheckAttributeParams.include(CheckAttributeParamsInstanceMethods) end |
.plugin_name ⇒ Symbol
Returns Plugin name.
47 48 49 |
# File 'lib/serega/plugins/formatters/formatters.rb', line 47 def self.plugin_name :formatters end |