Module: SerialBox::ClassMethods

Defined in:
lib/serialbox.rb

Overview

Methods added to the class that this module is mixed into.

Instance Method Summary collapse

Instance Method Details

#_serialbox_serializerObject



54
# File 'lib/serialbox.rb', line 54

def _serialbox_serializer() @_serialbox_serializer end

#serialize_fields {|serializer| ... } ⇒ Object

Call this method to define how your object is serialized. This method yields an object on which you call methods to define your serialization.

Yields:

  • (serializer)

    A block where you can define how your object is serialized.

Yield Parameters:



25
26
27
28
# File 'lib/serialbox.rb', line 25

def serialize_fields
  @_serialbox_serializer = Serializer.new(self)
  yield @_serialbox_serializer
end

#serialize_with(serializer, ...) ⇒ Object

Call this method to specify which serialization formats to use.

Parameters:

  • serializer (Symbol, Class)

    A serialization format adapter or the name of such adapter under the ‘SerialBox::Serializers` namespace. See the classes under Serializers for a list of possible values.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/serialbox.rb', line 38

def serialize_with(*serializers)
  serializers.map! do |s|
    case s
      when Class
        s
      when Symbol, String
        SerialBox::Serializers.const_get(s.to_sym)
      else
        raise ArgumentError, "Unknown serializer #{s.inspect}"
    end
  end

  serializers.each { |serializer| include serializer }
end