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.

Examples:

Define plugin

class AppSerializer < Serega
  plugin :camel_case
end

class UserSerializer < AppSerializer
  attribute :first_name
  attribute :last_name
  attribute :full_name, camel_case: false, value: proc { |user| [user.first_name, user.last_name].compact.join(" ") }
end

require "ostruct"
user = OpenStruct.new(first_name: "Bruce", last_name: "Wayne")
UserSerializer.to_h(user) # {firstName: "Bruce", lastName: "Wayne", full_name: "Bruce Wayne"}

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

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

Parameters:

  • serializer_class (Class<Serega>)

    Current serializer class

  • opts (Hash)

    loaded plugins opts



73
74
75
76
77
78
79
# File 'lib/serega/plugins/camel_case/camel_case.rb', line 73

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

.load_plugin(serializer_class, **_opts) ⇒ void

This method returns an undefined value.

Applies plugin code to specific serializer

Parameters:

  • serializer_class (Class<Serega>)

    Current serializer class

  • _opts (Hash)

    Loaded plugins options



59
60
61
62
63
# File 'lib/serega/plugins/camel_case/camel_case.rb', line 59

def self.load_plugin(serializer_class, **_opts)
  serializer_class::SeregaConfig.include(ConfigInstanceMethods)
  serializer_class::SeregaAttributeNormalizer.include(AttributeNormalizerInstanceMethods)
  serializer_class::CheckAttributeParams.include(CheckAttributeParamsInstanceMethods)
end

.plugin_nameSymbol

Returns Plugin name.

Returns:

  • (Symbol)

    Plugin name



47
48
49
# File 'lib/serega/plugins/camel_case/camel_case.rb', line 47

def self.plugin_name
  :camel_case
end