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.

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)

    Plugin options



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

Parameters:

  • serializer_class (Class<Serega>)

    Current serializer class

  • opts (Hash)

    plugin options



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

Parameters:

  • serializer_class (Class<Serega>)

    Current serializer class

  • _opts (Hash)

    Plugin options



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_nameSymbol

Returns Plugin name.

Returns:

  • (Symbol)

    Plugin name



41
42
43
# File 'lib/serega/plugins/camel_case/camel_case.rb', line 41

def self.plugin_name
  :camel_case
end