Class: Martinet::Rails::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/martinet/rails/serializer.rb

Class Method Summary collapse

Class Method Details

.deserializable?(object_hash:) ⇒ Boolean

FIXME: Refactor this method, maybe a rescue block?

Returns:

  • (Boolean)


28
29
30
31
32
33
# 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:) ⇒ Object



11
12
13
# 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:) ⇒ Object



15
16
17
18
19
20
# 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:) ⇒ Boolean

FIXME: Refactor this method

Returns:

  • (Boolean)


23
24
25
# 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:) ⇒ Object



6
7
8
9
# 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