Module: Gecko::Helpers::SerializationHelper

Included in:
Record::Base, Record::Variant::VariantLocation, Record::Variant::VariantPrice
Defined in:
lib/gecko/helpers/serialization_helper.rb

Overview

Provides serialization to records

Instance Method Summary collapse

Instance Method Details

#_serialize(serialized) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Serialize an attribute

Parameters:

  • serialized (Object)

    The attribute to serialize

Returns:

  • (String)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/gecko/helpers/serialization_helper.rb', line 78

def _serialize(serialized)
  if serialized.respond_to?(:serializable_hash)
    serialized.serializable_hash
  else
    case serialized
    when Array
      serialized.map { |attr| _serialize(attr) }
    when BigDecimal
      serialized.to_s("F")
    else
      serialized
    end
  end
end

#as_jsonHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a full JSON representation of a record

Examples:

product.as_json #=> {product: {id: 12, name: "Big"}}

Returns:

  • (Hash)


13
14
15
16
17
# File 'lib/gecko/helpers/serialization_helper.rb', line 13

def as_json
  {
    root => serializable_hash
  }
end

#rootString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return JSON root key for a record

Examples:

product.root #=> "product"

Returns:

  • (String)


101
102
103
# File 'lib/gecko/helpers/serialization_helper.rb', line 101

def root
  self.class.demodulized_name.underscore.to_sym
end

#serializable_hashHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return a serialized hash of the record’s attributes

Examples:

product.serializable_hash #=> {id: 12, name: "Big"}

Returns:

  • (Hash)


27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gecko/helpers/serialization_helper.rb', line 27

def serializable_hash
  attribute_hash = {}
  attribute_set.each do |attribute|
    next unless writeable?(attribute)
    serialize_attribute(attribute_hash, attribute)
  end

  embedded_collections_for_serialization.each do |collection|
    serialize_new_records(attribute_hash, collection)
  end

  attribute_hash
end

#serialize_attribute(attribute_hash, attribute) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Store the serialized representation of a single attribute

Parameters:

  • attribute_hash (Hash)

    Serialized record being iterated over

  • attribute (Virtus::Attribute)

    The attribute being serialized

Returns:

  • (undefined)


67
68
69
# File 'lib/gecko/helpers/serialization_helper.rb', line 67

def serialize_attribute(attribute_hash, attribute)
  attribute_hash[attribute.name] = _serialize(attributes[attribute.name])
end

#writeable?(attribute) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns true if an attribute can be serialized

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gecko/helpers/serialization_helper.rb', line 46

def writeable?(attribute)
  return if attribute.options[:readonly]
  return true unless attribute.options[:writeable_on]
  case attribute.options[:writeable_on]
  when :update
    persisted?
  when :create
    !persisted?
  else
    raise ArgumentError
  end
end