Module: Serega::SeregaPlugins::Root

Defined in:
lib/serega/plugins/root/root.rb

Overview

Plugin :root

Adds a root key to serialized data.

Accepts options:

  • :root - specifies root for all responses
  • :root_one - specifies root for single object serialization only
  • :root_many - specifies root for multiple objects serialization only

Adds additional config options:

- config.root.one
- config.root.many
- config.root.one=
- config.root.many=

Default root is :data.

Root can also be changed per serialization, or removed entirely by providing root: nil (per serialization it can still be added back).

Examples:

Define plugin

class UserSerializer < Serega
  plugin :root # default root is :data
end

class UserSerializer < Serega
  plugin :root, root: :users
end

class UserSerializer < Serega
  plugin :root, root_one: :user, root_many: :people
end

class UserSerializer < Serega
  plugin :root, root: nil # no root by default
end

Change root per serialization:

class UserSerializer < Serega
  plugin :root
end

UserSerializer.to_h(nil)              # => {:data=>nil}
UserSerializer.to_h(nil, root: :user) # => {:user=>nil}
UserSerializer.to_h(nil, root: nil)   # => nil

Defined Under Namespace

Modules: ClassMethods, ConfigInstanceMethods, DataBuilderClassMethods, InstanceMethods Classes: RootConfig

Constant Summary collapse

ROOT_DEFAULT =

Returns Default response root key.

Returns:

  • (Symbol)

    Default response root key

:data

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



108
109
110
111
112
113
114
115
116
117
# File 'lib/serega/plugins/root/root.rb', line 108

def self.after_load_plugin(serializer_class, **opts)
  config = serializer_class.config
  default = opts.fetch(:root, ROOT_DEFAULT)
  one = opts.fetch(:root_one, default)
  many = opts.fetch(:root_many, default)
  config.opts[:root] = {}
  config.root = {one: one, many: many}

  config.serialize_keys << :root
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



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/serega/plugins/root/root.rb', line 70

def self.before_load_plugin(serializer_class, **opts)
  allowed_keys = %i[root root_one root_many]
  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" \
      "  - :root [String, Symbol, nil] Specifies common root keyword used when serializing one or multiple objects\n" \
      "  - :root_one [String, Symbol, nil] Specifies root keyword used when serializing one object\n" \
      "  - :root_many [String, Symbol, nil] Specifies root keyword used when serializing multiple objects" \
  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



92
93
94
95
96
97
# File 'lib/serega/plugins/root/root.rb', line 92

def self.load_plugin(serializer_class, **_opts)
  serializer_class.extend(ClassMethods)
  serializer_class.include(InstanceMethods)
  serializer_class::SeregaConfig.include(ConfigInstanceMethods)
  serializer_class::SeregaDataBuilder.extend(DataBuilderClassMethods)
end

.plugin_nameSymbol

Returns Plugin name.

Returns:

  • (Symbol)

    Plugin name



58
59
60
# File 'lib/serega/plugins/root/root.rb', line 58

def self.plugin_name
  :root
end