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
-
#from_bytes(buffer, options = {}) { ... } ⇒ Object
(also: #load)
Sets the model
attributesfrom an Binary string. - #from_words(buffer = [], options = {}, &block) ⇒ Object
- #size(options = {}) ⇒ Object
-
#to_bytes(options = {}, &block) ⇒ Object
(also: #dump)
Returns a binary array representing the model.
- #to_words(options = {}, &block) ⇒ Object
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">
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, = {}, &block) = self..merge() retVal = Serializer.new(self, ).load buffer if block_given? yield self, 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 = [], = {}, &block) if self.[:endianess]==:big data = buffer.pack('S>*').unpack('C*') else data = buffer.pack('S<*').unpack('C*') end from_bytes(data, , &block) end |
#size(options = {}) ⇒ Object
440 441 442 443 |
# File 'lib/active_model_serializers_binary/active_model_serializers_binary.rb', line 440 def size( = {}) = self..merge() Serializer.new(self, ).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( = {}, &block) = self..merge() if block_given? yield self, end Serializer.new(self, ).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( = {}, &block) data = to_bytes(, &block) data.push(0) if data.count.odd? if self.[:endianess]==:big data.pack('C*').unpack('S>*') else data.pack('C*').unpack('S<*') end end |