Module: Serega::InstanceMethods

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

Overview

Core serializer instance methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



206
207
208
# File 'lib/serega.rb', line 206

def opts
  @opts
end

Instance Method Details

#as_json(object, opts = {}) ⇒ Hash

Serializes provided object as json (uses only JSON-compatible types) When you later serialize/de-serialize it from JSON you should receive equal object

Parameters:

  • object (Object)

    Serialized object

Returns:

  • (Hash)

    Serialization result



261
262
263
264
# File 'lib/serega.rb', line 261

def as_json(object, opts = {})
  json = to_json(object, opts)
  config.from_json.call(json)
end

#call(object, opts = {}) ⇒ Hash

Serializes provided object to hash

Parameters:

  • object (Object)

    Serialized object

  • opts (Hash) (defaults to: {})

    Serialization options, like :context and :many

Returns:

  • (Hash)

    Serialization result



228
229
230
231
232
233
# File 'lib/serega.rb', line 228

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

  self.class::SeregaSerializer.new(points: map, **opts).serialize(object)
end

#initialize(opts = FROZEN_EMPTY_HASH) ⇒ Object

Instantiates new Serega class

Parameters:

  • 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



215
216
217
218
# File 'lib/serega.rb', line 215

def initialize(opts = FROZEN_EMPTY_HASH)
  @opts = (opts == FROZEN_EMPTY_HASH) ? opts : prepare_modifiers(opts)
  self.class::CheckInitiateParams.new(@opts).validate if opts.fetch(:check_initiate_params) { config.check_initiate_params }
end

#to_h(object, opts = {}) ⇒ Object

See Also:



236
237
238
# File 'lib/serega.rb', line 236

def to_h(object, opts = {})
  call(object, opts)
end

#to_json(object, opts = {}) ⇒ Hash

Serializes provided object to json

Parameters:

  • object (Object)

    Serialized object

Returns:

  • (Hash)

    Serialization result



247
248
249
250
# File 'lib/serega.rb', line 247

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