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.
310 311 312 |
# File 'lib/serega.rb', line 310 def plan @plan end |
Instance Method Details
#as_json(object, opts = nil) ⇒ Hash
Serializes provided object as JSON
360 361 362 363 |
# File 'lib/serega.rb', line 360 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
322 323 324 325 326 327 328 |
# File 'lib/serega.rb', line 322 def call(object, opts = nil) self.class::CheckSerializeParams.new(opts).validate if opts&.any? opts ||= {} opts[:context] ||= {} serialize(object, opts) end |
#initialize(opts = nil) ⇒ Object
Instantiates new Serega class
298 299 300 301 302 303 |
# File 'lib/serega.rb', line 298 def initialize(opts = nil) @opts = (opts.nil? || opts.empty?) ? FROZEN_EMPTY_HASH : parse_modifiers(opts) 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
331 332 333 |
# File 'lib/serega.rb', line 331 def to_h(object, opts = nil) call(object, opts) end |
#to_json(object, opts = nil) ⇒ Hash
Serializes provided object to JSON string
345 346 347 348 |
# File 'lib/serega.rb', line 345 def to_json(object, opts = nil) hash = to_h(object, opts) config.to_json.call(hash) end |