Sha256: 84925800d614559390ab7a8a170f5f836f9b09aeaf4cff37edb2a17f71e80e69
Contents?: true
Size: 1.1 KB
Versions: 1
Compression:
Stored size: 1.1 KB
Contents
# frozen_string_literal: true module Martinet module Rails class Serializer def self.serialize(record:) return record unless serializable?(record: record) { klass: record.class.name, record_id: record.id } end def self.deserialize(object_hash:) record_for(object_hash: object_hash) || object_hash end def self.record_for(object_hash:) return unless deserializable?(object_hash: object_hash) record_klass = object_hash[:klass].safe_constantize record_klass.find_by(id: object_hash[:record_id]) end # FIXME: Refactor this method def self.serializable?(record:) record.respond_to?(:id) && record.id.presence && record.class.respond_to?(:find_by) end # FIXME: Refactor this method, maybe a rescue block? def self.deserializable?(object_hash:) valid = [:klass, :record_id].all? do |k| object_hash.respond_to?(:key) && object_hash.key?(k) && object_hash[k].presence end valid && object_hash[:klass].safe_constantize.respond_to?(:find_by) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
martinet-rails-0.2.0 | lib/martinet/rails/serializer.rb |