active_model_serializers_binary 0.0.10
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 +7 -0
- data/.gitignore +8 -0
- data/Gemfile +24 -0
- data/Gemfile.lock +179 -0
- data/MIT-LICENSE +20 -0
- data/README.md +17 -0
- data/README.rdoc +3 -0
- data/Rakefile +32 -0
- data/active_model_serializers_binary.gemspec +39 -0
- data/lib/active_model_serializers_binary/active_model_serializers_binary.rb +217 -0
- data/lib/active_model_serializers_binary/data_types.rb +218 -0
- data/lib/active_model_serializers_binary/version.rb +3 -0
- data/lib/active_model_serializers_binary.rb +1 -0
- data/lib/tasks/active_model_serializers_binary_tasks.rake +4 -0
- data/test/active_model_serializers_binary_test.rb +7 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/images/.keep +0 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/controllers/concerns/.keep +0 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/mailers/.keep +0 -0
- data/test/dummy/app/models/.keep +0 -0
- data/test/dummy/app/models/concerns/.keep +0 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/config/application.rb +23 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +37 -0
- data/test/dummy/config/environments/production.rb +82 -0
- data/test/dummy/config/environments/test.rb +39 -0
- data/test/dummy/config/initializers/assets.rb +8 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +56 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/lib/assets/.keep +0 -0
- data/test/dummy/lib/test_parser.rb +72 -0
- data/test/dummy/log/.keep +0 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/test_helper.rb +15 -0
- metadata +354 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c6b15a22af56d99e5309303d942972061b28f5f7
|
4
|
+
data.tar.gz: 34fe1c2c45fec1ab4e4d02bba3c4a7a3fb3d9ed4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ba001a7dc0310b82190fcb392dfe2d8748139bc5753705e1f136fb1eeec5c1ba8a96b3bf70ea776bfdb623025f9a14c71641a25b7957618d1c3200f285bfe0b3
|
7
|
+
data.tar.gz: 0ca34aff960f7de5470af3c63bd1ac0a98af8b0e141c33bcb8764421f9e182094e580efc3e83644b51eac749c7dd4980d5fbc04f189abafc7ea13b511a17a68f
|
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
# Declare your gem's dependencies in active_model_serializers_binary.gemspec.
|
4
|
+
# Bundler will treat runtime dependencies like base dependencies, and
|
5
|
+
# development dependencies will be added by default to the :development group.
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
# Declare any dependencies that are still in development here instead of in
|
9
|
+
# your gemspec. These might include edge Rails or gems from your path or
|
10
|
+
# Git. Remember to move these dependencies to your gemspec before releasing
|
11
|
+
# your gem to rubygems.org.
|
12
|
+
|
13
|
+
# To use debugger
|
14
|
+
# gem 'debugger'
|
15
|
+
|
16
|
+
group :development do
|
17
|
+
gem 'pry'
|
18
|
+
gem 'pry-rescue'
|
19
|
+
gem 'pry-doc'
|
20
|
+
gem 'pry-rails'
|
21
|
+
|
22
|
+
#debuggers
|
23
|
+
gem 'pry-byebug'
|
24
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,179 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
active_model_serializers_binary (0.0.9)
|
5
|
+
activemodel (~> 4.1)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actionmailer (4.1.4)
|
11
|
+
actionpack (= 4.1.4)
|
12
|
+
actionview (= 4.1.4)
|
13
|
+
mail (~> 2.5.4)
|
14
|
+
actionpack (4.1.4)
|
15
|
+
actionview (= 4.1.4)
|
16
|
+
activesupport (= 4.1.4)
|
17
|
+
rack (~> 1.5.2)
|
18
|
+
rack-test (~> 0.6.2)
|
19
|
+
actionview (4.1.4)
|
20
|
+
activesupport (= 4.1.4)
|
21
|
+
builder (~> 3.1)
|
22
|
+
erubis (~> 2.7.0)
|
23
|
+
activemodel (4.1.4)
|
24
|
+
activesupport (= 4.1.4)
|
25
|
+
builder (~> 3.1)
|
26
|
+
activerecord (4.1.4)
|
27
|
+
activemodel (= 4.1.4)
|
28
|
+
activesupport (= 4.1.4)
|
29
|
+
arel (~> 5.0.0)
|
30
|
+
activesupport (4.1.4)
|
31
|
+
i18n (~> 0.6, >= 0.6.9)
|
32
|
+
json (~> 1.7, >= 1.7.7)
|
33
|
+
minitest (~> 5.1)
|
34
|
+
thread_safe (~> 0.1)
|
35
|
+
tzinfo (~> 1.1)
|
36
|
+
arel (5.0.1.20140414130214)
|
37
|
+
bcrypt (3.1.7)
|
38
|
+
builder (3.2.2)
|
39
|
+
byebug (4.0.5)
|
40
|
+
columnize (= 0.9.0)
|
41
|
+
coderay (1.1.0)
|
42
|
+
coffee-rails (4.0.1)
|
43
|
+
coffee-script (>= 2.2.0)
|
44
|
+
railties (>= 4.0.0, < 5.0)
|
45
|
+
coffee-script (2.3.0)
|
46
|
+
coffee-script-source
|
47
|
+
execjs
|
48
|
+
coffee-script-source (1.7.1)
|
49
|
+
colorize (0.7.3)
|
50
|
+
columnize (0.9.0)
|
51
|
+
devise (3.2.4)
|
52
|
+
bcrypt (~> 3.0)
|
53
|
+
orm_adapter (~> 0.1)
|
54
|
+
railties (>= 3.2.6, < 5)
|
55
|
+
thread_safe (~> 0.1)
|
56
|
+
warden (~> 1.2.3)
|
57
|
+
erubis (2.7.0)
|
58
|
+
execjs (2.2.1)
|
59
|
+
hike (1.2.3)
|
60
|
+
i18n (0.6.11)
|
61
|
+
interception (0.5)
|
62
|
+
jbuilder (1.5.3)
|
63
|
+
activesupport (>= 3.0.0)
|
64
|
+
multi_json (>= 1.2.0)
|
65
|
+
jquery-ui-rails (4.2.1)
|
66
|
+
railties (>= 3.2.16)
|
67
|
+
jquery-validation-rails (1.12.0)
|
68
|
+
railties (>= 3.2, < 5.0)
|
69
|
+
thor (~> 0.14)
|
70
|
+
json (1.8.3)
|
71
|
+
libv8 (3.16.14.3)
|
72
|
+
mail (2.5.4)
|
73
|
+
mime-types (~> 1.16)
|
74
|
+
treetop (~> 1.4.8)
|
75
|
+
method_source (0.8.2)
|
76
|
+
mime-types (1.25.1)
|
77
|
+
minitest (5.4.0)
|
78
|
+
multi_json (1.10.1)
|
79
|
+
orm_adapter (0.5.0)
|
80
|
+
polyglot (0.3.5)
|
81
|
+
pry (0.10.1)
|
82
|
+
coderay (~> 1.1.0)
|
83
|
+
method_source (~> 0.8.1)
|
84
|
+
slop (~> 3.4)
|
85
|
+
pry-byebug (3.1.0)
|
86
|
+
byebug (~> 4.0)
|
87
|
+
pry (~> 0.10)
|
88
|
+
pry-doc (0.6.0)
|
89
|
+
pry (~> 0.9)
|
90
|
+
yard (~> 0.8)
|
91
|
+
pry-rails (0.3.4)
|
92
|
+
pry (>= 0.9.10)
|
93
|
+
pry-rescue (1.4.2)
|
94
|
+
interception (>= 0.5)
|
95
|
+
pry
|
96
|
+
rack (1.5.2)
|
97
|
+
rack-test (0.6.2)
|
98
|
+
rack (>= 1.0)
|
99
|
+
rails (4.1.4)
|
100
|
+
actionmailer (= 4.1.4)
|
101
|
+
actionpack (= 4.1.4)
|
102
|
+
actionview (= 4.1.4)
|
103
|
+
activemodel (= 4.1.4)
|
104
|
+
activerecord (= 4.1.4)
|
105
|
+
activesupport (= 4.1.4)
|
106
|
+
bundler (>= 1.3.0, < 2.0)
|
107
|
+
railties (= 4.1.4)
|
108
|
+
sprockets-rails (~> 2.0)
|
109
|
+
railties (4.1.4)
|
110
|
+
actionpack (= 4.1.4)
|
111
|
+
activesupport (= 4.1.4)
|
112
|
+
rake (>= 0.8.7)
|
113
|
+
thor (>= 0.18.1, < 2.0)
|
114
|
+
rake (10.3.2)
|
115
|
+
ref (1.0.5)
|
116
|
+
sass (3.2.19)
|
117
|
+
sass-rails (4.0.3)
|
118
|
+
railties (>= 4.0.0, < 5.0)
|
119
|
+
sass (~> 3.2.0)
|
120
|
+
sprockets (~> 2.8, <= 2.11.0)
|
121
|
+
sprockets-rails (~> 2.0)
|
122
|
+
slop (3.6.0)
|
123
|
+
sprockets (2.11.0)
|
124
|
+
hike (~> 1.2)
|
125
|
+
multi_json (~> 1.0)
|
126
|
+
rack (~> 1.0)
|
127
|
+
tilt (~> 1.1, != 1.3.0)
|
128
|
+
sprockets-rails (2.1.3)
|
129
|
+
actionpack (>= 3.0)
|
130
|
+
activesupport (>= 3.0)
|
131
|
+
sprockets (~> 2.8)
|
132
|
+
sqlite3 (1.3.9)
|
133
|
+
therubyracer (0.12.1)
|
134
|
+
libv8 (~> 3.16.14.0)
|
135
|
+
ref
|
136
|
+
thor (0.19.1)
|
137
|
+
thread_safe (0.3.4)
|
138
|
+
tilt (1.4.1)
|
139
|
+
treetop (1.4.15)
|
140
|
+
polyglot
|
141
|
+
polyglot (>= 0.3.1)
|
142
|
+
turbolinks (2.2.2)
|
143
|
+
coffee-rails
|
144
|
+
tzinfo (1.2.1)
|
145
|
+
thread_safe (~> 0.1)
|
146
|
+
uglifier (1.3.0)
|
147
|
+
execjs (>= 0.3.0)
|
148
|
+
multi_json (~> 1.0, >= 1.0.2)
|
149
|
+
warden (1.2.3)
|
150
|
+
rack (>= 1.0)
|
151
|
+
yard (0.8.7.6)
|
152
|
+
|
153
|
+
PLATFORMS
|
154
|
+
ruby
|
155
|
+
|
156
|
+
DEPENDENCIES
|
157
|
+
active_model_serializers_binary!
|
158
|
+
bundler (~> 1.6)
|
159
|
+
coffee-rails (~> 4.0)
|
160
|
+
colorize (~> 0.7)
|
161
|
+
devise (~> 3.2)
|
162
|
+
jbuilder (~> 1.2)
|
163
|
+
jquery-ui-rails (~> 4.2)
|
164
|
+
jquery-validation-rails (~> 1.12)
|
165
|
+
pry
|
166
|
+
pry-byebug
|
167
|
+
pry-doc
|
168
|
+
pry-rails
|
169
|
+
pry-rescue
|
170
|
+
rails (~> 4.1)
|
171
|
+
rake (~> 10.3)
|
172
|
+
sass-rails (~> 4.0)
|
173
|
+
sqlite3 (~> 1.3)
|
174
|
+
therubyracer (~> 0.12)
|
175
|
+
turbolinks (~> 2.2)
|
176
|
+
uglifier (~> 1.3)
|
177
|
+
|
178
|
+
BUNDLED WITH
|
179
|
+
1.11.2
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2014 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# README #
|
2
|
+
|
3
|
+
Serializador/Deserializador de bytes en Ruby
|
4
|
+
|
5
|
+
* La Serialización se realiza a partir de una instancia de un objeto para el cual se declaran los tipos para cada uno de los atributos.
|
6
|
+
|
7
|
+
* La deserialización convierte un array de bytes al objeto definido.
|
8
|
+
|
9
|
+
### Ejemplos de uso ###
|
10
|
+
|
11
|
+
|
12
|
+
```
|
13
|
+
#!ruby
|
14
|
+
|
15
|
+
test/dummy/lib/test_parser.rb
|
16
|
+
|
17
|
+
```
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'ActiveModelSerializersBinary'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
Bundler::GemHelper.install_tasks
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
|
24
|
+
Rake::TestTask.new(:test) do |t|
|
25
|
+
t.libs << 'lib'
|
26
|
+
t.libs << 'test'
|
27
|
+
t.pattern = 'test/**/*_test.rb'
|
28
|
+
t.verbose = false
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
task default: :test
|
@@ -0,0 +1,39 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
|
3
|
+
# Maintain your gem's version:
|
4
|
+
require "active_model_serializers_binary/version"
|
5
|
+
|
6
|
+
# Describe your gem and declare its dependencies:
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "active_model_serializers_binary"
|
9
|
+
s.version = ActiveModelSerializersBinary::VERSION
|
10
|
+
s.authors = ["ByS Sistemas de Control"]
|
11
|
+
s.email = ["info@bys-control.com.ar"]
|
12
|
+
s.homepage = "https://github.com/bys-control/active_model_serializers_binary"
|
13
|
+
s.summary = "Serialize models to/from binary format for raw data exchange"
|
14
|
+
s.description = "Binary serializer for Ruby Active Model"
|
15
|
+
s.license = "MIT"
|
16
|
+
|
17
|
+
s.files = `git ls-files -z`.split("\x0")
|
18
|
+
s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_development_dependency "bundler", "~> 1.6"
|
23
|
+
s.add_development_dependency "rake", "~> 10.3"
|
24
|
+
s.add_development_dependency "sqlite3", "~> 1.3"
|
25
|
+
s.add_development_dependency "rails", "~> 4.1"
|
26
|
+
|
27
|
+
s.add_development_dependency 'devise', '~> 3.2'
|
28
|
+
s.add_development_dependency 'jquery-ui-rails', '~> 4.2'
|
29
|
+
s.add_development_dependency 'sass-rails', '~> 4.0'
|
30
|
+
s.add_development_dependency 'uglifier', '~> 1.3'
|
31
|
+
s.add_development_dependency 'coffee-rails', '~> 4.0'
|
32
|
+
s.add_development_dependency 'turbolinks', "~> 2.2"
|
33
|
+
s.add_development_dependency 'jbuilder', '~> 1.2'
|
34
|
+
s.add_development_dependency 'jquery-validation-rails', '~> 1.12'
|
35
|
+
s.add_development_dependency 'therubyracer', '~> 0.12'
|
36
|
+
s.add_development_dependency "colorize", '~> 0.7'
|
37
|
+
|
38
|
+
s.add_dependency "activemodel", "~> 4.1"
|
39
|
+
end
|
@@ -0,0 +1,217 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
require 'active_support/core_ext/object' # Helpers para los objetos (instance_values, etc.)
|
3
|
+
require_relative 'data_types'
|
4
|
+
|
5
|
+
module ActiveModel
|
6
|
+
module Serializers
|
7
|
+
# == Active Model Binary serializer
|
8
|
+
module Binary
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
include ActiveModel::Serialization
|
11
|
+
include DataTypes
|
12
|
+
|
13
|
+
included do
|
14
|
+
extend ActiveModel::Naming
|
15
|
+
|
16
|
+
class_attribute :attr_config
|
17
|
+
self.attr_config = {}
|
18
|
+
end
|
19
|
+
|
20
|
+
module ClassMethods
|
21
|
+
def serialize_options(attr_name, coder, count=1, length=1)
|
22
|
+
self.attr_config.merge!(attr_name.to_s => {:coder => coder, :count => count, :length => length})
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Serializer #:nodoc:
|
27
|
+
attr_reader :options
|
28
|
+
|
29
|
+
def initialize(serializable, options = nil)
|
30
|
+
@serializable = serializable
|
31
|
+
@options = options ? options.dup : {}
|
32
|
+
end
|
33
|
+
|
34
|
+
def dump
|
35
|
+
serializable_values = @serializable.serializable_hash(options)
|
36
|
+
start_address = @options[:start_address] || 0
|
37
|
+
|
38
|
+
buffer = [] # Buffer en bytes
|
39
|
+
tmp_buffer = [] # Buffer temporal en bytes
|
40
|
+
current_address = start_address*2 + 0.0 # Dirección en bytes
|
41
|
+
|
42
|
+
@serializable.attr_config.each do |key, value|
|
43
|
+
var = value[:coder].new(value[:count], value[:length])
|
44
|
+
# Busca el valor del atributo y si no existe devuelve nil
|
45
|
+
var.value = serializable_values[key] rescue nil
|
46
|
+
|
47
|
+
byte = current_address.floor
|
48
|
+
bit = (current_address.modulo(1)*8).round
|
49
|
+
|
50
|
+
tmp_buffer = var.dump
|
51
|
+
|
52
|
+
if @options[:align]
|
53
|
+
# Si el dato es una palabra simple, alinea los datos en el siguiente byte par
|
54
|
+
if var.bit_length > 8 and current_address.modulo(2) != 0
|
55
|
+
byte += 1
|
56
|
+
bit = 0
|
57
|
+
end
|
58
|
+
# Si el dato es una palabra doble, alinea los datos en la siguiente palabra par
|
59
|
+
if var.bit_length > 16 and (current_address + start_address*2).modulo(4) != 0
|
60
|
+
byte += 4-byte%4
|
61
|
+
bit = 0
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Si los datos ocupan mas de un byte concatena los arrays
|
66
|
+
if var.bit_length >= 8 and @options[:align]
|
67
|
+
buffer.insert(byte, tmp_buffer).flatten!
|
68
|
+
else # En caso de ser bits
|
69
|
+
tmp_buffer.flatten!
|
70
|
+
tmp_bits=tmp_buffer.pack('C*').unpack('b*').first.slice(0,var.size*8)
|
71
|
+
tmp_buffer=[tmp_bits.rjust(tmp_bits.length+bit,'0')].pack('b*').unpack('C*')
|
72
|
+
|
73
|
+
tmp_buffer.each_with_index do |v,i|
|
74
|
+
buffer[byte+i] = (buffer[byte+i] || 0) | v
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
current_address = (byte+bit/8.0)+var.size
|
79
|
+
end
|
80
|
+
buffer.map!{|el| el || 0}
|
81
|
+
end
|
82
|
+
|
83
|
+
#deserializado
|
84
|
+
def load (buffer=[])
|
85
|
+
serialized_values = {}
|
86
|
+
start_address = @options[:start_address] || 0
|
87
|
+
|
88
|
+
buffer ||= [] # Buffer en bytes
|
89
|
+
tmp_buffer = [] # Buffer temporal en bytes
|
90
|
+
|
91
|
+
current_address = start_address*2 + 0.0 # Dirección en bytes
|
92
|
+
|
93
|
+
@serializable.attr_config.each do |key, value|
|
94
|
+
#puts "#{key} - #{value}"
|
95
|
+
byte = current_address.floor
|
96
|
+
bit = (current_address.modulo(1)*8).round
|
97
|
+
|
98
|
+
var = value[:coder].new(value[:count], value[:length]) #creo objeto del tipo de dato pasado
|
99
|
+
|
100
|
+
if @options[:align]
|
101
|
+
# Si el dato es una palabra simple, alinea los datos en el siguiente byte par
|
102
|
+
if var.bit_length > 8 and current_address.modulo(2) != 0
|
103
|
+
byte += 1
|
104
|
+
bit = 0
|
105
|
+
end
|
106
|
+
# Si el dato es una palabra doble, alinea los datos en la siguiente palabra par
|
107
|
+
if var.bit_length > 16 and (current_address + start_address*2).modulo(4) != 0
|
108
|
+
byte += 4-byte%4
|
109
|
+
bit = 0
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# Si los datos ocupan mas de un byte, obtiene los bytes completos del buffer original
|
114
|
+
if var.bit_length >= 8 and @options[:align]
|
115
|
+
result_deserialized=var.load(buffer.slice(byte, var.size))
|
116
|
+
else # En caso de ser bits
|
117
|
+
tmp_buffer = buffer.slice(byte, var.size.ceil)
|
118
|
+
result_deserialized=var.load([tmp_buffer.pack('C*').unpack('b*').first.slice(bit,var.size*8)].pack('b*').unpack('C*'))
|
119
|
+
end
|
120
|
+
# puts result_deserialized.inspect
|
121
|
+
serialized_values["#{key}"] = result_deserialized.count>1 ? result_deserialized : result_deserialized.first
|
122
|
+
current_address = (byte+bit/8.0)+var.size
|
123
|
+
end
|
124
|
+
|
125
|
+
# if !@serializable.instance_variable_get(:@attributes).nil?
|
126
|
+
# @serializable.instance_variable_get(:@attributes).merge!(serialized_values) rescue nil
|
127
|
+
# else
|
128
|
+
serialized_values.each do |k,v|
|
129
|
+
if @serializable.respond_to? "#{k}="
|
130
|
+
@serializable.send("#{k}=", v)
|
131
|
+
else
|
132
|
+
@serializable.instance_variable_set("@#{k}".to_sym, v)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
#end
|
136
|
+
@serializable
|
137
|
+
end
|
138
|
+
|
139
|
+
end #close class serializer
|
140
|
+
|
141
|
+
# Returns XML representing the model. Configuration can be
|
142
|
+
# passed through +options+.
|
143
|
+
#
|
144
|
+
# Without any +options+, the returned XML string will include all the
|
145
|
+
# model's attributes.
|
146
|
+
#
|
147
|
+
# user = User.find(1)
|
148
|
+
# user.to_xml
|
149
|
+
#
|
150
|
+
# <?xml version="1.0" encoding="UTF-8"?>
|
151
|
+
# <user>
|
152
|
+
# <id type="integer">1</id>
|
153
|
+
# <name>David</name>
|
154
|
+
# <age type="integer">16</age>
|
155
|
+
# <created-at type="dateTime">2011-01-30T22:29:23Z</created-at>
|
156
|
+
# </user>
|
157
|
+
#
|
158
|
+
# The <tt>:only</tt> and <tt>:except</tt> options can be used to limit the
|
159
|
+
# attributes included, and work similar to the +attributes+ method.
|
160
|
+
#
|
161
|
+
# To include the result of some method calls on the model use <tt>:methods</tt>.
|
162
|
+
#
|
163
|
+
# To include associations use <tt>:include</tt>.
|
164
|
+
#
|
165
|
+
# For further documentation, see <tt>ActiveRecord::Serialization#to_xml</tt>
|
166
|
+
def to_bytes(options = {}, &block)
|
167
|
+
default_options = {
|
168
|
+
:align => true,
|
169
|
+
:block => block
|
170
|
+
}
|
171
|
+
options = default_options.deep_merge(options)
|
172
|
+
if !options[:block].blank?
|
173
|
+
instance_exec(self, &options[:block])
|
174
|
+
end
|
175
|
+
Serializer.new(self, options).dump
|
176
|
+
end
|
177
|
+
|
178
|
+
# Sets the model +attributes+ from an XML string. Returns +self+.
|
179
|
+
#
|
180
|
+
# class Person
|
181
|
+
# include ActiveModel::Serializers::Xml
|
182
|
+
#
|
183
|
+
# attr_accessor :name, :age, :awesome
|
184
|
+
#
|
185
|
+
# def attributes=(hash)
|
186
|
+
# hash.each do |key, value|
|
187
|
+
# instance_variable_set("@#{key}", value)
|
188
|
+
# end
|
189
|
+
# end
|
190
|
+
#
|
191
|
+
# def attributes
|
192
|
+
# instance_values
|
193
|
+
# end
|
194
|
+
# end
|
195
|
+
#
|
196
|
+
# xml = { name: 'bob', age: 22, awesome:true }.to_xml
|
197
|
+
# person = Person.new
|
198
|
+
# person.from_xml(xml) # => #<Person:0x007fec5e3b3c40 @age=22, @awesome=true, @name="bob">
|
199
|
+
# person.name # => "bob"
|
200
|
+
# person.age # => 22
|
201
|
+
# person.awesome # => true
|
202
|
+
def from_bytes(buffer, options = {}, &block)
|
203
|
+
default_options = {
|
204
|
+
:align => true,
|
205
|
+
:block => block
|
206
|
+
}
|
207
|
+
options = default_options.deep_merge(options)
|
208
|
+
retVal = Serializer.new(self, options).load buffer
|
209
|
+
|
210
|
+
if !options[:block].blank?
|
211
|
+
instance_exec(self, &options[:block])
|
212
|
+
end
|
213
|
+
retVal
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|