Class: Mutils::Serialization::BaseSerializer

Inherits:
Object
  • Object
show all
Includes:
SerializationIncludes, SerializationMethods, SerializationResults
Defined in:
lib/mutils/serialization/base_serializer.rb

Overview

BaseSerializer: inherit this class to get Serializer functionality

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Results::Relations

#hash_relationships

Methods included from Results::Attributes

#fetch_attributes, #fetch_block_attributes

Methods included from Results::Main

#check_if_included, #class_name, #format_class_name, #generate_hash, #hashed_result, #scope_is_collection?

Methods included from SerializationMethods

included

Methods included from SerializationIncludes

included

Constructor Details

#initialize(object, options = {}) ⇒ BaseSerializer

Returns a new instance of BaseSerializer.



15
16
17
18
19
# File 'lib/mutils/serialization/base_serializer.rb', line 15

def initialize(object, options = {})
  options[:child] = false unless options[:child]
  self.scope = object
  self.options = options
end

Instance Attribute Details

#scopeObject

Returns the value of attribute scope.



9
10
11
# File 'lib/mutils/serialization/base_serializer.rb', line 9

def scope
  @scope
end

Instance Method Details

#as_json(_options = {}) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/mutils/serialization/base_serializer.rb', line 21

def as_json(_options = {})
  if options[:child] || !self.class.include_root
    to_h
  else
    { class_name => to_h }
  end
end

#to_hObject



29
30
31
# File 'lib/mutils/serialization/base_serializer.rb', line 29

def to_h
  generate_hash
end

#to_json(_options = {}) ⇒ Object



33
34
35
# File 'lib/mutils/serialization/base_serializer.rb', line 33

def to_json(_options = {})
  JSON.generate(as_json, options)
end