active_model_serializers_binary 0.0.13 → 0.0.24

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2f3830d0d3a2a18ae95bb47c427e1af2ea6d2d86
4
- data.tar.gz: 546aff445bb2ec63c9734955c762f3a87029b4b3
3
+ metadata.gz: 0a481a565f5e643a79745f877c5b5ee262e44f86
4
+ data.tar.gz: ad2610cd3717c694f67ba9911579b4f0a47e87b7
5
5
  SHA512:
6
- metadata.gz: 92274b86db2b91c5085e29e659279428d0221b1bd3bcfd4c9f650de4d32db55800887e22919261fea282960ac87efed344844da28a3955386848ece83e2c4509
7
- data.tar.gz: 272721accba6bd385c4b1c967afd42af2a155e132c4c807d69752bb4673e05cbb6d72da8f64cf0f3767cbbfc0e5ea9797c12db564d655fed3d29436b944976de
6
+ metadata.gz: 98c03ce80bc8c768195b236ca904185e2e408a6c4b905e22228798482ff67607fad999d313514d25c9a94f15aa62ca12ffe269ea628c50e09317c3c2ca6783c0
7
+ data.tar.gz: 3a8d735b73f56e7c4eadbdf35714608987baaedb3e29a610d6bfa2e98b0a1c19b9ed8f7f00dbe60c3760774e01ad8cbac817df3308325fb1f9a0605981e83af7
data/Gemfile CHANGED
@@ -21,4 +21,5 @@ group :development do
21
21
 
22
22
  #debuggers
23
23
  gem 'pry-byebug'
24
+ gem 'annotate'
24
25
  end
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- active_model_serializers_binary (0.0.12)
4
+ active_model_serializers_binary (0.0.24)
5
5
  activemodel (~> 4.1)
6
6
 
7
7
  GEM
@@ -33,6 +33,9 @@ GEM
33
33
  minitest (~> 5.1)
34
34
  thread_safe (~> 0.1)
35
35
  tzinfo (~> 1.1)
36
+ annotate (2.6.5)
37
+ activerecord (>= 2.3.0)
38
+ rake (>= 0.8.7)
36
39
  arel (5.0.1.20140414130214)
37
40
  bcrypt (3.1.7)
38
41
  builder (3.2.2)
@@ -155,6 +158,7 @@ PLATFORMS
155
158
 
156
159
  DEPENDENCIES
157
160
  active_model_serializers_binary!
161
+ annotate
158
162
  bundler (~> 1.6)
159
163
  coffee-rails (~> 4.0)
160
164
  colorize (~> 0.7)
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
11
11
  s.email = ["info@bys-control.com.ar"]
12
12
  s.homepage = "https://github.com/bys-control/active_model_serializers_binary"
13
13
  s.summary = "Serialize models to/from binary format for raw data exchange"
14
- s.description = "Binary serializer for Ruby Active Model"
14
+ s.description = "active_model_serializers_binary is a declarative way to serialize/deserialize ActiveModel classes for raw data exchange."
15
15
  s.license = "MIT"
16
16
 
17
17
  s.files = `git ls-files -z`.split("\x0")
@@ -16,58 +16,58 @@ module ActiveModel
16
16
  class_attribute :attr_config
17
17
  self.attr_config = {}
18
18
 
19
- def initialize
20
- super
21
- self.attr_config.each do |key, options|
22
- options[:parent] = self
23
- end
19
+ def attributes
20
+ keys = self.attr_config.select{|k, v| v[:virtual]==true}.keys
21
+ values = keys.map{ |var| self.instance_variable_get("@#{var}") }
22
+ super.merge(Hash[keys.zip values])
24
23
  end
25
24
  end
26
25
 
27
26
  module ClassMethods
28
27
  # todo: agrupar parametros en hash (rompe la compatibilidad hacia atras)
29
- def serialize_options(attr_name, coder, count=1, length=1, &block )
30
- self.attr_config.merge!(attr_name.to_s => {:coder => coder, :count => count, :length => length, :block => block, :name => attr_name})
28
+ def serialize_options(attr_name, coder, count=1, length=1, virtual=false, &block )
29
+ attr_accessor(attr_name) if virtual
30
+ self.attr_config.merge!(attr_name.to_s => {:coder => coder, :count => count, :length => length, :block => block, :name => attr_name, :virtual => virtual})
31
31
  end
32
32
 
33
33
  def int8( attr_name, options = {}, &block )
34
- serialize_options attr_name, DataTypes::Int8, options[:count], options[:length], &block
34
+ serialize_options attr_name, DataTypes::Int8, options[:count], options[:length], options[:virtual], &block
35
35
  end
36
36
 
37
37
  def int16( attr_name, options = {}, &block )
38
- serialize_options attr_name, DataTypes::Int16, options[:count], options[:length], &block
38
+ serialize_options attr_name, DataTypes::Int16, options[:count], options[:length], options[:virtual], &block
39
39
  end
40
40
 
41
41
  def int32( attr_name, options = {}, &block )
42
- serialize_options attr_name, DataTypes::Int32, options[:count], options[:length], &block
42
+ serialize_options attr_name, DataTypes::Int32, options[:count], options[:length], options[:virtual], &block
43
43
  end
44
44
 
45
45
  def uint8( attr_name, options = {}, &block )
46
- serialize_options attr_name, DataTypes::UInt8, options[:count], options[:length], &block
46
+ serialize_options attr_name, DataTypes::UInt8, options[:count], options[:length], options[:virtual], &block
47
47
  end
48
48
 
49
49
  def uint16( attr_name, options = {}, &block )
50
- serialize_options attr_name, DataTypes::UInt16, options[:count], options[:length], &block
50
+ serialize_options attr_name, DataTypes::UInt16, options[:count], options[:length], options[:virtual], &block
51
51
  end
52
52
 
53
53
  def uint32( attr_name, options = {}, &block )
54
- serialize_options attr_name, DataTypes::UInt32, options[:count], options[:length], &block
54
+ serialize_options attr_name, DataTypes::UInt32, options[:count], options[:length], options[:virtual], &block
55
55
  end
56
56
 
57
57
  def bitfield( attr_name, options = {}, &block )
58
- serialize_options attr_name, DataTypes::BitField, options[:count], options[:length], &block
58
+ serialize_options attr_name, DataTypes::BitField, options[:count], options[:length], options[:virtual], &block
59
59
  end
60
60
 
61
61
  def float32( attr_name, options = {}, &block )
62
- serialize_options attr_name, DataTypes::Float32, options[:count], options[:length], &block
62
+ serialize_options attr_name, DataTypes::Float32, options[:count], options[:length], options[:virtual], &block
63
63
  end
64
64
 
65
65
  def char( attr_name, options = {}, &block )
66
- serialize_options attr_name, DataTypes::Char, options[:count], options[:length], &block
66
+ serialize_options attr_name, DataTypes::Char, options[:count], options[:length], options[:virtual], &block
67
67
  end
68
68
 
69
69
  def bool( attr_name, options = {}, &block )
70
- serialize_options attr_name, DataTypes::Bool, options[:count], options[:length], &block
70
+ serialize_options attr_name, DataTypes::Bool, options[:count], options[:length], options[:virtual], &block
71
71
  end
72
72
  end
73
73
 
@@ -88,7 +88,7 @@ module ActiveModel
88
88
  current_address = start_address*2 + 0.0 # Dirección en bytes
89
89
 
90
90
  @serializable.attr_config.each do |key, options|
91
- var = options[:coder].new(options)
91
+ var = options[:coder].new(options.merge(parent: @serializable))
92
92
  # Busca el valor del atributo y si no existe devuelve nil
93
93
  var.value = serializable_values[key] rescue nil
94
94
 
@@ -96,7 +96,6 @@ module ActiveModel
96
96
  bit = (current_address.modulo(1)*8).round
97
97
 
98
98
  tmp_buffer = var.dump
99
-
100
99
  if @options[:align]
101
100
  if !var.type.in? [:bitfield, :bool]
102
101
  # Se posiciona al principio de un byte
@@ -147,11 +146,10 @@ module ActiveModel
147
146
  current_address = start_address*2 + 0.0 # Dirección en bytes
148
147
 
149
148
  @serializable.attr_config.each do |key, options|
150
- #puts "#{key} - #{options}"
151
149
  byte = current_address.floor
152
150
  bit = (current_address.modulo(1)*8).round
153
151
 
154
- var = options[:coder].new(options) #creo objeto del tipo de dato pasado
152
+ var = options[:coder].new(options.merge(parent: @serializable)) #creo objeto del tipo de dato pasado
155
153
 
156
154
  if @options[:align]
157
155
  if !var.type.in? [:bitfield, :bool]
@@ -181,22 +179,18 @@ module ActiveModel
181
179
  tmp_buffer = buffer.slice(byte, (var.size+bit/8.0).ceil)
182
180
  result_deserialized=var.load([tmp_buffer.pack('C*').unpack('b*').first.slice(bit,var.size*8)].pack('b*').unpack('C*'))
183
181
  end
184
- # puts result_deserialized.inspect
185
182
  serialized_values["#{key}"] = result_deserialized.count>1 ? result_deserialized : result_deserialized.first
186
183
  current_address = (byte+bit/8.0)+var.size
187
184
  end
188
185
 
189
- # if !@serializable.instance_variable_get(:@attributes).nil?
190
- # @serializable.instance_variable_get(:@attributes).merge!(serialized_values) rescue nil
191
- # else
192
- serialized_values.each do |k,v|
193
- if @serializable.respond_to? "#{k}="
194
- @serializable.send("#{k}=", v)
195
- else
196
- @serializable.instance_variable_set("@#{k}".to_sym, v)
197
- end
186
+ # Asigno los datos leidos
187
+ serialized_values.each do |k,v|
188
+ if @serializable.respond_to? "#{k}="
189
+ @serializable.send("#{k}=", v)
190
+ else
191
+ @serializable.instance_variable_set("@#{k}".to_sym, v)
198
192
  end
199
- #end
193
+ end
200
194
  @serializable
201
195
  end
202
196
 
@@ -221,6 +215,12 @@ module ActiveModel
221
215
  Serializer.new(self, options).dump
222
216
  end
223
217
 
218
+ def to_words(options = {}, &block)
219
+ data = to_bytes(options, &block)
220
+ byte_count = (data.count/2.0).ceil*2
221
+ data.fill(0, data.count...byte_count).pack('C*').unpack('v*')
222
+ end
223
+
224
224
  # Sets the model +attributes+ from an Binary string. Returns +self+.
225
225
  #
226
226
  # class Person
@@ -269,6 +269,12 @@ module ActiveModel
269
269
  end
270
270
  retVal
271
271
  end
272
+
273
+ def from_words(buffer, options = {}, &block)
274
+ data = buffer.pack('v*').unpack('C*')
275
+ from_bytes(data, options, &block)
276
+ end
277
+
272
278
  end
273
279
  end
274
280
  end
@@ -5,7 +5,7 @@ module DataTypes
5
5
  attr_accessor :raw_value, :bit_length, :type, :sign, :count, :length, :value, :name, :parent
6
6
 
7
7
  def initialize(options = {})
8
- @default_value = options[:default_value] || 0
8
+ @default_value = options[:default_value].nil? ? 0 : options[:default_value]
9
9
  @raw_value = nil
10
10
  @bit_length = options[:bit_length] # Cantidad de bits del tipo de dato
11
11
  @type = type
@@ -28,7 +28,7 @@ module DataTypes
28
28
 
29
29
  # Return size of object in bytes
30
30
  def size
31
- ((@bit_length*@length*@count)/8.0).ceil
31
+ (@bit_length*@length*@count)/8.0
32
32
  end
33
33
 
34
34
  def check( value, options = {} )
@@ -52,7 +52,7 @@ module DataTypes
52
52
  when :char
53
53
  v.to_s[0...length]
54
54
  when :bool
55
- (v.in? [0, false]) ? false : true
55
+ (v.in? [1, true]) ? true : false
56
56
  else
57
57
  v.to_i
58
58
  end
@@ -115,8 +115,8 @@ module DataTypes
115
115
  self.value = value if !value.nil?
116
116
  if !@block.nil?
117
117
  value = @parent.instance_exec( self, :dump, &@block )
118
- self.value = value if !value.nil?
119
118
  end
119
+ self.value = value if !value.nil?
120
120
  end
121
121
 
122
122
  #
@@ -127,8 +127,7 @@ module DataTypes
127
127
  #
128
128
  def after_load
129
129
  if !@block.nil?
130
- value = @parent.instance_exec( self, :load, &@block )
131
- self.value = value if !value.nil?
130
+ @parent.instance_exec( self, :load, &@block )
132
131
  end
133
132
  @value
134
133
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveModelSerializersBinary
2
- VERSION = "0.0.13"
2
+ VERSION = "0.0.24"
3
3
  end
@@ -0,0 +1,46 @@
1
+ # == Schema Information
2
+ #
3
+ # Table name: productos
4
+ #
5
+ # id :integer not null, primary key
6
+ # uid :integer
7
+ # silo :integer
8
+ # nombre :string(255)
9
+ # total_acumulado :integer
10
+ # bits1 :boolean
11
+ # bits2 :boolean
12
+ # ffloat :float
13
+ # variable :string(255)
14
+ # created_at :datetime
15
+ # updated_at :datetime
16
+ #
17
+
18
+ class Producto < ActiveRecord::Base
19
+ include ActiveModel::Serializers::Binary
20
+
21
+ int16 :uid
22
+ int16 :silo
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
46
+ end
@@ -0,0 +1,16 @@
1
+ class CreateProductos < ActiveRecord::Migration
2
+ def change
3
+ create_table :productos do |t|
4
+ t.integer :uid
5
+ t.integer :silo
6
+ t.string :nombre
7
+ t.integer :total_acumulado
8
+ t.boolean :bits1
9
+ t.boolean :bits2
10
+ t.float :ffloat
11
+ t.string :variable
12
+
13
+ t.timestamps
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: UTF-8
2
+ # This file is auto-generated from the current state of the database. Instead
3
+ # of editing this file, please use the migrations feature of Active Record to
4
+ # incrementally modify your database, and then regenerate this schema definition.
5
+ #
6
+ # Note that this schema.rb definition is the authoritative source for your
7
+ # database schema. If you need to create the application database on another
8
+ # system, you should be using db:schema:load, not running all the migrations
9
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
11
+ #
12
+ # It's strongly recommended that you check this file into your version control system.
13
+
14
+ ActiveRecord::Schema.define(version: 20160608024807) do
15
+
16
+ create_table "productos", force: true do |t|
17
+ t.integer "uid"
18
+ t.integer "silo"
19
+ t.string "nombre"
20
+ t.integer "total_acumulado"
21
+ t.boolean "bits1"
22
+ t.boolean "bits2"
23
+ t.float "ffloat"
24
+ t.string "variable"
25
+ t.datetime "created_at"
26
+ t.datetime "updated_at"
27
+ end
28
+
29
+ end
@@ -7,54 +7,30 @@ class String
7
7
  end
8
8
  end
9
9
 
10
- class Producto
11
- include ActiveModel::Serializers::Binary
12
-
13
- attr_accessor :start_address, :id, :silo, :nombre, :total_acumulado, :bits1, :bits2, :float, :total_acumulado_1, :variable
14
-
15
- def attributes
16
- instance_values
17
- end
18
-
19
- def attributes=(hash)
20
- hash.each do |key, value|
21
- instance_variable_set("@#{key}", value)
22
- end
23
- end
24
-
25
- # def metodo
26
- # self.instance_variable_get("@variable")
27
- # end
28
-
29
- # def metodo= (value)
30
- # self.instance_variable_set("@variable", value)
31
- # end
32
-
33
- int16 :id
34
- int16 :silo
35
- char :nombre, count: 1, length: 20
36
- int32 :total_acumulado
37
- bool :bits1
38
- bool :bits2
39
- int32 :total_acumulado_1
40
- float32 :float
41
- char :variable, count: 1, length: 20 do |field, mode|
42
- puts (mode.to_s + ': variable block').blue
43
- end
44
- end
45
-
46
-
47
10
  orig = Producto.new
48
- orig.start_address = 0
49
- orig.id = 1
11
+ orig.uid = 1
50
12
  orig.silo = 0
51
13
  orig.nombre = "MAIZ"
52
14
  orig.total_acumulado = 50
53
15
  orig.bits1 = 1
54
- orig.bits2 = 1
55
- orig.total_acumulado_1 = 20
56
- orig.float= 1.2345678
16
+ orig.bits2 = 0
17
+ orig.bits3 = nil
18
+ orig.bits4 = nil
19
+ orig.bits5 = nil
20
+ orig.bits6 = nil
21
+ orig.bits7 = nil
22
+ orig.bits8 = nil
23
+ orig.bits9 = nil
24
+ orig.bits10 = nil
25
+ orig.bits11 = nil
26
+ orig.bits12 = nil
27
+ orig.bits13 = nil
28
+ orig.bits14 = nil
29
+ orig.bits15 = nil
30
+ orig.bits16 = nil
31
+ orig.ffloat= 1.2345678
57
32
  orig.variable = '012345678901234567890123456789'
33
+ orig.test = (1..10).to_a
58
34
 
59
35
  puts 'Datos originales...'
60
36
  puts orig.inspect.green
@@ -0,0 +1,38 @@
1
+ # == Schema Information
2
+ #
3
+ # Table name: productos
4
+ #
5
+ # id :integer not null, primary key
6
+ # uid :integer
7
+ # silo :integer
8
+ # nombre :string(255)
9
+ # total_acumulado :integer
10
+ # bits1 :boolean
11
+ # bits2 :boolean
12
+ # ffloat :float
13
+ # variable :string(255)
14
+ # created_at :datetime
15
+ # updated_at :datetime
16
+ #
17
+
18
+ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
19
+
20
+ one:
21
+ id: 1
22
+ silo: 1
23
+ nombre: MyString
24
+ total_acumulado: 1
25
+ bits1: false
26
+ bits2: false
27
+ ffloat: 1.5
28
+ variable: MyString
29
+
30
+ two:
31
+ id: 1
32
+ silo: 1
33
+ nombre: MyString
34
+ total_acumulado: 1
35
+ bits1: false
36
+ bits2: false
37
+ ffloat: 1.5
38
+ variable: MyString
@@ -0,0 +1,24 @@
1
+ # == Schema Information
2
+ #
3
+ # Table name: productos
4
+ #
5
+ # id :integer not null, primary key
6
+ # uid :integer
7
+ # silo :integer
8
+ # nombre :string(255)
9
+ # total_acumulado :integer
10
+ # bits1 :boolean
11
+ # bits2 :boolean
12
+ # ffloat :float
13
+ # variable :string(255)
14
+ # created_at :datetime
15
+ # updated_at :datetime
16
+ #
17
+
18
+ require 'test_helper'
19
+
20
+ class ProductoTest < ActiveSupport::TestCase
21
+ # test "the truth" do
22
+ # assert true
23
+ # end
24
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_model_serializers_binary
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.0.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - ByS Sistemas de Control
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-13 00:00:00.000000000 Z
11
+ date: 2016-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -220,7 +220,8 @@ dependencies:
220
220
  - - "~>"
221
221
  - !ruby/object:Gem::Version
222
222
  version: '4.1'
223
- description: Binary serializer for Ruby Active Model
223
+ description: active_model_serializers_binary is a declarative way to serialize/deserialize
224
+ ActiveModel classes for raw data exchange.
224
225
  email:
225
226
  - info@bys-control.com.ar
226
227
  executables: []
@@ -253,6 +254,7 @@ files:
253
254
  - test/dummy/app/mailers/.keep
254
255
  - test/dummy/app/models/.keep
255
256
  - test/dummy/app/models/concerns/.keep
257
+ - test/dummy/app/models/producto.rb
256
258
  - test/dummy/app/views/layouts/application.html.erb
257
259
  - test/dummy/bin/bundle
258
260
  - test/dummy/bin/rails
@@ -276,6 +278,8 @@ files:
276
278
  - test/dummy/config/locales/en.yml
277
279
  - test/dummy/config/routes.rb
278
280
  - test/dummy/config/secrets.yml
281
+ - test/dummy/db/migrate/20160608024807_create_productos.rb
282
+ - test/dummy/db/schema.rb
279
283
  - test/dummy/lib/assets/.keep
280
284
  - test/dummy/lib/test_parser.rb
281
285
  - test/dummy/log/.keep
@@ -283,6 +287,8 @@ files:
283
287
  - test/dummy/public/422.html
284
288
  - test/dummy/public/500.html
285
289
  - test/dummy/public/favicon.ico
290
+ - test/dummy/test/fixtures/productos.yml
291
+ - test/dummy/test/models/producto_test.rb
286
292
  - test/test_helper.rb
287
293
  homepage: https://github.com/bys-control/active_model_serializers_binary
288
294
  licenses:
@@ -321,6 +327,7 @@ test_files:
321
327
  - test/dummy/app/mailers/.keep
322
328
  - test/dummy/app/models/.keep
323
329
  - test/dummy/app/models/concerns/.keep
330
+ - test/dummy/app/models/producto.rb
324
331
  - test/dummy/app/views/layouts/application.html.erb
325
332
  - test/dummy/bin/bundle
326
333
  - test/dummy/bin/rails
@@ -344,6 +351,8 @@ test_files:
344
351
  - test/dummy/config/locales/en.yml
345
352
  - test/dummy/config/routes.rb
346
353
  - test/dummy/config/secrets.yml
354
+ - test/dummy/db/migrate/20160608024807_create_productos.rb
355
+ - test/dummy/db/schema.rb
347
356
  - test/dummy/lib/assets/.keep
348
357
  - test/dummy/lib/test_parser.rb
349
358
  - test/dummy/log/.keep
@@ -351,5 +360,7 @@ test_files:
351
360
  - test/dummy/public/422.html
352
361
  - test/dummy/public/500.html
353
362
  - test/dummy/public/favicon.ico
363
+ - test/dummy/test/fixtures/productos.yml
364
+ - test/dummy/test/models/producto_test.rb
354
365
  - test/test_helper.rb
355
366
  has_rdoc: