Class: DataTypes::BaseType
- Inherits:
-
Object
- Object
- DataTypes::BaseType
- Defined in:
- lib/active_model_serializers_binary/base_type.rb
Direct Known Subclasses
BitField, Bool, Char, Float32, Float64, Int16, Int32, Int8, UInt16, UInt32, UInt8
Instance Attribute Summary collapse
-
#bit_length ⇒ Object
Returns the value of attribute bit_length.
-
#count ⇒ Object
Returns the value of attribute count.
-
#endianess ⇒ Object
Returns the value of attribute endianess.
-
#length ⇒ Object
Returns the value of attribute length.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#raw_value ⇒ Object
Returns the value of attribute raw_value.
-
#sign ⇒ Object
Returns the value of attribute sign.
-
#type ⇒ Object
Returns the value of attribute type.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#after_load ⇒ Array
Se ejecuta después de deserializar los datos.
-
#before_dump(value) ⇒ Array
Se ejecuta antes de serializar los datos.
- #check(value, options = {}) ⇒ Object
-
#check_raw_value(value) ⇒ Object
Los datos siempre vienen en bytes.
- #check_value(value) ⇒ Object
-
#initialize(options = {}) ⇒ BaseType
constructor
A new instance of BaseType.
-
#size ⇒ Object
Return size of object in bytes.
- #to_s ⇒ Object
- #trim(value, bit_length, sign) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ BaseType
Returns a new instance of BaseType.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/active_model_serializers_binary/base_type.rb', line 7 def initialize( = {}) @default_value = [:default_value].nil? ? 0 : [:default_value] @raw_value = nil @bit_length = [:bit_length] # Cantidad de bits del tipo de dato @type = type @sign = [:sign] # :signed / :unsigned @count = [:count] || 1 # Cantidad de elementos del array @length = [:length] || 1 # En char y bitfield especifica la longitud del campo. Ignorado para el resto de los tipos @value = check_value( @default_value ) @block = [:block] @name = [:name] @parent = [:parent] @endianess = [:endianess] || :little end |
Instance Attribute Details
#bit_length ⇒ Object
Returns the value of attribute bit_length.
5 6 7 |
# File 'lib/active_model_serializers_binary/base_type.rb', line 5 def bit_length @bit_length end |
#count ⇒ Object
Returns the value of attribute count.
5 6 7 |
# File 'lib/active_model_serializers_binary/base_type.rb', line 5 def count @count end |
#endianess ⇒ Object
Returns the value of attribute endianess.
5 6 7 |
# File 'lib/active_model_serializers_binary/base_type.rb', line 5 def endianess @endianess end |
#length ⇒ Object
Returns the value of attribute length.
5 6 7 |
# File 'lib/active_model_serializers_binary/base_type.rb', line 5 def length @length end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/active_model_serializers_binary/base_type.rb', line 5 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
5 6 7 |
# File 'lib/active_model_serializers_binary/base_type.rb', line 5 def parent @parent end |
#raw_value ⇒ Object
Returns the value of attribute raw_value.
5 6 7 |
# File 'lib/active_model_serializers_binary/base_type.rb', line 5 def raw_value @raw_value end |
#sign ⇒ Object
Returns the value of attribute sign.
5 6 7 |
# File 'lib/active_model_serializers_binary/base_type.rb', line 5 def sign @sign end |
#type ⇒ Object
Returns the value of attribute type.
5 6 7 |
# File 'lib/active_model_serializers_binary/base_type.rb', line 5 def type @type end |
#value ⇒ Object
Returns the value of attribute value.
5 6 7 |
# File 'lib/active_model_serializers_binary/base_type.rb', line 5 def value @value end |
Instance Method Details
#after_load ⇒ Array
Se ejecuta después de deserializar los datos. @value contiene los datos deserializados
131 132 133 134 135 136 |
# File 'lib/active_model_serializers_binary/base_type.rb', line 131 def after_load if !@block.nil? @parent.instance_exec( self, :load, &@block ) end @value end |
#before_dump(value) ⇒ Array
Se ejecuta antes de serializar los datos
117 118 119 120 121 122 123 |
# File 'lib/active_model_serializers_binary/base_type.rb', line 117 def before_dump(value) self.value = value if !value.nil? if !@block.nil? value = @parent.instance_exec( self, :dump, &@block ) end self.value = value if !value.nil? end |
#check(value, options = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/active_model_serializers_binary/base_type.rb', line 35 def check( value, = {} ) type = [:type] count = [:count] length = [:length] bit_length = [:bit_length] sign = [:sign] default_value = [:default_value] value = Array(value) # Se asegura de que sea un array value = value[0...count] # Corta el array según la cantidad de elementos especificados en la declaración # Lo convierte al tipo especificado value.map! do |v| if v.nil? default_value else case type when :float32, :float64 v.to_f when :char v.to_s[0...length] when :bool (v.in? [1, true]) ? true : false when :nest v else v.to_i end end end trim(value, bit_length, sign) # Se asegura de que los valores esten dentro de los rangos permitidos pra el tipo de dato declarado value.fill(default_value, value.length...count) # Completa los elementos faltantes del array con default_value end |
#check_raw_value(value) ⇒ Object
Los datos siempre vienen en bytes
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/active_model_serializers_binary/base_type.rb', line 81 def check_raw_value(value) check(value, { :type => :uint8, :count => (size < 1 ? 1 : size), :length => 1, :bit_length => 8, :sign => :unsigned, :default_value => 0, }) end |
#check_value(value) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/active_model_serializers_binary/base_type.rb', line 69 def check_value(value) check(value, { :type => @type, :count => @count, :length => @length, :bit_length => @bit_length, :sign => @sign, :default_value => @default_value, }) end |
#size ⇒ Object
Return size of object in bytes
31 32 33 |
# File 'lib/active_model_serializers_binary/base_type.rb', line 31 def size (@bit_length*@length*@count)/8.0 end |
#to_s ⇒ Object
22 23 24 |
# File 'lib/active_model_serializers_binary/base_type.rb', line 22 def to_s @value.to_s end |
#trim(value, bit_length, sign) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/active_model_serializers_binary/base_type.rb', line 92 def trim(value, bit_length, sign) # Recorta los valores según el bit_length value.map! do |v| if sign == :signed [-2**(bit_length-1),[v.to_i,2**(bit_length-1)-1].min].max elsif sign == :unsigned [0,[v.to_i,2**(bit_length)-1].min].max else v end end end |