module ActiveModel::Serializers::Binary

Active Model Binary serializer

Public Class Methods

new( *args ) click to toggle source
Calls superclass method
# File lib/active_model_serializers_binary/active_model_serializers_binary.rb, line 28
def initialize( *args )
  initialize_serializer
  super rescue super()
end

Public Instance Methods

attributes() click to toggle source
Calls superclass method
# File lib/active_model_serializers_binary/active_model_serializers_binary.rb, line 22
def attributes
  keys = self.attr_config.select{ |attr| attr[:virtual]==true }.map{ |attr| attr[:name] }
  values = keys.map{ |attr| self.instance_variable_get("@#{attr}") }
  (super rescue {}).merge(Hash[keys.zip values])
end
dump(options = {}, &block)
Alias for: to_bytes
from_bytes(buffer, options = {}) { |self| ... } click to toggle source

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">

@param [Array] buffer byte array with model data to deserialize @param [Hash] options deserealization options

@return [Object] Deserialized object

@yield code block to execute after deserialization

# File lib/active_model_serializers_binary/active_model_serializers_binary.rb, line 396
def from_bytes(buffer, options = {}, &block)
  options = self.serialize_options_global.deep_merge(options)
  retVal = Serializer.new(self, options).load buffer
  
  if block_given?
    yield self
  end
  retVal
end
Also aliased as: load
from_words(buffer = [], options = {}, &block) click to toggle source
# File lib/active_model_serializers_binary/active_model_serializers_binary.rb, line 408
def from_words(buffer = [], options = {}, &block)
  data = buffer.pack('v*').unpack('C*')
  from_bytes(data, options, &block)
end
initialize_serializer() click to toggle source
# File lib/active_model_serializers_binary/active_model_serializers_binary.rb, line 33
def initialize_serializer
  self.class.add_virtual_attributes self
end
load(buffer, options = {}, &block)
Alias for: from_bytes
size(options = {}) click to toggle source
# File lib/active_model_serializers_binary/active_model_serializers_binary.rb, line 413
def size(options = {})
  options = self.serialize_options_global.deep_merge(options)
  Serializer.new(self, options).size
end
to_bytes(options = {}) { |self| ... } click to toggle source

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]
# File lib/active_model_serializers_binary/active_model_serializers_binary.rb, line 344
def to_bytes(options = {}, &block)
  options = self.serialize_options_global.deep_merge(options)
  if block_given?
      yield self
  end
  Serializer.new(self, options).dump
end
Also aliased as: dump
to_words(options = {}, &block) click to toggle source
# File lib/active_model_serializers_binary/active_model_serializers_binary.rb, line 354
def to_words(options = {}, &block)
  data = to_bytes(options, &block)
  byte_count = (data.count/2.0).ceil*2
  data.fill(0, data.count...byte_count).pack('C*').unpack('v*')
end