Class: Surrealist::Serializer

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Defined in:
lib/surrealist/serializer.rb

Overview

Abstract class to be inherited from

Examples:

Usage

class CatSerializer < Surrealist::Serializer
  json_schema { { age: Integer, age_group: String } }

  def age_group
    age <= 5 ? 'kitten' : 'cat'
  end
end

class Cat
  include Surrealist
  attr_reader :age

  surrealize_with CatSerializer

  def initialize(age)
    @age = age
  end
end

Cat.new(12).surrealize # Checks for schema in CatSerializer (if .surrealize_with is stated)
# => '{ "age": 12, "age_group": "cat" }'

CatSerializer.new(Cat.new(3)).surrealize # explicit usage of CatSerializer
# => '{ "age": 3, "age_group": "kitten" }'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

defined_schema, delegate_surrealization_to, json_schema, surrealize_with

Constructor Details

#initialize(object, **context) ⇒ Serializer

NOTE: #context will work only when using serializer explicitly,

e.g `CatSerializer.new(Cat.new(3), food: CatFood.new)`
And then food will be available inside serializer via `context[:food]`


76
77
78
79
# File 'lib/surrealist/serializer.rb', line 76

def initialize(object, **context)
  @object = wrap_hash_into_struct(object)
  @context = context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)

Methods not found inside serializer will be invoked on the object



109
110
111
112
113
# File 'lib/surrealist/serializer.rb', line 109

def method_missing(method, *args, &block)
  return super unless object.respond_to?(method)

  object.public_send(method, *args, &block)
end

Class Method Details

.method_defined?(method) ⇒ Boolean

Only lookup for methods defined in Surrealist::Serializer subclasses

to prevent invoke of Kernel methods

Parameters:

  • method (Symbol)

    method to be invoked

Returns:

  • (Boolean)


58
59
60
61
62
63
# File 'lib/surrealist/serializer.rb', line 58

def method_defined?(method)
  return true if instance_methods(false).include?(method)
  return false if superclass == Surrealist::Serializer

  super
end

.private_method_defined?(method) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
# File 'lib/surrealist/serializer.rb', line 65

def private_method_defined?(method)
  return true if private_instance_methods(false).include?(method)
  return false if superclass == Surrealist::Serializer

  super
end

.serializer_context(*array) ⇒ Object Also known as: serializer_contexts

Defines instance methods that read values from the context hash.

Parameters:

  • array (Array<Symbol>)

    an array of symbols which represent method names

Raises:

  • ArgumentError if type of argument is not an array of symbols



41
42
43
44
45
46
47
# File 'lib/surrealist/serializer.rb', line 41

def serializer_context(*array)
  unless array.all? { |i| i.is_a? Symbol }
    raise ArgumentError, 'Please provide an array of symbols to `.serializer_context`'
  end

  array.each { |method| define_method(method) { context[method] } }
end

Instance Method Details

#build_schema(**args) ⇒ Object

Passes build_schema to Surrealist



91
92
93
94
95
96
97
# File 'lib/surrealist/serializer.rb', line 91

def build_schema(**args)
  if Helper.collection?(object)
    build_collection_schema(**args)
  else
    Surrealist.build_schema(instance: self, **args)
  end
end

#surrealize(**args) ⇒ Object

Checks whether object is a collection or an instance and serializes it



82
83
84
85
86
87
88
# File 'lib/surrealist/serializer.rb', line 82

def surrealize(**args)
  if Helper.collection?(object)
    Surrealist.surrealize_collection(object, **args.merge(context: context))
  else
    Surrealist.surrealize(instance: self, **args)
  end
end