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 Method Summary collapse

Constructor Details

#initialize(serializable, options = nil) ⇒ Serializer

attr_reader :options



114
115
116
117
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 114

def initialize(serializable, options = nil)
  @serializable = serializable
  @options = options ? options.dup : {}
end

Instance Method Details

#dumpObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 119

def dump
  serializable_values = @serializable.serializable_hash(@options)
  start_address = @options[:start_address] || 0

  buffer = [] # Data Buffer
  tmp_buffer = [] # Aux Data Buffer
  current_address = start_address*2 + 0.0 # Address in bytes

  @serializable.attr_config.each do |attr_name, attr_options|
    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 = attr_options[:coder].new(serializable_values[attr_name].attributes)
    end

    byte = current_address.floor
    bit = (current_address.modulo(1)*8).round

    tmp_buffer = var.dump

    if @options[:align]
      if !attr_options[:type].in? [:bitfield, :bool, :nest]
        # Se posiciona al principio de un byte
        if bit != 0
          byte += 1
          bit = 0
          current_address = (byte+bit/8.0)
        end
        # Si el dato es una palabra simple, alinea los datos en el siguiente byte par
        if var.bit_length > 8 and current_address.modulo(2) != 0
          byte += 1
          bit = 0
        end
        # Si el dato es una palabra doble, alinea los datos en la siguiente palabra par
        if var.bit_length > 16 and (current_address + start_address*2).modulo(4) != 0
          byte += 4-byte%4
          bit = 0
        end
      end
    end

    # Si los datos ocupan mas de un byte concatena los arrays
    if !attr_options[:type].in? [:bitfield, :bool] and @options[:align]
      buffer.insert(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+bit,'0')].pack('b*').unpack('C*')

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

    current_address = (byte+bit/8.0)+var.size
  end
  buffer.map!{|el| el || 0}
end

#load(buffer = []) ⇒ Object

deserializado



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 180

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

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

  current_address = start_address*2 + 0.0 # Dirección en bytes

  @serializable.attr_config.each do |attr, attr_options|
    byte = current_address.floor
    bit = (current_address.modulo(1)*8).round

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

    if @options[:align]
      if !attr_options[:type].in? [:bitfield, :bool, :nest]
        # Se posiciona al principio de un byte
        if bit != 0
          byte += 1
          bit = 0
          current_address = (byte+bit/8.0)
        end
        # Si el dato es una palabra simple, alinea los datos en el siguiente byte par
        if var.bit_length > 8 and current_address.modulo(2) != 0
          byte += 1
          bit = 0
        end
        # Si el dato es una palabra doble, alinea los datos en la siguiente palabra par
        if var.bit_length > 16 and (current_address + start_address*2).modulo(4) != 0
          byte += 4-byte%4
          bit = 0
        end
      end
    end

    # 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(byte, var.size))
    else # En caso de ser bits
      tmp_buffer = buffer.slice(byte, (var.size+bit/8.0).ceil)
      result_deserialized=var.load([tmp_buffer.pack('C*').unpack('b*').first.slice(bit,var.size*8)].pack('b*').unpack('C*'))
    end

    if attr_options[:type] == :nest
      serialized_values["#{attr}"] = result_deserialized
    else
      serialized_values["#{attr}"] = result_deserialized.count>1 ? result_deserialized : result_deserialized.first
    end
    current_address = (byte+bit/8.0)+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



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 240

def size
  serializable_values = @serializable.serializable_hash(@options)
  start_address = @options[:start_address] || 0

  current_address = 0.0 # Dirección en bytes

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

    byte = current_address.floor
    bit = (current_address.modulo(1)*8).round

    if @options[:align]
      if !attr_options[:type].in? [:bitfield, :bool, :nest]
        # Se posiciona al principio de un byte
        if bit != 0
          byte += 1
          bit = 0
          current_address = (byte+bit/8.0)
        end
        # Si el dato es una palabra simple, alinea los datos en el siguiente byte par
        if var.bit_length > 8 and current_address.modulo(2) != 0
          byte += 1
          bit = 0
        end
        # Si el dato es una palabra doble, alinea los datos en la siguiente palabra par
        if var.bit_length > 16 and (current_address + start_address*2).modulo(4) != 0
          byte += 4-byte%4
          bit = 0
        end
      end
    end

    current_address = (byte+bit/8.0)+var.size
  end
  current_address-start_address
end