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.



77
78
79
80
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 77

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



75
76
77
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 75

def options
  @options
end

Instance Method Details

#dumpObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 82

def dump
  serializable_values = @serializable.serializable_hash(options)
  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 |key, options|
    var = options[:coder].new(options.merge(parent: @serializable))
    # Busca el valor del atributo y si no existe devuelve nil
    var.value = serializable_values[key] rescue nil

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

    tmp_buffer = var.dump
    if @options[:align]
      if !var.type.in? [:bitfield, :bool]
        # 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 !var.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



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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 139

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 |key, options|
    byte = current_address.floor
    bit = (current_address.modulo(1)*8).round

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

    if @options[:align]
      if !var.type.in? [:bitfield, :bool]
        # 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 var.bit_length >= 8 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
    serialized_values["#{key}"] = result_deserialized.count>1 ? result_deserialized : result_deserialized.first
    current_address = (byte+bit/8.0)+var.size
  end

  # Asigno los datos leidos
  serialized_values.each do |k,v|
    if @serializable.respond_to? "#{k}="
      @serializable.send("#{k}=", v)
    else
      @serializable.instance_variable_set("@#{k}".to_sym, v)
    end
  end
  @serializable
end