Module: Serega::InstanceMethods
- Included in:
- Serega
- Defined in:
- lib/serega.rb
Overview
Serializers instance methods
Instance Attribute Summary collapse
-
#plan ⇒ Serega::SeregaPlan
readonly
Plan for serialization.
Instance Method Summary collapse
-
#as_json(object, opts = nil) ⇒ Hash
Serializes provided object as JSON.
-
#call(object, opts = nil) ⇒ Hash
Serializes provided object to Hash.
-
#initialize(opts = nil) ⇒ Object
Instantiates new Serega class.
-
#preloads ⇒ Hash
Merged preloads of all serialized attributes.
- #to_h(object, opts = nil) ⇒ Object
-
#to_json(object, opts = nil) ⇒ Hash
Serializes provided object to JSON string.
Instance Attribute Details
#plan ⇒ Serega::SeregaPlan (readonly)
Plan for serialization. This plan can be traversed to find serialized attributes and nested attributes.
404 405 406 |
# File 'lib/serega.rb', line 404 def plan @plan end |
Instance Method Details
#as_json(object, opts = nil) ⇒ Hash
Serializes provided object as JSON
459 460 461 462 |
# File 'lib/serega.rb', line 459 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
416 417 418 419 420 421 422 |
# File 'lib/serega.rb', line 416 def call(object, opts = nil) opts = opts ? opts.transform_keys!(&:to_sym) : {} self.class::CheckSerializeParams.new(opts).validate unless opts.empty? opts[:context] ||= {} serialize(object, opts) end |
#initialize(opts = nil) ⇒ Object
Instantiates new Serega class
385 386 387 388 389 390 391 392 393 394 395 396 397 |
# File 'lib/serega.rb', line 385 def initialize(opts = nil) @opts = if opts.nil? || opts.empty? FROZEN_EMPTY_HASH else opts.transform_keys!(&:to_sym) parse_modifiers(opts) end self.class::CheckInitiateParams.new(@opts).validate if opts&.fetch(:check_initiate_params) { config.check_initiate_params } @plan = self.class::SeregaPlan.call(@opts) end |
#preloads ⇒ Hash
Returns merged preloads of all serialized attributes.
430 431 432 |
# File 'lib/serega.rb', line 430 def preloads @preloads ||= SeregaUtils::PreloadsConstructor.call(plan) end |
#to_h(object, opts = nil) ⇒ Object
425 426 427 |
# File 'lib/serega.rb', line 425 def to_h(object, opts = nil) call(object, opts) end |
#to_json(object, opts = nil) ⇒ Hash
Serializes provided object to JSON string
444 445 446 447 |
# File 'lib/serega.rb', line 444 def to_json(object, opts = nil) hash = to_h(object, opts) config.to_json.call(hash) end |