Class: NoCms::Blocks::DateSerializer

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

Overview

This class implements the read/write behaviour for dates.

It’s just like the SimpleFieldSerializer but it parses and builds dates

Instance Attribute Summary

Attributes inherited from BaseSerializer

#container, #field, #field_config

Instance Method Summary collapse

Methods inherited from BaseSerializer

#duplicate, #initialize, #read, #write

Constructor Details

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

Instance Method Details

#read_fieldObject

It parses the date from the value in the fields_info hash. It uses the class in the field configuration to parse the saved string



11
12
13
14
# File 'app/serializers/no_cms/blocks/date_serializer.rb', line 11

def read_field
  date_string = self.container.fields_info.symbolize_keys[self.field]
  date_string.blank? ? nil : self.field_config[:type].parse(date_string)
end

#write_field(value) ⇒ Object

It stores the value in the fields_info hash



18
19
20
21
22
23
24
25
26
27
# File 'app/serializers/no_cms/blocks/date_serializer.rb', line 18

def write_field value
  if value.blank?
    self.container.fields_info[self.field] = value
  elsif value.is_a?(String)
    value = self.field_config[:type].parse(value)
    self.container.fields_info[self.field] = value.strftime('%Y-%m-%dT%H:%M:%S.%N %Z')
  else
    self.container.fields_info[self.field] = value.strftime('%Y-%m-%dT%H:%M:%S.%N %Z')
  end
end