active_model_serializers_binary 0.1.5 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8867fc2c627dc2fe2e4e7c064bd1ed5ae721eb89
4
- data.tar.gz: b096205753623930ad89c0c9dd4d308f3ff8ef19
3
+ metadata.gz: 876801a003f61b57070687c4049198f3a7407a21
4
+ data.tar.gz: e9c677a84305f640f0f77280d01767246da97f9d
5
5
  SHA512:
6
- metadata.gz: c89aed42e8c94feb10867ee254dec8638e9ecae1ed8b414cef0792ec08e50e000d08dc99c0ea1e0356bbf1efc19b2a086b213efb63d4b69b69b07f8a9b148804
7
- data.tar.gz: 7acb69425c7502733109d4200fb255951a93f5354d8cb2d3b6c1662948f521264d470099cab822e472593250aac36eeb4868a34e86a80077988c4c0b30258898
6
+ metadata.gz: 34ed74da0e671a9be0e153940416150b8550e091d8e5ee10ab8c74caf89c30cbe963dfe62cdb47b3926b13b9cd9e7140d1617473e42d27197f7873e1fa0ba859
7
+ data.tar.gz: f7e0803f4e919c2c7b2c3d7e32718f0b912aaeff3be7288b86016c26bc21b3dbeb1ff1edc4bea9af6783dcb189d929fcae2ac1a8d961d389e36edd4c89c027d9
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- active_model_serializers_binary (0.1.5)
4
+ active_model_serializers_binary (0.2.0)
5
5
  activemodel (~> 5.0)
6
6
 
7
7
  GEM
@@ -14,11 +14,16 @@ module ActiveModel
14
14
  extend ActiveModel::Naming
15
15
 
16
16
  class_attribute :attr_config
17
- self.attr_config = {}
17
+ class_attribute :serialize_options_global
18
+ self.attr_config = []
19
+ self.serialize_options_global = {
20
+ align: false, # Don't align data to byte/word/dword boundary
21
+ endianess: :little
22
+ }
18
23
 
19
24
  def attributes
20
- keys = self.attr_config.select{ |k, v| v[:virtual]==true }.keys
21
- values = keys.map{ |var| self.instance_variable_get("@#{var}") }
25
+ keys = self.attr_config.select{ |attr| attr[:virtual]==true }.map{ |attr| attr[:name] }
26
+ values = keys.map{ |attr| self.instance_variable_get("@#{attr}") }
22
27
  (super rescue {}).merge(Hash[keys.zip values])
23
28
  end
24
29
 
@@ -36,16 +41,16 @@ module ActiveModel
36
41
 
37
42
  module ClassMethods
38
43
  def add_virtual_attributes( instance )
39
- self.attr_config.each{ |k, v| add_virtual_attribute(instance, k, v) }
44
+ self.attr_config.each{ |attr| add_virtual_attribute(instance, attr) }
40
45
  end
41
46
 
42
- def add_virtual_attribute( instance, attr_name, attr_value )
43
- attr_name = attr_name.to_s
44
- if attr_value[:virtual] == true
47
+ def add_virtual_attribute( instance, attr )
48
+ if attr[:virtual] == true
45
49
  true
46
50
  else
51
+ attr_name = attr[:name].to_s
47
52
  if !instance.respond_to? attr_name
48
- attr_value[:virtual] = true
53
+ attr[:virtual] = true
49
54
  attr_accessor attr_name
50
55
  true
51
56
  else
@@ -56,56 +61,114 @@ module ActiveModel
56
61
 
57
62
  # todo: agrupar parametros en hash (rompe la compatibilidad hacia atras)
58
63
  def serialize_options( attr_name, coder, count=1, length=1, virtual=false, &block )
59
- self.attr_config.merge!(attr_name.to_s => {:coder => coder, :count => count, :length => length, :block => block, :name => attr_name, :virtual => virtual})
64
+ self.attr_config.push({:coder => coder, :count => count, :length => length, :block => block, :name => attr_name.to_s, :virtual => virtual})
65
+ if virtual==true
66
+ attr_accessor attr_name
67
+ end
60
68
  end
61
69
 
62
70
  def serialize_attribute_options( attr_name, options, &block )
63
- self.attr_config.merge!(attr_name.to_s => options.merge({ block: block }))
71
+ self.attr_config.push(options.merge({:name => attr_name.to_s, block: block }))
72
+ if options[:virtual]==true
73
+ attr_accessor attr_name
74
+ end
64
75
  end
65
76
 
66
77
  def int8( attr_name, options = {}, &block )
78
+ options = serialize_options_global.merge(options)
67
79
  serialize_attribute_options attr_name, options.merge({coder: DataTypes::Int8}), &block
68
80
  end
69
81
 
70
82
  def int16( attr_name, options = {}, &block )
83
+ options = serialize_options_global.merge(options)
71
84
  serialize_attribute_options attr_name, options.merge({coder: DataTypes::Int16}), &block
72
85
  end
73
86
 
87
+ def int16le( attr_name, options = {}, &block )
88
+ int16( attr_name, options.merge({endianess: :little}), &block )
89
+ end
90
+
91
+ def int16be( attr_name, options = {}, &block )
92
+ int16( attr_name, options.merge({endianess: :big}), &block )
93
+ end
94
+
74
95
  def int32( attr_name, options = {}, &block )
96
+ options = serialize_options_global.merge(options)
75
97
  serialize_attribute_options attr_name, options.merge({coder: DataTypes::Int32}), &block
76
98
  end
77
99
 
100
+ def int32le( attr_name, options = {}, &block )
101
+ int32( attr_name, options.merge({endianess: :little}), &block )
102
+ end
103
+
104
+ def int32be( attr_name, options = {}, &block )
105
+ int16( attr_name, options.merge({endianess: :big}), &block )
106
+ end
107
+
78
108
  def uint8( attr_name, options = {}, &block )
109
+ options = serialize_options_global.merge(options)
79
110
  serialize_attribute_options attr_name, options.merge({coder: DataTypes::UInt8}), &block
80
111
  end
81
112
 
82
113
  def uint16( attr_name, options = {}, &block )
114
+ options = serialize_options_global.merge(options)
83
115
  serialize_attribute_options attr_name, options.merge({coder: DataTypes::UInt16}), &block
84
116
  end
85
117
 
118
+ def uint16le( attr_name, options = {}, &block )
119
+ uint16( attr_name, options.merge({endianess: :little}), &block )
120
+ end
121
+
122
+ def uint16be( attr_name, options = {}, &block )
123
+ uint16( attr_name, options.merge({endianess: :big}), &block )
124
+ end
125
+
86
126
  def uint32( attr_name, options = {}, &block )
127
+ options = serialize_options_global.merge(options)
87
128
  serialize_attribute_options attr_name, options.merge({coder: DataTypes::UInt32}), &block
88
129
  end
89
130
 
131
+ def uint32le( attr_name, options = {}, &block )
132
+ uint32( attr_name, options.merge({endianess: :little}), &block )
133
+ end
134
+
135
+ def uint32be( attr_name, options = {}, &block )
136
+ uint32( attr_name, options.merge({endianess: :big}), &block )
137
+ end
138
+
90
139
  def bitfield( attr_name, options = {}, &block )
140
+ options = serialize_options_global.merge(options)
91
141
  serialize_attribute_options attr_name, options.merge({coder: DataTypes::BitField}), &block
92
142
  end
93
143
 
94
144
  def float32( attr_name, options = {}, &block )
145
+ options = serialize_options_global.merge(options)
146
+ serialize_attribute_options attr_name, options.merge({coder: DataTypes::Float32}), &block
147
+ end
148
+
149
+ def float64( attr_name, options = {}, &block )
150
+ options = serialize_options_global.merge(options)
95
151
  serialize_attribute_options attr_name, options.merge({coder: DataTypes::Float32}), &block
96
152
  end
97
153
 
98
154
  def char( attr_name, options = {}, &block )
155
+ options = serialize_options_global.merge(options)
99
156
  serialize_attribute_options attr_name, options.merge({coder: DataTypes::Char}), &block
100
157
  end
101
158
 
102
159
  def bool( attr_name, options = {}, &block )
160
+ options = serialize_options_global.merge(options)
103
161
  serialize_attribute_options attr_name, options.merge({coder: DataTypes::Bool}), &block
104
162
  end
105
163
 
106
164
  def nest( attr_name, options={}, &block )
165
+ options = serialize_options_global.merge(options)
107
166
  serialize_attribute_options attr_name, options.merge({type: :nest}), &block
108
167
  end
168
+
169
+ def endianess( type = :little )
170
+ serialize_options_global.merge!({endianess: type})
171
+ end
109
172
  end
110
173
 
111
174
  class Serializer #:nodoc:
@@ -124,12 +187,14 @@ module ActiveModel
124
187
  tmp_buffer = [] # Aux Data Buffer
125
188
  current_address = start_address*2 + 0.0 # Address in bytes
126
189
 
127
- @serializable.attr_config.each do |attr_name, attr_options|
190
+ @serializable.attr_config.each do |attr_options|
191
+ attr_name = attr_options[:name]
128
192
  if attr_options[:type] != :nest
129
193
  var = attr_options[:coder].new(attr_options.merge(parent: @serializable))
130
194
  var.value = serializable_values[attr_name] rescue nil
131
195
  else
132
- var = attr_options[:coder].new(serializable_values[attr_name].attributes)
196
+ var_value = serializable_values[attr_name].attributes rescue nil
197
+ var = attr_options[:coder].new(var_value)
133
198
  end
134
199
 
135
200
  byte = current_address.floor
@@ -186,7 +251,8 @@ module ActiveModel
186
251
 
187
252
  current_address = start_address*2 + 0.0 # Dirección en bytes
188
253
 
189
- @serializable.attr_config.each do |attr, attr_options|
254
+ @serializable.attr_config.each do |attr_options|
255
+ attr_name = attr_options[:name]
190
256
  byte = current_address.floor
191
257
  bit = (current_address.modulo(1)*8).round
192
258
 
@@ -222,9 +288,9 @@ module ActiveModel
222
288
  end
223
289
 
224
290
  if attr_options[:type] == :nest
225
- serialized_values["#{attr}"] = result_deserialized
291
+ serialized_values["#{attr_name}"] = result_deserialized
226
292
  else
227
- serialized_values["#{attr}"] = result_deserialized.count>1 ? result_deserialized : result_deserialized.first
293
+ serialized_values["#{attr_name}"] = result_deserialized.count>1 ? result_deserialized : result_deserialized.first
228
294
  end
229
295
  current_address = (byte+bit/8.0)+var.size
230
296
  end
@@ -243,7 +309,7 @@ module ActiveModel
243
309
 
244
310
  current_address = 0.0 # Dirección en bytes
245
311
 
246
- @serializable.attr_config.each do |attr_name, attr_options|
312
+ @serializable.attr_config.each do |attr_options|
247
313
  var = attr_options[:coder].new(attr_options.merge(parent: @serializable))
248
314
 
249
315
  byte = current_address.floor
@@ -269,7 +335,6 @@ module ActiveModel
269
335
  end
270
336
  end
271
337
  end
272
-
273
338
  current_address = (byte+bit/8.0)+var.size
274
339
  end
275
340
  current_address-start_address
@@ -286,10 +351,7 @@ module ActiveModel
286
351
  # => [98, 111, 98, 0, 0, 0, 0, 0, 0, 0, 22, 0, 1]
287
352
  #
288
353
  def to_bytes(options = {}, &block)
289
- default_options = {
290
- :align => true,
291
- }
292
- options = default_options.deep_merge(options)
354
+ options = self.serialize_options_global.deep_merge(options)
293
355
  if block_given?
294
356
  yield self
295
357
  end
@@ -341,10 +403,7 @@ module ActiveModel
341
403
  # @yield code block to execute after deserialization
342
404
  #
343
405
  def from_bytes(buffer, options = {}, &block)
344
- default_options = {
345
- :align => true
346
- }
347
- options = default_options.deep_merge(options)
406
+ options = self.serialize_options_global.deep_merge(options)
348
407
  retVal = Serializer.new(self, options).load buffer
349
408
 
350
409
  if block_given?
@@ -361,10 +420,7 @@ module ActiveModel
361
420
  end
362
421
 
363
422
  def size(options = {})
364
- default_options = {
365
- :align => true
366
- }
367
- options = default_options.deep_merge(options)
423
+ options = self.serialize_options_global.deep_merge(options)
368
424
  Serializer.new(self, options).size
369
425
  end
370
426
 
@@ -2,7 +2,7 @@ module DataTypes
2
2
 
3
3
  class BaseType
4
4
 
5
- attr_accessor :raw_value, :bit_length, :type, :sign, :count, :length, :value, :name, :parent
5
+ attr_accessor :raw_value, :bit_length, :type, :sign, :count, :length, :value, :name, :parent, :endianess
6
6
 
7
7
  def initialize(options = {})
8
8
  @default_value = options[:default_value].nil? ? 0 : options[:default_value]
@@ -16,6 +16,7 @@ module DataTypes
16
16
  @block = options[:block]
17
17
  @name = options[:name]
18
18
  @parent = options[:parent]
19
+ @endianess = options[:endianess] || :little
19
20
  end
20
21
 
21
22
  def to_s
@@ -1,187 +1,251 @@
1
- require_relative 'base_type.rb'
2
-
3
- module DataTypes
4
-
5
- class Int8 < BaseType
6
- def initialize(options = {})
7
- super options.merge :bit_length => 8, :sign => :signed
8
- end
9
-
10
- def dump(value=nil)
11
- before_dump( value ).pack('c*').unpack('C*')
12
- @raw_value = @value
13
- end
14
-
15
- def load(raw_value)
16
- self.value = check_raw_value(raw_value).pack('C*').unpack('c*')
17
- after_load
18
- end
19
- end
20
-
21
- class Int16 < BaseType
22
- def initialize(options = {})
23
- super options.merge :bit_length => 16, :sign => :signed
24
- end
25
-
26
- def dump(value=nil)
27
- before_dump( value )
28
- @raw_value = @value.pack('s<*').unpack('C*')
29
- end
30
-
31
- def load(raw_value)
32
- self.value = check_raw_value(raw_value).pack('C*').unpack('s<*')
33
- after_load
34
- end
35
- end
36
-
37
- class Int32 < BaseType
38
- def initialize(options = {})
39
- super options.merge :bit_length => 32, :sign => :signed
40
- end
41
-
42
- def dump(value=nil)
43
- before_dump( value )
44
- @raw_value = @value.pack('l<*').unpack('C*')
45
- end
46
-
47
- def load(raw_value)
48
- self.value = check_raw_value(raw_value).pack('C*').unpack('l<*') if !value.nil?
49
- after_load
50
- end
51
- end
52
-
53
- class UInt16 < BaseType
54
- def initialize(options = {})
55
- super options.merge :bit_length => 16, :sign => :unsigned
56
- end
57
-
58
- def dump(value=nil)
59
- before_dump( value )
60
- @raw_value = @value.pack('S<*').unpack('C*')
61
- end
62
-
63
- def load(raw_value)
64
- @raw_value = check_raw_value(raw_value)
65
- self.value = @raw_value.pack('C*').unpack('S<*')
66
- after_load
67
- end
68
- end
69
-
70
- class UInt32 < BaseType
71
- def initialize(options = {})
72
- super options.merge :bit_length => 32, :sign => :unsigned
73
- end
74
-
75
- def dump(value=nil)
76
- before_dump( value )
77
- @raw_value = @value.pack('L<*').unpack('C*')
78
- end
79
-
80
- def load(raw_value)
81
- self.value = check_raw_value(raw_value).pack('C*').unpack('L<*') if !value.nil?
82
- after_load
83
- end
84
- end
85
-
86
- class UInt8 < BaseType
87
- def initialize(options = {})
88
- super options.merge :bit_length => 8, :sign => :unsigned
89
- end
90
-
91
- def dump(value=nil)
92
- before_dump( value )
93
- @raw_value = @value
94
- end
95
-
96
- def load(raw_value)
97
- self.value = check_raw_value(raw_value)
98
- after_load
99
- end
100
- end
101
-
102
- class BitField < BaseType
103
- def initialize(options = {})
104
- length = options[:bin_length].blank? ? 1 : (options[:bin_length] > 32 ? 32 : options[:bin_length])
105
- super options.merge :bit_length => length, :sign => :unsigned
106
- end
107
-
108
- def format
109
- if bit_length <= 8 # 8 bits
110
- 'C*'
111
- elsif bit_length <= 16 # 16 bits
112
- 'v*'
113
- else # 32 bits
114
- 'l*'
115
- end
116
- end
117
-
118
- def word_length
119
- if bit_length <= 8 # 8 bits
120
- 8
121
- elsif bit_length <= 16 # 16 bits
122
- 16
123
- else # 32 bits
124
- 32
125
- end
126
- end
127
-
128
- def dump(value=nil)
129
- before_dump( value )
130
- data = @value.pack(format).unpack('b*').first.chars.each_slice(word_length).map(&:join).map{|n| n.slice(0,bit_length)}
131
- @raw_value = [data.join].pack('b*').unpack('C*')
132
- end
133
-
134
- def load(raw_value)
135
- self.value = check_raw_value(raw_value).pack('C*').unpack('b*').first.chars.slice(0,@bit_length*@count).each_slice(bit_length).map(&:join).map{|n| [n].pack('b*').unpack('C*').first}
136
- after_load
137
- end
138
- end
139
-
140
- class Char < BaseType
141
- def initialize(options = {})
142
- super options.merge :bit_length => 8, :sign => nil, :default_value => "\x0"
143
- end
144
-
145
- def dump(value=nil)
146
- before_dump( value )
147
- @raw_value = @value.map{|v| v.ljust(@length, @default_value).slice(0,@length).unpack('C*')}
148
- end
149
-
150
- def load(raw_value)
151
- self.value = check_raw_value(raw_value).pack('C*').unpack("Z#{@length}") if !value.nil?
152
- after_load
153
- end
154
- end
155
-
156
- class Bool < BaseType
157
- def initialize(options = {})
158
- super options.merge :bit_length => 1, :default_value => false
159
- end
160
-
161
- def dump(value=nil)
162
- before_dump( value )
163
- @raw_value = Array(@value.map{|v| v ? 1 : 0}.join).pack('b*').unpack('C*')
164
- end
165
-
166
- def load(raw_value)
167
- self.value = check_raw_value(raw_value).pack('C*').unpack('b*').first.slice(0,size*8).split('').map(&:to_i) if !value.nil?
168
- after_load
169
- end
170
- end
171
-
172
- class Float32 < BaseType
173
- def initialize(options = {})
174
- super options.merge :bit_length => 32, :sign => nil, :default_value => 0.0
175
- end
176
-
177
- def dump(value=nil)
178
- before_dump( value )
179
- @raw_value = @value.pack('e*').unpack('C*')
180
- end
181
-
182
- def load(raw_value)
183
- self.value = check_raw_value(raw_value).pack('C*').unpack('e*') if !value.nil?
184
- after_load
185
- end
186
- end
1
+ require_relative 'base_type.rb'
2
+
3
+ module DataTypes
4
+
5
+ class Int8 < BaseType
6
+ def initialize(options = {})
7
+ super options.merge :bit_length => 8, :sign => :signed
8
+ end
9
+
10
+ def dump(value=nil)
11
+ before_dump( value ).pack('c*').unpack('C*')
12
+ @raw_value = @value
13
+ end
14
+
15
+ def load(raw_value)
16
+ self.value = check_raw_value(raw_value).pack('C*').unpack('c*')
17
+ after_load
18
+ end
19
+ end
20
+
21
+ class Int16 < BaseType
22
+ def initialize(options = {})
23
+ super options.merge :bit_length => 16, :sign => :signed
24
+ end
25
+
26
+ def dump(value=nil)
27
+ before_dump( value )
28
+ if @endianess == :big
29
+ @raw_value = @value.pack('s>*').unpack('C*')
30
+ else
31
+ @raw_value = @value.pack('s<*').unpack('C*')
32
+ end
33
+ end
34
+
35
+ def load(raw_value)
36
+ if @endianess == :big
37
+ self.value = check_raw_value(raw_value).pack('C*').unpack('s>*')
38
+ else
39
+ self.value = check_raw_value(raw_value).pack('C*').unpack('s<*')
40
+ end
41
+ after_load
42
+ end
43
+ end
44
+
45
+ class Int32 < BaseType
46
+ def initialize(options = {})
47
+ super options.merge :bit_length => 32, :sign => :signed
48
+ end
49
+
50
+ def dump(value=nil)
51
+ before_dump( value )
52
+ if @endianess == :big
53
+ @raw_value = @value.pack('l>*').unpack('C*')
54
+ else
55
+ @raw_value = @value.pack('l<*').unpack('C*')
56
+ end
57
+ end
58
+
59
+ def load(raw_value)
60
+ if @endianess == :big
61
+ self.value = check_raw_value(raw_value).pack('C*').unpack('l>*') if !value.nil?
62
+ else
63
+ self.value = check_raw_value(raw_value).pack('C*').unpack('l<*') if !value.nil?
64
+ end
65
+ after_load
66
+ end
67
+ end
68
+
69
+ class UInt16 < BaseType
70
+ def initialize(options = {})
71
+ super options.merge :bit_length => 16, :sign => :unsigned
72
+ end
73
+
74
+ def dump(value=nil)
75
+ before_dump( value )
76
+ if @endianess == :big
77
+ @raw_value = @value.pack('S>*').unpack('C*')
78
+ else
79
+ @raw_value = @value.pack('S<*').unpack('C*')
80
+ end
81
+ end
82
+
83
+ def load(raw_value)
84
+ @raw_value = check_raw_value(raw_value)
85
+ if @endianess == :big
86
+ self.value = @raw_value.pack('C*').unpack('S>*')
87
+ else
88
+ self.value = @raw_value.pack('C*').unpack('S<*')
89
+ end
90
+ after_load
91
+ end
92
+ end
93
+
94
+ class UInt32 < BaseType
95
+ def initialize(options = {})
96
+ super options.merge :bit_length => 32, :sign => :unsigned
97
+ end
98
+
99
+ def dump(value=nil)
100
+ before_dump( value )
101
+ if @endianess == :big
102
+ @raw_value = @value.pack('L>*').unpack('C*')
103
+ else
104
+ @raw_value = @value.pack('L<*').unpack('C*')
105
+ end
106
+ end
107
+
108
+ def load(raw_value)
109
+ if @endianess == :big
110
+ self.value = check_raw_value(raw_value).pack('C*').unpack('L>*') if !value.nil?
111
+ else
112
+ self.value = check_raw_value(raw_value).pack('C*').unpack('L<*') if !value.nil?
113
+ end
114
+ after_load
115
+ end
116
+ end
117
+
118
+ class UInt8 < BaseType
119
+ def initialize(options = {})
120
+ super options.merge :bit_length => 8, :sign => :unsigned
121
+ end
122
+
123
+ def dump(value=nil)
124
+ before_dump( value )
125
+ @raw_value = @value
126
+ end
127
+
128
+ def load(raw_value)
129
+ self.value = check_raw_value(raw_value)
130
+ after_load
131
+ end
132
+ end
133
+
134
+ class BitField < BaseType
135
+ def initialize(options = {})
136
+ length = options[:bin_length].blank? ? 1 : (options[:bin_length] > 32 ? 32 : options[:bin_length])
137
+ super options.merge :bit_length => length, :sign => :unsigned
138
+ end
139
+
140
+ def format
141
+ if bit_length <= 8 # 8 bits
142
+ 'C*'
143
+ elsif bit_length <= 16 # 16 bits
144
+ 'v*'
145
+ else # 32 bits
146
+ 'l*'
147
+ end
148
+ end
149
+
150
+ def word_length
151
+ if bit_length <= 8 # 8 bits
152
+ 8
153
+ elsif bit_length <= 16 # 16 bits
154
+ 16
155
+ else # 32 bits
156
+ 32
157
+ end
158
+ end
159
+
160
+ def dump(value=nil)
161
+ before_dump( value )
162
+ data = @value.pack(format).unpack('b*').first.chars.each_slice(word_length).map(&:join).map{|n| n.slice(0,bit_length)}
163
+ @raw_value = [data.join].pack('b*').unpack('C*')
164
+ end
165
+
166
+ def load(raw_value)
167
+ self.value = check_raw_value(raw_value).pack('C*').unpack('b*').first.chars.slice(0,@bit_length*@count).each_slice(bit_length).map(&:join).map{|n| [n].pack('b*').unpack('C*').first}
168
+ after_load
169
+ end
170
+ end
171
+
172
+ class Char < BaseType
173
+ def initialize(options = {})
174
+ super options.merge :bit_length => 8, :sign => nil, :default_value => "\x0"
175
+ end
176
+
177
+ def dump(value=nil)
178
+ before_dump( value )
179
+ @raw_value = @value.map{|v| v.ljust(@length, @default_value).slice(0,@length).unpack('C*')}
180
+ end
181
+
182
+ def load(raw_value)
183
+ self.value = check_raw_value(raw_value).pack('C*').unpack("Z#{@length}") if !value.nil?
184
+ after_load
185
+ end
186
+ end
187
+
188
+ class Bool < BaseType
189
+ def initialize(options = {})
190
+ super options.merge :bit_length => 1, :default_value => false
191
+ end
192
+
193
+ def dump(value=nil)
194
+ before_dump( value )
195
+ @raw_value = Array(@value.map{|v| v ? 1 : 0}.join).pack('b*').unpack('C*')
196
+ end
197
+
198
+ def load(raw_value)
199
+ self.value = check_raw_value(raw_value).pack('C*').unpack('b*').first.slice(0,size*8).split('').map(&:to_i) if !value.nil?
200
+ after_load
201
+ end
202
+ end
203
+
204
+ class Float32 < BaseType
205
+ def initialize(options = {})
206
+ super options.merge :bit_length => 32, :sign => nil, :default_value => 0.0
207
+ end
208
+
209
+ def dump(value=nil)
210
+ before_dump( value )
211
+ if @endianess == :big
212
+ @raw_value = @value.pack('g*').unpack('C*')
213
+ else
214
+ @raw_value = @value.pack('e*').unpack('C*')
215
+ end
216
+ end
217
+
218
+ def load(raw_value)
219
+ if @endianess == :big
220
+ self.value = check_raw_value(raw_value).pack('C*').unpack('g*') if !value.nil?
221
+ else
222
+ self.value = check_raw_value(raw_value).pack('C*').unpack('e*') if !value.nil?
223
+ end
224
+ after_load
225
+ end
226
+ end
227
+
228
+ class Float64 < BaseType
229
+ def initialize(options = {})
230
+ super options.merge :bit_length => 64, :sign => nil, :default_value => 0.0
231
+ end
232
+
233
+ def dump(value=nil)
234
+ before_dump( value )
235
+ if @endianess == :big
236
+ @raw_value = @value.pack('G*').unpack('C*')
237
+ else
238
+ @raw_value = @value.pack('E*').unpack('C*')
239
+ end
240
+ end
241
+
242
+ def load(raw_value)
243
+ if @endianess == :big
244
+ self.value = check_raw_value(raw_value).pack('C*').unpack('G*') if !value.nil?
245
+ else
246
+ self.value = check_raw_value(raw_value).pack('C*').unpack('E*') if !value.nil?
247
+ end
248
+ after_load
249
+ end
250
+ end
187
251
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveModelSerializersBinary
2
- VERSION = "0.1.5"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -5,12 +5,12 @@
5
5
  # id :integer not null, primary key
6
6
  # uid :integer
7
7
  # silo :integer
8
- # nombre :string
8
+ # nombre :string(255)
9
9
  # total_acumulado :integer
10
10
  # bits1 :boolean
11
11
  # bits2 :boolean
12
12
  # ffloat :float
13
- # variable :string
13
+ # variable :string(255)
14
14
  # created_at :datetime
15
15
  # updated_at :datetime
16
16
  #
@@ -18,6 +18,30 @@
18
18
  class Producto < ActiveRecord::Base
19
19
  include ActiveModel::Serializers::Binary
20
20
 
21
+ int16 :uid
22
+ int16 :silo
21
23
  char :nombre, count: 1, length: 20
24
+ int32 :total_acumulado
25
+ bool :bits1
26
+ bool :bits2
27
+ bool :bits3, virtual: true
28
+ bool :bits4, virtual: true
29
+ bool :bits5, virtual: true
30
+ bool :bits6, virtual: true
31
+ bool :bits7, virtual: true
32
+ bool :bits8, virtual: true
33
+ bool :bits9, virtual: true
34
+ bool :bits10, virtual: true
35
+ bool :bits11, virtual: true
36
+ bool :bits12, virtual: true
37
+ bool :bits13, virtual: true
38
+ bool :bits14, virtual: true
39
+ bool :bits15, virtual: true
40
+ bool :bits16, virtual: true
41
+ float32 :ffloat
42
+ char :variable, count: 1, length: 20 do |field, mode|
43
+ puts (mode.to_s + ': variable block').blue
44
+ end
45
+ int32 :test, count: 10, virtual: true # No existe en la DB
22
46
  nest :tipo, coder: Tipo
23
47
  end
@@ -8,8 +8,9 @@ class String
8
8
  end
9
9
 
10
10
  orig = Producto.new
11
+ orig.silo = 0xFF7F
11
12
  orig.nombre = "AAAAAAAAAAAAAAAAAAAA"
12
- orig.tipo = Tipo.new({name:"BBBBBBBBBBBBBBBBBBBB", producto_id: 0x55AA})
13
+ orig.tipo = Tipo.new({name:"BBBBBBBBBBBBBBBBBBBB", producto_id: 0xFF7F})
13
14
 
14
15
  puts 'Datos originales...'
15
16
  puts orig.inspect.green
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_model_serializers_binary
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ByS Sistemas de Control