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 = {}) ⇒ Hash

Serializes provided object as JSON

Parameters:

  • object (Object)

    Serialized object

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

    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



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

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

    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



270
271
272
273
274
275
# File 'lib/serega.rb', line 270

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

  serialize(object, opts)
end

#initialize(opts = FROZEN_EMPTY_HASH) ⇒ Object

Instantiates new Serega class

Parameters:

  • opts (Hash) (defaults to: FROZEN_EMPTY_HASH)

    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)



255
256
257
258
# File 'lib/serega.rb', line 255

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

#mapArray<Serega::SeregaMapPoint>

Array of MapPoints, which are attributes combined with nested attributes. This map can be traversed to find currently serializing attributes.

Returns:



317
318
319
# File 'lib/serega.rb', line 317

def map
  @map ||= self.class::SeregaMap.call(opts)
end

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

See Also:



278
279
280
# File 'lib/serega.rb', line 278

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

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

Serializes provided object to JSON string

Parameters:

  • object (Object)

    Serialized object

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

    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



292
293
294
295
# File 'lib/serega.rb', line 292

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