Class: ActiveModel::Serializers::Binary::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model_serializers_binary/active_model_serializers_binary.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(serializable, options = nil) ⇒ Serializer

Returns a new instance of Serializer.



193
194
195
196
197
198
199
200
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 193

def initialize(serializable, options = nil)
  @serializable = serializable
  @options = options ? options.dup : {}
  @current_address = 0
  @start_address = 0
  @current_byte = 0
  @current_bit = 0
end

Instance Attribute Details

#start_addressObject

Returns the value of attribute start_address.



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

def start_address
  @start_address
end

Instance Method Details

#align_data(attr_options, var) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 230

def align_data( attr_options, var )
  # XXX: hay que sacar nest de acá
  if !attr_options[:type].in? [:bitfield, :bool, :nest]
    # Se posiciona al principio de un byte
    if self.current_bit != 0
      self.current_address = self.current_address.ceil
    end
    if @options[:align]==:dword
      # Si el dato es una palabra simple, alinea los datos en el siguiente byte par
      if var.bit_length > 8 and (self.current_address + self.start_address).modulo(2) != 0
        self.current_byte += 1
      end
      # Si el dato es una palabra doble, alinea los datos en la siguiente palabra par
      if var.bit_length > 16 and (self.current_address + self.start_address).modulo(4) != 0
        self.current_byte += 4-self.current_byte%4
      end
    elsif @options[:align]==:word
      # Si el dato es una palabra simple, alinea los datos en el siguiente byte par
      if var.bit_length > 8 and (self.current_address + self.start_address).modulo(2) != 0
        self.current_byte += 1
      end
    end
  end
end

#current_addressObject



208
209
210
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 208

def current_address
  @current_address
end

#current_address=(value) ⇒ Object



202
203
204
205
206
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 202

def current_address= (value)
  @current_address = value
  @current_byte = value.floor
  @current_bit = (value.modulo(1)*8).round
end

#current_bitObject



226
227
228
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 226

def current_bit
  @current_bit
end

#current_bit=(value) ⇒ Object



221
222
223
224
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 221

def current_bit= (value)
  @current_bit = value
  @current_address = (@current_byte+@current_bit/8.0)
end

#current_byteObject



217
218
219
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 217

def current_byte
  @current_byte
end

#current_byte=(value) ⇒ Object



212
213
214
215
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 212

def current_byte= (value)
  @current_byte = value
  @current_address = (@current_byte+@current_bit/8.0)
end

#dumpObject



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 255

def dump
  serializable_values = @serializable.attributes
  self.start_address = @options[:start_address] || 0

  buffer = [] # Data Buffer
  tmp_buffer = [] # Aux Data Buffer

  self.current_address = self.start_address # Address in bytes

  @serializable.attr_config.each do |attr_options|
    attr_name = attr_options[:name]
    if attr_options[:type] != :nest
      var = attr_options[:coder].new(attr_options.merge(parent: @serializable))
      var.value = serializable_values[attr_name] rescue nil
    else
      var_value = serializable_values[attr_name].attributes rescue nil
      var = attr_options[:coder].new(var_value)
    end

    tmp_buffer = var.dump

    align_data(attr_options, var) if @options[:align]

    # Si los datos ocupan mas de un byte concatena los arrays
    if !attr_options[:type].in? [:bitfield, :bool] and @options[:align]
      buffer.insert(self.current_byte, tmp_buffer).flatten!
    else # En caso de ser bits
      tmp_buffer.flatten!
      tmp_bits=tmp_buffer.pack('C*').unpack('b*').first.slice(0,var.size*8)
      tmp_buffer=[tmp_bits.rjust(tmp_bits.length+self.current_bit,'0')].pack('b*').unpack('C*')

      tmp_buffer.each_with_index do |v,i|
        buffer[self.current_byte+i] = (buffer[self.current_byte+i] || 0) | v
      end
    end

    self.current_address += var.size
  end
  buffer.map!{|el| el || 0}
end

#load(buffer = []) ⇒ Object

deserializado



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 297

def load (buffer=[])
  serialized_values = {}
  self.start_address = @options[:start_address] || 0

  buffer ||= [] # Buffer en bytes
  tmp_buffer = [] # Buffer temporal en bytes

  self.current_address = self.start_address # Dirección en bytes

  @serializable.attr_config.each do |attr_options|
    attr_name = attr_options[:name]

    var = attr_options[:coder].new(attr_options.merge(parent: @serializable)) #creo objeto del tipo de dato pasado

    align_data(attr_options, var) if @options[:align]

    # Si los datos ocupan mas de un byte, obtiene los bytes completos del buffer original
    if !attr_options[:type].in? [:bitfield, :bool] and @options[:align]
      result_deserialized=var.load(buffer.slice(self.current_byte, var.size))
    else # En caso de ser bits
      tmp_buffer = buffer.slice(self.current_byte, (var.size+self.current_bit/8.0).ceil)
      result_deserialized=var.load([tmp_buffer.pack('C*').unpack('b*').first.slice(self.current_bit,var.size*8)].pack('b*').unpack('C*'))
    end

    if attr_options[:type] == :nest
      serialized_values["#{attr_name}"] = result_deserialized
    else
      serialized_values["#{attr_name}"] = result_deserialized.count>1 ? result_deserialized : result_deserialized.first
    end
    self.current_address += var.size
  end

  # Asigno los datos leidos
  serialized_values.each do |k,v|
    @serializable.send("#{k}=", v)
  end
  @serializable
end

#sizeObject

Return size of object in bytes



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 337

def size
  serializable_values = @serializable.attributes
  self.start_address = @options[:start_address] || 0

  current_address = 0.0 # Dirección en bytes

  @serializable.attr_config.each do |attr_options|
    var = attr_options[:coder].new(attr_options.merge(parent: @serializable))

    align_data(attr_options, var) if @options[:align]

    self.current_address += var.size
  end
  self.current_address-self.start_address
end