Module: Serega::InstanceMethods

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

Overview

Serializers instance methods

Instance Method Summary collapse

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):

  • :context (Hash)

    Serialization context

  • :many (Boolean)

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

Returns:

  • (Hash)

    Serialization result



336
337
338
339
# File 'lib/serega.rb', line 336

def as_json(object, opts = nil)
  json = to_json(object, opts)
  config.from_json.call(json)
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):

  • :context (Hash)

    Serialization context

  • :many (Boolean)

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

Returns:

  • (Hash)

    Serialization result



298
299
300
301
302
303
304
# File 'lib/serega.rb', line 298

def call(object, opts = nil)
  self.class::CheckSerializeParams.new(opts).validate if opts&.any?
  opts ||= {}
  opts[:context] ||= {}

  serialize(object, opts)
end

#initialize(opts = nil) ⇒ Object

Instantiates new Serega class

Parameters:

  • 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)



283
284
285
286
# File 'lib/serega.rb', line 283

def initialize(opts = nil)
  @opts = (opts.nil? || opts.empty?) ? FROZEN_EMPTY_HASH : parse_modifiers(opts)
  self.class::CheckInitiateParams.new(@opts).validate if opts&.fetch(:check_initiate_params) { config.check_initiate_params }
end

#planArray<Serega::SeregaPlanPoint>

Plan for serialization. This plan can be traversed to find serialized attributes and nested attributes.

Returns:



346
347
348
# File 'lib/serega.rb', line 346

def plan
  @plan ||= self.class::SeregaPlan.call(opts)
end

#to_h(object, opts = nil) ⇒ Object

See Also:



307
308
309
# File 'lib/serega.rb', line 307

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

#to_json(object, opts = nil) ⇒ Hash

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):

  • :context (Hash)

    Serialization context

  • :many (Boolean)

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

Returns:

  • (Hash)

    Serialization result



321
322
323
324
# File 'lib/serega.rb', line 321

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