Class: SerialBox::Serializer

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

Overview

An object that is used to define how a class is serialized. The methods of this object can be called within a block given to ClassMethods#serialize_fields.

Defined Under Namespace

Classes: BlockDeserialization, BlockSerialization

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Serializer

Returns a new instance of Serializer.



67
68
69
70
71
# File 'lib/serialbox.rb', line 67

def initialize(object)
  @serialization_operations   = Array.new
  @deserialization_operations = Array.new
  @object                     = object
end

Instance Attribute Details

#deserialization_operationsObject (readonly)



64
65
66
# File 'lib/serialbox.rb', line 64

def deserialization_operations
  @deserialization_operations
end

#objectObject (readonly)



64
65
66
# File 'lib/serialbox.rb', line 64

def object
  @object
end

#serialization_operationsObject (readonly)



64
65
66
# File 'lib/serialbox.rb', line 64

def serialization_operations
  @serialization_operations
end

Instance Method Details

#deserializer(json_field, object_method) ⇒ Object #deserializer(json_field) {|value| ... } ⇒ Object

Defines how a field is handled when an object is deserialized from its primitive representation.

Overloads:

  • #deserializer(json_field, object_method) ⇒ Object

    Specifies that the value in ‘json_field` should be passed to `object_method` during deserialization.

    Parameters:

    • json_field (String)

      The field to retrieve the value from.

    • object_method (Symbol)

      The method to call to store the value into the object.

  • #deserializer(json_field) {|value| ... } ⇒ Object

    Specifies that the value stored in ‘json_field` should be passed to the block provided, which will store that value into the object being deserialized.

    Parameters:

    • json_field (String)

      The field to retrieve the value from in the JSON representation.

    Yields:

    • (value)

      A block that stores the value into the object. This block is executed in the context of the object.

    Yield Parameters:

    • value

      The value to store.



156
157
158
159
160
161
162
163
164
# File 'lib/serialbox.rb', line 156

def deserializer(json_field, object_method=nil, &block)
  if block_given?
    @deserialization_operations << BlockDeserialization.new(json_field, block, object)
  elsif object_method
    @deserialization_operations << BlockDeserialization.new(json_field, lambda { |value| send object_method, value }, object)
  else
    raise ArgumentError, "Must provide the name of a method or a block that sets a field from a deserialized value"
  end
end

#serialize(field, ..., options = {}) ⇒ Object

Specifies that one or more fields should be serialized in the most obvious manner. This is suitable when serializing a simple getter/setter pair or an instance variable.

Parameters:

  • field (Symbol)

    The name of a method (or instance variable with “@” sigil) that will be serialized.

  • options (Hash) (defaults to: {})

    Additional options controlling how the field is serialized.

Options Hash (options):

  • :into (String)

    If given, specifies that the field should be serialized into a key with a different name. Can only be provided if a single field is given.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/serialbox.rb', line 87

def serialize(*fields)
  options = fields.extract_options!
  if options[:into] && fields.size > 1
    raise ArgumentError, "Can't specify :into option with multiple fields"
  end

  fields.each do |field|
    if field.to_s[0, 1] == '@'
      field      = field.to_s[1..-1]
      json_field = options[:into] || field
      serializer(json_field) { instance_variable_get :"@#{field}" }
      deserializer(json_field) { |value| instance_variable_set :"@#{field}", value }
    else
      json_field = options[:into] || field
      serializer json_field, field
      deserializer json_field, :"#{field}="
    end
  end
end

#serializer(json_field, object_method) ⇒ Object #serializer(json_field) { ... } ⇒ Object

Defines how a field is handled when an object is prepared for serialization.

Overloads:

  • #serializer(json_field, object_method) ⇒ Object

    Specifies that the method ‘object_method` should be called and its result should be stored in a field named `json_field` as part of serialization.

    Parameters:

    • json_field (String)

      The field to store the resulting value in.

    • object_method (Symbol)

      The method to call to retrieve a value.

  • #serializer(json_field) { ... } ⇒ Object

    Specifies that a block should be called in the context of the instance being serialized, and the result should be stored in a field named ‘json_field`.

    Parameters:

    • json_field (String)

      The field to store the resulting value in in the JSON representation.

    Yields:

    • A block that returns the value to serialize.

    Yield Returns:

    • The value to serialize into ‘json_field`.



126
127
128
129
130
131
132
133
134
# File 'lib/serialbox.rb', line 126

def serializer(json_field, object_method=nil, &block)
  if block_given?
    @serialization_operations << BlockSerialization.new(json_field, block)
  elsif object_method
    @serialization_operations << BlockSerialization.new(json_field, lambda { send object_method })
  else
    raise ArgumentError, "Must provide the name of a method or a block that returns a value to serialize"
  end
end