Class: ApiResource::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/api_resource/serializer.rb

Overview

Handles serialization for a given instance of ApiResource::Base with a set of options

Author:

  • [dlangevin]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, options = {}) ⇒ Serializer

Constructor

explicitly exclude Associations to explicitly include Attributes to explicitly include the primary key to include attributes that are blank/nil in the serialized hash

Parameters:

  • record (ApiResource::Base)

    Record to serialize

  • options (defaults to: {})

    = {} [Hash] Options supplied

Options Hash (options):

  • except (Array<Symbol,String>) — default: Array<>

    Attributes to

  • Array (Array<Symbol,String] include_associations (Array<>))

    include_associations (Array<>)

  • Array (Array<Symbol,String] include_extras (Array<>))

    include_extras (Array<>)

  • include_id (Boolean) — default: false

    Whether or not to include

  • include_nil_attributes (Boolean) — default: false

    Whether or not



36
37
38
39
# File 'lib/api_resource/serializer.rb', line 36

def initialize(record, options = {})
  @record = record
  @options = options.with_indifferent_access
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/api_resource/serializer.rb', line 13

def options
  @options
end

#recordObject (readonly)

Returns the value of attribute record.



17
18
19
# File 'lib/api_resource/serializer.rb', line 17

def record
  @record
end

Instance Method Details

#to_hashHashWithIndifferentAccess

Return our serialized object as a Hash

Returns:

  • (HashWithIndifferentAccess)


45
46
47
48
49
50
51
52
# File 'lib/api_resource/serializer.rb', line 45

def to_hash
  ret = HashWithIndifferentAccess.new
  ret.merge!(self.attributes_for_hash)
  ret.merge!(self.associations_for_hash)
  ret.merge!(self.add_on_data_for_hash)
  ret.merge!(self.association_foreign_keys_for_hash)
  ret
end