Class: Rbdantic::Serialization::Dumper
- Inherits:
-
Object
- Object
- Rbdantic::Serialization::Dumper
- Defined in:
- lib/rbdantic/serialization/dumper.rb
Overview
Handles model_dump with various options
Class Method Summary collapse
Instance Method Summary collapse
- #dump ⇒ Object
-
#initialize(model, mode: nil, include: nil, exclude: nil, exclude_unset: false, exclude_defaults: false, by_alias: false) ⇒ Dumper
constructor
A new instance of Dumper.
Constructor Details
#initialize(model, mode: nil, include: nil, exclude: nil, exclude_unset: false, exclude_defaults: false, by_alias: false) ⇒ Dumper
Returns a new instance of Dumper.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/rbdantic/serialization/dumper.rb', line 11 def initialize(model, mode: nil, include: nil, exclude: nil, exclude_unset: false, exclude_defaults: false, by_alias: false) @model = model @mode = mode @include = include @exclude = exclude @exclude_unset = exclude_unset @exclude_defaults = exclude_defaults @by_alias = by_alias @set_fields = Array(model.instance_variable_get("@__fields_set__")) end |
Class Method Details
.dump(model, **options) ⇒ Object
7 8 9 |
# File 'lib/rbdantic/serialization/dumper.rb', line 7 def self.dump(model, **) new(model, **).dump end |
Instance Method Details
#dump ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rbdantic/serialization/dumper.rb', line 23 def dump result = {} @model.class.fields.each do |name, field_info| next if should_exclude?(name, field_info) # Use alias if requested and present output_name = (@by_alias && field_info.alias_name) ? field_info.alias_name.to_sym : name.to_sym result[output_name] = serialize_item( @model.instance_variable_get("@#{name}"), **(name, field_info) ) end extra_fields.each do |name| next if should_exclude_extra?(name) result[name.to_sym] = serialize_item(@model.instance_variable_get("@#{name}")) end result end |