Module: Serega::InstanceMethods
- Included in:
- Serega
- Defined in:
- lib/serega.rb
Overview
Serializers instance methods
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.
-
#map ⇒ Array<Serega::SeregaMapPoint>
Array of MapPoints, which are attributes combined with nested attributes.
- #to_h(object, opts = nil) ⇒ Object
-
#to_json(object, opts = nil) ⇒ Hash
Serializes provided object to JSON string.
Instance Method Details
#as_json(object, opts = nil) ⇒ Hash
Serializes provided object as JSON
329 330 331 332 |
# File 'lib/serega.rb', line 329 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
291 292 293 294 295 296 297 |
# File 'lib/serega.rb', line 291 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
276 277 278 279 |
# File 'lib/serega.rb', line 276 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 } end |
#map ⇒ Array<Serega::SeregaMapPoint>
Array of MapPoints, which are attributes combined with nested attributes. This map can be traversed to find currently serializing attributes.
339 340 341 |
# File 'lib/serega.rb', line 339 def map @map ||= self.class::SeregaMap.call(opts) end |
#to_h(object, opts = nil) ⇒ Object
300 301 302 |
# File 'lib/serega.rb', line 300 def to_h(object, opts = nil) call(object, opts) end |
#to_json(object, opts = nil) ⇒ Hash
Serializes provided object to JSON string
314 315 316 317 |
# File 'lib/serega.rb', line 314 def to_json(object, opts = nil) hash = to_h(object, opts) config.to_json.call(hash) end |