Module: Codecs::DataClassCodec

Defined in:
lib/emery/codecs.rb

Class Method Summary collapse

Class Method Details

.applicable?(type) ⇒ Boolean

Returns:



191
192
193
# File 'lib/emery/codecs.rb', line 191

def self.applicable?(type)
  type.respond_to? :ancestors and type.ancestors.include? DataClass
end

.deserialize(type, json_value) ⇒ Object



194
195
196
197
198
199
200
201
# File 'lib/emery/codecs.rb', line 194

def self.deserialize(type, json_value)
  T.check(T.hash(String, NilableUnknown), json_value)
  parameters = type.typed_attributes.map do |attr, attr_type|
    attr_value = json_value[attr.to_s]
    [attr, Jsoner.deserialize(attr_type, attr_value)]
  end
  return type.new(parameters.to_h)
end

.serialize(type, value) ⇒ Object



203
204
205
206
207
208
209
# File 'lib/emery/codecs.rb', line 203

def self.serialize(type, value)
  T.check(type, value)
  attrs = type.typed_attributes.map do |attr, attr_type|
    [attr, Jsoner.serialize(attr_type, value.send(attr))]
  end
  return attrs.to_h
end