class Martinet::Rails::Serializer

Public Class Methods

deserializable?(object_hash:) click to toggle source

FIXME: Refactor this method, maybe a rescue block?

# File lib/martinet/rails/serializer.rb, line 28
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
deserialize(object_hash:) click to toggle source
# File lib/martinet/rails/serializer.rb, line 11
def self.deserialize(object_hash:)
  record_for(object_hash: object_hash) || object_hash
end
record_for(object_hash:) click to toggle source
# File lib/martinet/rails/serializer.rb, line 15
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
serializable?(record:) click to toggle source

FIXME: Refactor this method

# File lib/martinet/rails/serializer.rb, line 23
def self.serializable?(record:)
  record.respond_to?(:id) && record.id.presence && record.class.respond_to?(:find_by)
end
serialize(record:) click to toggle source
# File lib/martinet/rails/serializer.rb, line 6
def self.serialize(record:)
  return record unless serializable?(record: record)
  { klass: record.class.name, record_id: record.id }
end