Module: Serega::ClassMethods

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

Overview

Serializers class methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configSeregaConfig (readonly)

Returns current config

Returns:



76
77
78
# File 'lib/serega.rb', line 76

def config
  @config
end

Instance Method Details

#as_json(object, opts = nil) ⇒ Hash

Serializes provided object as JSON

Parameters:

  • object (Object)

    Serialized object

  • opts (Hash, nil) (defaults to: nil)

    Serializer modifiers and other instantiating options

Options Hash (opts):

  • :only (Array, Hash, String, Symbol)

    The only attributes to serialize

  • :except (Array, Hash, String, Symbol)

    Attributes to hide

  • :with (Array, Hash, String, Symbol)

    Attributes (usually hidden) to serialize additionally

  • :validate (Boolean)

    Validates provided modifiers (Default is true)

  • :context (Hash)

    Serialization context

  • :many (Boolean)

    Set true if provided multiple objects (Default ‘object.is_a?(Enumerable)`)

Returns:

  • (Hash)

    Serialization result



227
228
229
# File 'lib/serega.rb', line 227

def as_json(object, opts = nil)
  config.from_json.call(to_json(object, opts))
end

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

Adds attribute

Patched in:

  • plugin :presenter (additionally adds method in Presenter class)

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:



143
144
145
146
# File 'lib/serega.rb', line 143

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

#attributesHash

Lists attributes

Returns:

  • (Hash)

    attributes list



127
128
129
# File 'lib/serega.rb', line 127

def attributes
  @attributes ||= {}
end

#call(object, opts = nil) ⇒ Hash

Serializes provided object to Hash

Parameters:

  • object (Object)

    Serialized object

  • opts (Hash, nil) (defaults to: nil)

    Serializer modifiers and other instantiating options

Options Hash (opts):

  • :only (Array, Hash, String, Symbol)

    The only attributes to serialize

  • :except (Array, Hash, String, Symbol)

    Attributes to hide

  • :with (Array, Hash, String, Symbol)

    Attributes (usually hidden) to serialize additionally

  • :validate (Boolean)

    Validates provided modifiers (Default is true)

  • :context (Hash)

    Serialization context

  • :many (Boolean)

    Set true if provided multiple objects (Default ‘object.is_a?(Enumerable)`)

Returns:

  • (Hash)

    Serialization result



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/serega.rb', line 162

def call(object, opts = nil)
  opts ||= FROZEN_EMPTY_HASH
  initiate_keys = config.initiate_keys

  if opts.empty?
    modifiers_opts = FROZEN_EMPTY_HASH
    serialize_opts = nil
  else
    serialize_opts = opts.except(*initiate_keys)
    modifiers_opts = opts.slice(*initiate_keys)
  end

  new(modifiers_opts).to_h(object, serialize_opts)
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:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/serega.rb', line 85

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

  plugin = SeregaPlugins.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)

    Is plugin used



112
113
114
115
116
117
118
119
120
# File 'lib/serega.rb', line 112

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

#to_h(object, opts = nil) ⇒ Hash

Serializes provided object to Hash

Parameters:

  • object (Object)

    Serialized object

  • opts (Hash, nil) (defaults to: nil)

    Serializer modifiers and other instantiating options

Options Hash (opts):

  • :only (Array, Hash, String, Symbol)

    The only attributes to serialize

  • :except (Array, Hash, String, Symbol)

    Attributes to hide

  • :with (Array, Hash, String, Symbol)

    Attributes (usually hidden) to serialize additionally

  • :validate (Boolean)

    Validates provided modifiers (Default is true)

  • :context (Hash)

    Serialization context

  • :many (Boolean)

    Set true if provided multiple objects (Default ‘object.is_a?(Enumerable)`)

Returns:

  • (Hash)

    Serialization result



191
192
193
# File 'lib/serega.rb', line 191

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

#to_json(object, opts = nil) ⇒ String

Serializes provided object to JSON string

Parameters:

  • object (Object)

    Serialized object

  • opts (Hash, nil) (defaults to: nil)

    Serializer modifiers and other instantiating options

Options Hash (opts):

  • :only (Array, Hash, String, Symbol)

    The only attributes to serialize

  • :except (Array, Hash, String, Symbol)

    Attributes to hide

  • :with (Array, Hash, String, Symbol)

    Attributes (usually hidden) to serialize additionally

  • :validate (Boolean)

    Validates provided modifiers (Default is true)

  • :context (Hash)

    Serialization context

  • :many (Boolean)

    Set true if provided multiple objects (Default ‘object.is_a?(Enumerable)`)

Returns:

  • (String)

    Serialization result



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

def to_json(object, opts = nil)
  config.to_json.call(to_h(object, opts))
end