module Netfira::WebConnect::Model::Record::Serializer

Constants

EXCLUDE_FROM_SERIALIZE

Public Class Methods

attributes_to_serialize() click to toggle source

Returns an array of three arrays of keys, e.g.

['product_id', 'unit_price'], ['description'], [

]

# File lib/netfira/web_connect/model/record/serializer.rb, line 17
def self.attributes_to_serialize
  @attributes_to_serialize ||= [
      attribute_names - EXCLUDE_FROM_SERIALIZE,
      has_languages? ? self::Translation.translated_attribute_names : [],
      [] # Custom attributes (not implemented)
  ]
end

Public Instance Methods

to_s() click to toggle source
# File lib/netfira/web_connect/model/record/serializer.rb, line 26
def to_s

  # Map each array of keys from the above method into a sorted hash of key/value pairs
  data = self.class.attributes_to_serialize.map do |keys|
    pairs = keys.sort.map do |key|
      [key.camelize(:lower), cast_for_serialization(__send__ key)]
    end
    pairs.reject!{ |v| v[1].nil? }.to_h
  end

  # Return the class name and the sets above, e.g. Product{...},{...},{...}
  self.class.name.demodulize << JSON.generate(data)[1..-2]
end

Private Instance Methods

cast_for_serialization(value) click to toggle source
# File lib/netfira/web_connect/model/record/serializer.rb, line 42
def cast_for_serialization(value)
  if TranslatedString === value
    value = value.sort
    value = value.empty? ? nil : value.to_h
  elsif Numeric === value
    value = (value == value.floor) ? value.to_i : value.to_f
  elsif OctaWord === value
    value = value.to_s
  end
  value
end