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.
- #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.
318 319 320 |
# File 'lib/serega.rb', line 318 def plan @plan end |
Instance Method Details
#as_json(object, opts = nil) ⇒ Hash
Serializes provided object as JSON
368 369 370 371 |
# File 'lib/serega.rb', line 368 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
330 331 332 333 334 335 336 |
# File 'lib/serega.rb', line 330 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
299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/serega.rb', line 299 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 |
#to_h(object, opts = nil) ⇒ Object
339 340 341 |
# File 'lib/serega.rb', line 339 def to_h(object, opts = nil) call(object, opts) end |
#to_json(object, opts = nil) ⇒ Hash
Serializes provided object to JSON string
353 354 355 356 |
# File 'lib/serega.rb', line 353 def to_json(object, opts = nil) hash = to_h(object, opts) config.to_json.call(hash) end |