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.



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

def opts
  @opts
end

Instance Method Details

#as_json(object, opts = FROZEN_EMPTY_HASH) ⇒ Hash

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

Parameters:

  • object (Object)

    Serialized object

Returns:

  • (Hash)

    Serialization result



266
267
268
269
# File 'lib/serega.rb', line 266

def as_json(object, opts = FROZEN_EMPTY_HASH)
  hash = to_h(object, opts)
  SeregaUtils::AsJSON.call(hash, to_json: self.class.config[:to_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



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

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

  self.class::SeregaConvert.call(object, **opts, map: map)
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



219
220
221
222
223
# File 'lib/serega.rb', line 219

def initialize(opts = FROZEN_EMPTY_HASH)
  self.class::CheckInitiateParams.call(opts)
  opts = prepare_modifiers(opts) if opts && (opts != FROZEN_EMPTY_HASH)
  @opts = opts
end

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

See Also:



241
242
243
# File 'lib/serega.rb', line 241

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

#to_json(object, opts = FROZEN_EMPTY_HASH) ⇒ Hash

Serializes provided object to json

Parameters:

  • object (Object)

    Serialized object

Returns:

  • (Hash)

    Serialization result



252
253
254
255
# File 'lib/serega.rb', line 252

def to_json(object, opts = FROZEN_EMPTY_HASH)
  hash = to_h(object, opts)
  self.class.config[:to_json].call(hash)
end