Class: NoCms::Blocks::BaseMultipleSerializer

Inherits:
BaseSerializer show all
Defined in:
app/serializers/no_cms/blocks/base_multiple_serializer.rb

Overview

This class implements the read/write behaviour for fields affected by the “multiple” setting. As an example, this may include ActiveRecord and ActiveResource serializers.

It relies on its subclasses implementing a multiple and single versions for the read and write methods.

Instance Attribute Summary

Attributes inherited from BaseSerializer

#container, #field, #field_config

Instance Method Summary collapse

Methods inherited from BaseSerializer

#initialize, #read, #write

Constructor Details

This class inherits a constructor from NoCms::Blocks::BaseSerializer

Instance Method Details

#duplicateObject

In fields configured as multiple and with the :dup behaviour, we have to get the whole array or relation and dup each element one by one. Otherwise we just dup the array.

In any other case (the field is not multiple or has any other duplication behaviour) we are fine with the default behaviour from BaseSerializer.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/serializers/no_cms/blocks/base_multiple_serializer.rb', line 74

def duplicate
  if field_config[:multiple] && field_config[:duplicate] == :dup
    field_value = read
    dupped_value = case field_value
    when nil
      nil
    when Array, ActiveRecord::Relation
      field_value.map(&:dup)
    else
      field_value.dup
    end
    write dupped_value
  else
    super
  end
end

#read_fieldObject

This method is the “read” implementation for the serializer.

Depending on the field being configured as multiple or not it delegates the reading to the ‘read_multiple_field` or `read_single_field`.



18
19
20
21
22
23
24
# File 'app/serializers/no_cms/blocks/base_multiple_serializer.rb', line 18

def read_field
  if field_config[:multiple]
    read_multiple_field
  else
    read_single_field
  end
end

#read_multiple_fieldObject

Method to be implemented by the subclasses with the behaviour for reading a field configured as multiple.

Raises:

  • (NotImplementedError)


36
37
38
# File 'app/serializers/no_cms/blocks/base_multiple_serializer.rb', line 36

def read_multiple_field
  raise NotImplementedError.new("The serializer #{self.inspect} has no 'read_multiple_field' implementation")
end

#read_single_fieldObject

Method to be implemented by the subclasses with the behaviour for reading a field not configured as multiple.

Raises:

  • (NotImplementedError)


29
30
31
# File 'app/serializers/no_cms/blocks/base_multiple_serializer.rb', line 29

def read_single_field
  raise NotImplementedError.new("The serializer #{self.inspect} has no 'read_single_field' implementation")
end

#write_field(value) ⇒ Object

This method is the “write” implementation for the serializer.

Depending on the field being configured as multiple or not it delegates the writing to the ‘write_multiple_field` or `write_single_field`.



45
46
47
48
49
50
51
# File 'app/serializers/no_cms/blocks/base_multiple_serializer.rb', line 45

def write_field value
  if field_config[:multiple]
    write_multiple_field value
  else
    write_single_field value
  end
end

#write_multiple_field(values) ⇒ Object

Method to be implemented by the subclasses with the behaviour for writing a field configured as multiple.

Raises:

  • (NotImplementedError)


63
64
65
# File 'app/serializers/no_cms/blocks/base_multiple_serializer.rb', line 63

def write_multiple_field values
  raise NotImplementedError.new("The serializer #{self.inspect} has no 'write_multiple_field' implementation")
end

#write_single_field(value) ⇒ Object

Method to be implemented by the subclasses with the behaviour for writing a field not configured as multiple.

Raises:

  • (NotImplementedError)


56
57
58
# File 'app/serializers/no_cms/blocks/base_multiple_serializer.rb', line 56

def write_single_field value
  raise NotImplementedError.new("The serializer #{self.inspect} has no 'write_single_field' implementation")
end