Module: Serega::SeregaPlugins::CamelCase
- Defined in:
- lib/serega/plugins/camel_case/camel_case.rb
Overview
Plugin :camel_case
By default when we add attribute like ‘attribute :first_name` this means:
-
adding a ‘:first_name` key to resulted hash
-
adding a ‘#first_name` method call result as value
But its often desired to response with camelCased keys. Earlier this can be achieved by specifying attribute name and method directly for each attribute: ‘attribute :firstName, method: first_name`
Now this plugin transforms all attribute names automatically. We use simple regular expression to replace ‘_x` to `X` for the whole string. You can provide your own callable transformation when defining plugin, for example `plugin :camel_case, transform: ->(name) { name.camelize }`
For any attribute camelCase-behavior can be skipped when ‘camel_case: false` attribute option provided.
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
91 92 93 94 95 96 97 |
# File 'lib/serega/plugins/camel_case/camel_case.rb', line 91 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
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/serega/plugins/camel_case/camel_case.rb', line 58 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
77 78 79 80 81 |
# File 'lib/serega/plugins/camel_case/camel_case.rb', line 77 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.
47 48 49 |
# File 'lib/serega/plugins/camel_case/camel_case.rb', line 47 def self.plugin_name :camel_case end |