Module: Gecko::Helpers::SerializationHelper
- Defined in:
- lib/gecko/helpers/serialization_helper.rb
Overview
Provides serialization to records
Instance Method Summary collapse
-
#_serialize(serialized) ⇒ String
private
Serialize an attribute.
-
#as_json ⇒ Hash
private
Returns a full JSON representation of a record.
-
#root ⇒ String
private
Return JSON root key for a record.
-
#serializable_hash ⇒ Hash
private
Return a serialized hash of the record’s attributes.
-
#serialize_attribute(attribute_hash, attribute) ⇒ undefined
private
Store the serialized representation of a single attribute.
-
#writeable?(attribute) ⇒ Boolean
private
Returns true if an attribute can be serialized.
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
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/gecko/helpers/serialization_helper.rb', line 82 def _serialize(serialized) # rubocop:disable Metrics/MethodLength 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_json ⇒ Hash
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
15 16 17 18 19 |
# File 'lib/gecko/helpers/serialization_helper.rb', line 15 def as_json { root => serializable_hash } end |
#root ⇒ 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.
Return JSON root key for a record
105 106 107 |
# File 'lib/gecko/helpers/serialization_helper.rb', line 105 def root self.class.demodulized_name.underscore.to_sym end |
#serializable_hash ⇒ Hash
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
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/gecko/helpers/serialization_helper.rb', line 29 def serializable_hash attribute_hash = {} attribute_set.each do |attribute| next unless writeable?(attribute) serialize_attribute(attribute_hash, attribute) end .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
71 72 73 |
# File 'lib/gecko/helpers/serialization_helper.rb', line 71 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
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/gecko/helpers/serialization_helper.rb', line 49 def writeable?(attribute) return if attribute.[:readonly] return true unless attribute.[:writeable_on] case attribute.[:writeable_on] when :update persisted? when :create !persisted? else raise ArgumentError end end |