Module: ActiveModel::Serializers::Binary

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Serialization, DataTypes
Defined in:
lib/active_model_serializers_binary/active_model_serializers_binary.rb

Overview

Active Model Binary serializer

Defined Under Namespace

Modules: ClassMethods Classes: Serializer

Instance Method Summary collapse

Instance Method Details

#from_bytes(buffer, options = {}) { ... } ⇒ Object Also known as: load

Sets the model attributes from an Binary string. Returns self.

class Person
  include ActiveModel::Serializers::Binary

  attr_accessor :name, :age, :awesome

  def attributes=(hash)
    hash.each do |key, value|
      instance_variable_set("@#{key}", value)
    end
  end

  def attributes
    instance_values
  end

  char :name, count: 1, length: 10
  int16 :age
  bool :awesome
end

bytes = [98, 111, 98, 0, 0, 0, 0, 0, 0, 0, 22, 0, 1]
person = Person.new
person.from_bytes(bytes) do |p|
  p.name.upcase!
end
=> #<Person:0x007fec5e3b3c40 @age=22, @awesome=true, @name="bob">

Parameters:

  • buffer (Array)

    byte array with model data to deserialize

  • options (Hash) (defaults to: {})

    deserealization options

Yields:

  • code block to execute after deserialization

Returns:

  • (Object)

    Deserialized object



419
420
421
422
423
424
425
426
427
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 419

def from_bytes(buffer, options = {}, &block)
  options = self.serialize_options_global.merge(options)
  retVal = Serializer.new(self, options).load buffer
  
  if block_given?
    yield self, options
  end
  retVal
end

#from_words(buffer = [], options = {}, &block) ⇒ Object



431
432
433
434
435
436
437
438
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 431

def from_words(buffer = [], options = {}, &block)
  if self.serialize_options_global[:endianess]==:big
    data = buffer.pack('S>*').unpack('C*')
  else
    data = buffer.pack('S<*').unpack('C*')
  end
  from_bytes(data, options, &block)
end

#size(options = {}) ⇒ Object



440
441
442
443
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 440

def size(options = {})
  options = self.serialize_options_global.merge(options)
  Serializer.new(self, options).size
end

#to_bytes(options = {}, &block) ⇒ Object Also known as: dump

Returns a binary array representing the model. Configuration can be passed through options.

person = Person.find(1)
person.to_bytes

=> [98, 111, 98, 0, 0, 0, 0, 0, 0, 0, 22, 0, 1]


363
364
365
366
367
368
369
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 363

def to_bytes(options = {}, &block)
  options = self.serialize_options_global.merge(options)
  if block_given?
      yield self, options
  end
  Serializer.new(self, options).dump
end

#to_words(options = {}, &block) ⇒ Object



373
374
375
376
377
378
379
380
381
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 373

def to_words(options = {}, &block)
  data = to_bytes(options, &block)
  data.push(0) if data.count.odd?
  if self.serialize_options_global[:endianess]==:big
    data.pack('C*').unpack('S>*')
  else
    data.pack('C*').unpack('S<*')
  end
end