Module: Serega::ClassMethods

Included in:
Serega
Defined in:
lib/serega.rb

Overview

Core serializer class methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configConfig (readonly)

Returns current serializer config.

Returns:

  • (Config)

    current serializer config



73
74
75
# File 'lib/serega.rb', line 73

def config
  @config
end

Instance Method Details

#as_json(object, opts = FROZEN_EMPTY_HASH) ⇒ Object



213
214
215
216
# File 'lib/serega.rb', line 213

def as_json(object, opts = FROZEN_EMPTY_HASH)
  initiate_keys = config[:initiate_keys]
  new(opts.slice(*initiate_keys)).as_json(object, opts.except(*initiate_keys))
end

#attribute(name, **opts, &block) ⇒ Serega::Attribute

Adds attribute

Parameters:

  • name (Symbol)

    Attribute name. Attribute value will be found by executing `object.<name>`

  • opts (Hash)

    Options to serialize attribute

  • block (Proc)

    Custom block to find attribute value. Accepts object and context.

Returns:



180
181
182
183
# File 'lib/serega.rb', line 180

def attribute(name, **opts, &block)
  attribute = self::Attribute.new(name: name, opts: opts, block: block)
  attributes[attribute.name] = attribute
end

#attributesHash

Lists attributes

Returns:

  • (Hash)

    attributes list



167
168
169
# File 'lib/serega.rb', line 167

def attributes
  @attributes ||= {}
end

#call(object, opts = FROZEN_EMPTY_HASH) ⇒ Object



199
200
201
202
# File 'lib/serega.rb', line 199

def call(object, opts = FROZEN_EMPTY_HASH)
  initiate_keys = config[:initiate_keys]
  new(opts.slice(*initiate_keys)).to_h(object, opts.except(*initiate_keys))
end

#plugin(name, **opts) ⇒ class<Module>

Enables plugin for current serializer

Parameters:

  • name (Symbol, Class<Module>)

    Plugin name or plugin module itself

  • opts (Hash)

    ] Plugin options

Returns:

  • (class<Module>)

    Loaded plugin module

Raises:



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/serega.rb', line 125

def plugin(name, **opts)
  raise Error, "This plugin is already loaded" if plugin_used?(name)

  plugin = Plugins.find_plugin(name)

  # We split loading of plugin to three parts - before_load, load, after_load:
  #
  # - **before_load_plugin** usually used to check requirements and to load additional plugins
  # - **load_plugin** usually used to include plugin modules
  # - **after_load_plugin** usually used to add config options
  plugin.before_load_plugin(self, **opts) if plugin.respond_to?(:before_load_plugin)
  plugin.load_plugin(self, **opts) if plugin.respond_to?(:load_plugin)
  plugin.after_load_plugin(self, **opts) if plugin.respond_to?(:after_load_plugin)

  # Store attached plugins, so we can check it is loaded later
  config[:plugins] << (plugin.respond_to?(:plugin_name) ? plugin.plugin_name : plugin)

  plugin
end

#plugin_used?(name) ⇒ Boolean

Checks plugin is used

Parameters:

  • name (Symbol, Class<Module>)

    Plugin name or plugin module itself

Returns:

  • (Boolean)


152
153
154
155
156
157
158
159
160
# File 'lib/serega.rb', line 152

def plugin_used?(name)
  plugin_name =
    case name
    when Module then name.respond_to?(:plugin_name) ? name.plugin_name : name
    else name
    end

  config[:plugins].include?(plugin_name)
end

#relation(name, serializer:, **opts, &block) ⇒ Serega::Attribute

Adds attribute with forced :serializer option

Parameters:

  • name (Symbol)

    Attribute name. Attribute value will be found by executing `object.<name>`

  • serializer (Serega, Proc)

    Specifies nested serializer for relationship

  • opts (Hash)

    Options for attribute serialization

  • block (Proc)

    Custom block to find attribute value. Accepts object and context.

Returns:



195
196
197
# File 'lib/serega.rb', line 195

def relation(name, serializer:, **opts, &block)
  attribute(name, serializer: serializer, **opts, &block)
end

#to_h(object, opts = FROZEN_EMPTY_HASH) ⇒ Object



204
205
206
# File 'lib/serega.rb', line 204

def to_h(object, opts = FROZEN_EMPTY_HASH)
  call(object, opts)
end

#to_json(object, opts = FROZEN_EMPTY_HASH) ⇒ Object



208
209
210
211
# File 'lib/serega.rb', line 208

def to_json(object, opts = FROZEN_EMPTY_HASH)
  initiate_keys = config[:initiate_keys]
  new(opts.slice(*initiate_keys)).to_json(object, opts.except(*initiate_keys))
end