Module: Serega::SeregaPlugins::CamelCase
- Defined in:
- lib/serega/plugins/camel_case/camel_case.rb
Overview
Plugin :camel_case
CamelCases every attribute name automatically, replacing _x with X
throughout the string. Runs once, when the attribute is defined, not on
every serialization.
Provide a custom transformation with
plugin :camel_case, transform: ->(name) { name.camelize }.
Skip camelCase for a single attribute with camel_case: false.
Defined Under Namespace
Modules: AttributeNormalizerInstanceMethods, CheckAttributeParamsInstanceMethods, ConfigInstanceMethods Classes: CamelCaseConfig, CheckOptCamelCase
Constant Summary collapse
- TRANSFORM_DEFAULT =
Default camel-case transformation
proc { |attribute_name| attribute_name.gsub!(/_[a-z]/) { |m| m[-1].upcase! } attribute_name }
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
88 89 90 91 92 93 94 |
# File 'lib/serega/plugins/camel_case/camel_case.rb', line 88 def self.after_load_plugin(serializer_class, **opts) config = serializer_class.config config.opts[:camel_case] = {} config.camel_case.transform = opts[:transform] || TRANSFORM_DEFAULT config.attribute_keys << :camel_case end |
.before_load_plugin(serializer_class, **opts) ⇒ void
This method returns an undefined value.
Checks requirements to load plugin
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/serega/plugins/camel_case/camel_case.rb', line 53 def self.before_load_plugin(serializer_class, **opts) allowed_keys = %i[transform] 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" \ " - :transform [#call] - Custom transformation" end end |
.load_plugin(serializer_class, **_opts) ⇒ void
This method returns an undefined value.
Applies plugin code to specific serializer
73 74 75 76 77 |
# File 'lib/serega/plugins/camel_case/camel_case.rb', line 73 def self.load_plugin(serializer_class, **_opts) serializer_class::SeregaConfig.include(ConfigInstanceMethods) serializer_class::SeregaAttributeNormalizer.include(AttributeNormalizerInstanceMethods) serializer_class::CheckAttributeParams.include(CheckAttributeParamsInstanceMethods) end |
.plugin_name ⇒ Symbol
Returns Plugin name.
41 42 43 |
# File 'lib/serega/plugins/camel_case/camel_case.rb', line 41 def self.plugin_name :camel_case end |