Class: DataTypes::BaseType

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model_serializers_binary/base_type.rb

Direct Known Subclasses

BitField, Bool, Char, Float32, Int16, Int32, Int8, UInt16, UInt32, UInt8

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BaseType

Returns a new instance of BaseType.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/active_model_serializers_binary/base_type.rb', line 7

def initialize(options = {})
  @default_value = options[:default_value].nil? ? 0 : options[:default_value]
  @raw_value = nil
  @bit_length = options[:bit_length]        # Cantidad de bits del tipo de dato
  @type = type
  @sign = options[:sign]                    # :signed / :unsigned
  @count = options[:count] || 1             # Cantidad de elementos del array
  @length = options[:length]  || 1          # En char y bitfield especifica la longitud del campo. Ignorado para el resto de los tipos
  @value = check_value( @default_value )
  @block = options[:block]
  @name = options[:name]
  @parent = options[:parent]
end

Instance Attribute Details

#bit_lengthObject

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

#countObject

Returns the value of attribute count.



5
6
7
# File 'lib/active_model_serializers_binary/base_type.rb', line 5

def count
  @count
end

#lengthObject

Returns the value of attribute length.



5
6
7
# File 'lib/active_model_serializers_binary/base_type.rb', line 5

def length
  @length
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/active_model_serializers_binary/base_type.rb', line 5

def name
  @name
end

#parentObject

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_valueObject

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

#signObject

Returns the value of attribute sign.



5
6
7
# File 'lib/active_model_serializers_binary/base_type.rb', line 5

def sign
  @sign
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/active_model_serializers_binary/base_type.rb', line 5

def type
  @type
end

#valueObject

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_loadArray

Se ejecuta después de deserializar los datos. @value contiene los datos deserializados

Returns:

  • (Array)

    Array con los datos deserializados



130
131
132
133
134
135
# File 'lib/active_model_serializers_binary/base_type.rb', line 130

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

Parameters:

  • value (Object)

    valor del objeto a serializar original

Returns:

  • (Array)

    nuevo valor del objeto a serializar



116
117
118
119
120
121
122
# File 'lib/active_model_serializers_binary/base_type.rb', line 116

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



34
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
# File 'lib/active_model_serializers_binary/base_type.rb', line 34

def check( value, options = {} )
  type = options[:type]
  count = options[:count]
  length = options[:length]
  bit_length = options[:bit_length]
  sign = options[:sign]
  default_value = options[: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
          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



80
81
82
83
84
85
86
87
88
89
# File 'lib/active_model_serializers_binary/base_type.rb', line 80

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



68
69
70
71
72
73
74
75
76
77
# File 'lib/active_model_serializers_binary/base_type.rb', line 68

def check_value(value)
  check(value, {
    :type => @type,
    :count => @count,
    :length => @length,
    :bit_length => @bit_length,
    :sign => @sign,
    :default_value => @default_value,
    })
end

#sizeObject

Return size of object in bytes



30
31
32
# File 'lib/active_model_serializers_binary/base_type.rb', line 30

def size
  (@bit_length*@length*@count)/8.0
end

#to_sObject



21
22
23
# File 'lib/active_model_serializers_binary/base_type.rb', line 21

def to_s
  @value.to_s
end

#trim(value, bit_length, sign) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/active_model_serializers_binary/base_type.rb', line 91

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