Module: Serega::InstanceMethods
- Included in:
- Serega
- Defined in:
- lib/serega.rb
Overview
Core serializer instance methods
Instance Attribute Summary collapse
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
-
#as_json(object, opts = {}) ⇒ Hash
Serializes provided object as json (uses only JSON-compatible types) When you later serialize/de-serialize it from JSON you should receive equal object.
-
#call(object, opts = {}) ⇒ Hash
Serializes provided object to hash.
-
#initialize(opts = FROZEN_EMPTY_HASH) ⇒ Object
Instantiates new Serega class.
- #to_h(object, opts = {}) ⇒ Object
-
#to_json(object, opts = {}) ⇒ Hash
Serializes provided object to json.
Instance Attribute Details
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
201 202 203 |
# File 'lib/serega.rb', line 201 def opts @opts end |
Instance Method Details
#as_json(object, opts = {}) ⇒ Hash
Serializes provided object as json (uses only JSON-compatible types) When you later serialize/de-serialize it from JSON you should receive equal object
256 257 258 259 |
# File 'lib/serega.rb', line 256 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
223 224 225 226 227 228 |
# File 'lib/serega.rb', line 223 def call(object, opts = {}) self.class::CheckSerializeParams.new(opts).validate opts[:context] ||= {} self.class::SeregaConvert.call(object, **opts, map: map) end |
#initialize(opts = FROZEN_EMPTY_HASH) ⇒ Object
Instantiates new Serega class
210 211 212 213 |
# File 'lib/serega.rb', line 210 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 |
#to_h(object, opts = {}) ⇒ Object
231 232 233 |
# File 'lib/serega.rb', line 231 def to_h(object, opts = {}) call(object, opts) end |
#to_json(object, opts = {}) ⇒ Hash
Serializes provided object to json
242 243 244 245 |
# File 'lib/serega.rb', line 242 def to_json(object, opts = {}) hash = to_h(object, opts) config.to_json.call(hash) end |