Class: Settis::ActiveRecordSerializer

Inherits:
Serializer
  • Object
show all
Defined in:
lib/settis/serializers/active_record.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Serializer

for, inherited, #initialize

Constructor Details

This class inherits a constructor from Settis::Serializer

Class Method Details

.serializes?(type) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/settis/serializers/active_record.rb', line 19

def self.serializes?(type)
  type.is_a?(Class) and type.ancestors.include?(ActiveRecord::Base)
end

Instance Method Details

#deserialize(value) ⇒ Object



33
34
35
# File 'lib/settis/serializers/active_record.rb', line 33

def deserialize(value)
  type.find_by_id(value) unless value.nil?
end

#serialize(value) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/settis/serializers/active_record.rb', line 23

def serialize(value)
  case value
  when Integer then value.to_s
  when type then value.id.to_s
  when nil then nil
  else
    raise ArgumentError, "Setting value must be an instance of #{type}, an ID number, or nil"
  end
end