msgpack_rails 0.0.1 → 0.1.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 +4 -4
- data/README.md +45 -3
- data/lib/msgpack_rails/activemodel/serializers/message_pack.rb +39 -0
- data/lib/msgpack_rails/activesupport/core_ext/object/to_msgpack.rb +3 -2
- data/lib/msgpack_rails/activesupport/message_pack/decoding.rb +14 -0
- data/lib/msgpack_rails/activesupport/message_pack/encoding.rb +8 -4
- data/lib/msgpack_rails/activesupport/message_pack.rb +1 -0
- data/lib/msgpack_rails/version.rb +1 -1
- data/lib/msgpack_rails.rb +10 -0
- data/msgpack_rails.gemspec +3 -2
- data/test/{msgpack → msgpack_rails}/core_ext_test.rb +0 -0
- data/test/msgpack_rails/decoding_test.rb +22 -0
- data/test/{msgpack → msgpack_rails}/encoding_test.rb +0 -0
- data/test/msgpack_rails/models/contact.rb +26 -0
- data/test/msgpack_rails/serializer_test.rb +70 -0
- metadata +29 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9e573e9ead86a6c6edc4e4ef6dc435d021695165
|
4
|
+
data.tar.gz: b718ccf23b3a33571a7e7212d1bb7ee7e5a07342
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aac10592d912856d86ab31d9e7a9367054b27ff4533d0af16800e34eb747d76d21d7430bb03922850b8e302ddf519ba4d3b061210b6a691bd960092ed3598caf
|
7
|
+
data.tar.gz: 286ba642e3fbf050682e18fab75a416b1522b8bb90c2c02f47e095c47838f108c9a05f09b61b294e1c9212ebb9d69478f6018dbf85ec1dc7a66d506f29ecb0e4
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
#
|
1
|
+
# msgpack_rails
|
2
2
|
|
3
|
-
|
3
|
+
The Rails way to serialize/deserialize with [Message Pack](http://msgpack.org).
|
4
|
+
It implements an [ActiveSupport](http://rubygems.org/gems/activesupport) adapter and an [ActiveModel](http://rubygems.org/gems/activemodel) adapter for Message Pack.
|
4
5
|
|
5
6
|
## Installation
|
6
7
|
|
@@ -18,7 +19,48 @@ Or install it yourself as:
|
|
18
19
|
|
19
20
|
## Usage
|
20
21
|
|
21
|
-
|
22
|
+
`msgpack_rails` converts data type using `as_json` before feeding it into [msgpack](http://rubygems.org/gems/msgpack).
|
23
|
+
Here are a few examples:
|
24
|
+
|
25
|
+
$ ActiveSupport::MessagePack.encode(:a => :b)
|
26
|
+
=> "\x81\xA1a\xA1b"
|
27
|
+
|
28
|
+
$ ActiveSupport::MessagePack.encode(Time.now)
|
29
|
+
=> "\xB92013-09-11T10:40:39-07:00"
|
30
|
+
|
31
|
+
$ Time.now.as_msgpack
|
32
|
+
=> "2013-09-11T10:48:13-07:00"
|
33
|
+
|
34
|
+
$ Time.now.to_msgpack
|
35
|
+
=> "\xB92013-09-11T10:40:39-07:00"
|
36
|
+
|
37
|
+
$ ActiveSupport::MessagePack.decode Time.now.to_msgpack
|
38
|
+
=> "2013-09-11T11:23:07-07:00"
|
39
|
+
|
40
|
+
# After setting ActiveSupport.parse_msgpack_times to true
|
41
|
+
$ ActiveSupport::MessagePack.decode Time.now.to_msgpack
|
42
|
+
=> Wed, 11 Sep 2013 11:25:18 -0700
|
43
|
+
|
44
|
+
You can also use it as part of `ActiveModel`:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
class Contact
|
48
|
+
include ActiveModel::Serializers::MessagePack
|
49
|
+
|
50
|
+
...
|
51
|
+
end
|
52
|
+
|
53
|
+
@contact = Contact.new
|
54
|
+
@contact.name = 'Konata Izumi'
|
55
|
+
@contact.age = 16
|
56
|
+
@contact.created_at = Time.utc(2006, 8, 1)
|
57
|
+
@contact.awesome = true
|
58
|
+
@contact.preferences = { 'shows' => 'anime' }
|
59
|
+
|
60
|
+
@contact.to_msgpack # => msgpack output
|
61
|
+
@contact.to_msgpack(:root => true) # => include root in msgpack output
|
62
|
+
```
|
63
|
+
|
22
64
|
|
23
65
|
## Contributing
|
24
66
|
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require "msgpack_rails/activesupport/message_pack"
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
module Serializers
|
5
|
+
module MessagePack
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
include ActiveModel::Serialization
|
8
|
+
|
9
|
+
included do
|
10
|
+
extend ActiveModel::Naming
|
11
|
+
|
12
|
+
class_attribute :include_root_in_msgpack
|
13
|
+
self.include_root_in_msgpack = false
|
14
|
+
end
|
15
|
+
|
16
|
+
def as_msgpack(options = {})
|
17
|
+
root = if options && options.key?(:root)
|
18
|
+
options[:root]
|
19
|
+
else
|
20
|
+
include_root_in_msgpack
|
21
|
+
end
|
22
|
+
|
23
|
+
if root
|
24
|
+
root = self.class.model_name.element if root == true
|
25
|
+
{ root => serializable_hash(options) }
|
26
|
+
else
|
27
|
+
serializable_hash(options)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def from_msgpack(msgpack, include_root=include_root_in_msgpack)
|
32
|
+
hash = ActiveSupport::MessagePack.decode(msgpack)
|
33
|
+
hash = hash.values.first if include_root
|
34
|
+
self.attributes = hash
|
35
|
+
self
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -10,8 +10,9 @@ end
|
|
10
10
|
# otherwise they will always use to_msgpack gem implementation.
|
11
11
|
[Object, NilClass, TrueClass, FalseClass, Fixnum, Bignum, Float, String, Array, Hash, Symbol].each do |klass|
|
12
12
|
klass.class_eval do
|
13
|
-
|
14
|
-
|
13
|
+
alias_method :msgpack_to_msgpack, :to_msgpack if respond_to?(:to_msgpack)
|
14
|
+
# Dumps object in msgpack. See http://msgpack.org for more infoa
|
15
|
+
def to_msgpack(options = nil)
|
15
16
|
ActiveSupport::MessagePack.encode(self, options)
|
16
17
|
end
|
17
18
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module ActiveSupport
|
2
|
+
mattr_accessor :parse_msgpack_times
|
3
|
+
|
4
|
+
module MessagePack
|
5
|
+
def self.decode(data)
|
6
|
+
data = ::MessagePack.unpack(data)
|
7
|
+
if ActiveSupport.parse_msgpack_times
|
8
|
+
ActiveSupport::JSON.send(:convert_dates_from, data)
|
9
|
+
else
|
10
|
+
data
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -3,15 +3,19 @@ require "msgpack_rails/activesupport/core_ext/object/to_msgpack"
|
|
3
3
|
|
4
4
|
module ActiveSupport
|
5
5
|
module MessagePack
|
6
|
-
def self.encode(value, options =
|
7
|
-
out = options.
|
8
|
-
|
6
|
+
def self.encode(value, options = nil)
|
7
|
+
as_msgpack_opts, out = if options.is_a?(Hash)
|
8
|
+
[options, nil]
|
9
|
+
else
|
10
|
+
[nil, options]
|
11
|
+
end
|
12
|
+
value.as_msgpack(as_msgpack_opts).msgpack_to_msgpack(out)
|
9
13
|
end
|
10
14
|
end
|
11
15
|
end
|
12
16
|
|
13
17
|
class Object
|
14
|
-
def as_msgpack(options =
|
18
|
+
def as_msgpack(options = nil)
|
15
19
|
as_json(options)
|
16
20
|
end
|
17
21
|
end
|
data/lib/msgpack_rails.rb
CHANGED
@@ -1,9 +1,19 @@
|
|
1
1
|
require "active_support"
|
2
|
+
require "active_model"
|
2
3
|
require "msgpack_rails/version"
|
3
4
|
require "msgpack_rails/activesupport/message_pack"
|
5
|
+
require "msgpack_rails/activemodel/serializers/message_pack"
|
4
6
|
|
5
7
|
module ActiveSupport
|
6
8
|
eager_autoload do
|
7
9
|
autoload :MessagePack
|
8
10
|
end
|
9
11
|
end
|
12
|
+
|
13
|
+
module ActiveModel
|
14
|
+
module Serializers
|
15
|
+
eager_autoload do
|
16
|
+
autoload :MessagePack
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/msgpack_rails.gemspec
CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Jingwen Owen Ou"]
|
10
10
|
spec.email = ["jingweno@gmail.com"]
|
11
11
|
spec.description = %q{Message Pack for Rails.}
|
12
|
-
spec.summary = %q{
|
13
|
-
spec.homepage = ""
|
12
|
+
spec.summary = %q{The Rails way to serialize/deserialize with Message Pack.}
|
13
|
+
spec.homepage = "https://github.com/jingweno/msgpack_rails"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
@@ -19,5 +19,6 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
21
|
spec.add_runtime_dependency "activesupport", ">= 3.0"
|
22
|
+
spec.add_runtime_dependency "activemodel", ">= 3.0"
|
22
23
|
spec.add_runtime_dependency "msgpack"
|
23
24
|
end
|
File without changes
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative "../test_helper"
|
2
|
+
|
3
|
+
class TestMessagePackEncoding < Test::Unit::TestCase
|
4
|
+
def test_decoding
|
5
|
+
[Time.now, [Date.new], {:foo => DateTime.new}].each do |data|
|
6
|
+
assert_equal data.as_json, ActiveSupport::MessagePack.decode(ActiveSupport::MessagePack.encode(data))
|
7
|
+
end
|
8
|
+
|
9
|
+
ActiveSupport.parse_msgpack_times = true
|
10
|
+
|
11
|
+
date = DateTime.parse(DateTime.now.as_json)
|
12
|
+
|
13
|
+
test_data = date
|
14
|
+
assert_equal test_data, ActiveSupport::MessagePack.decode(ActiveSupport::MessagePack.encode(test_data))
|
15
|
+
|
16
|
+
test_data = [date]
|
17
|
+
assert_equal test_data, ActiveSupport::MessagePack.decode(ActiveSupport::MessagePack.encode(test_data))
|
18
|
+
|
19
|
+
test_data = { "foo" => date }
|
20
|
+
assert_equal test_data, ActiveSupport::MessagePack.decode(ActiveSupport::MessagePack.encode(test_data))
|
21
|
+
end
|
22
|
+
end
|
File without changes
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class Contact
|
2
|
+
extend ActiveModel::Naming
|
3
|
+
include ActiveModel::Conversion
|
4
|
+
|
5
|
+
attr_accessor :id, :name, :age, :created_at, :awesome, :preferences
|
6
|
+
|
7
|
+
def social
|
8
|
+
%w(twitter github)
|
9
|
+
end
|
10
|
+
|
11
|
+
def network
|
12
|
+
{ git: :github }
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(options = {})
|
16
|
+
options.each { |name, value| send("#{name}=", value) }
|
17
|
+
end
|
18
|
+
|
19
|
+
def pseudonyms
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
|
23
|
+
def persisted?
|
24
|
+
id
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require_relative "../test_helper"
|
2
|
+
require_relative "models/contact"
|
3
|
+
|
4
|
+
class Contact
|
5
|
+
include ActiveModel::Serializers::MessagePack
|
6
|
+
include ActiveModel::Validations
|
7
|
+
|
8
|
+
def attributes=(hash)
|
9
|
+
hash.each do |k, v|
|
10
|
+
instance_variable_set("@#{k}", v)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
remove_method :attributes if method_defined?(:attributes)
|
15
|
+
|
16
|
+
def attributes
|
17
|
+
instance_values
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class TestMessagePackSerializer < Test::Unit::TestCase
|
22
|
+
def setup
|
23
|
+
ActiveSupport.parse_msgpack_times = true
|
24
|
+
|
25
|
+
@contact = Contact.new
|
26
|
+
@contact.name = 'Konata Izumi'
|
27
|
+
@contact.age = 16
|
28
|
+
@contact.created_at = Time.utc(2006, 8, 1)
|
29
|
+
@contact.awesome = true
|
30
|
+
@contact.preferences = { 'shows' => 'anime' }
|
31
|
+
end
|
32
|
+
|
33
|
+
def teardown
|
34
|
+
# set to the default value
|
35
|
+
ActiveSupport.parse_msgpack_times = false
|
36
|
+
Contact.include_root_in_msgpack = false
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_not_include_root_in_msgpack
|
40
|
+
contact = ActiveSupport::MessagePack.decode(@contact.to_msgpack)
|
41
|
+
assert_equal @contact.attributes.size, contact.size
|
42
|
+
|
43
|
+
@contact.attributes.each do |k, v|
|
44
|
+
assert_equal v, contact[k]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_include_root_in_msgpack
|
49
|
+
result = ActiveSupport::MessagePack.decode(@contact.to_msgpack(:root => true))
|
50
|
+
assert_equal 1, result.size
|
51
|
+
|
52
|
+
contact = result["contact"]
|
53
|
+
@contact.attributes.each do |k, v|
|
54
|
+
assert_equal v, contact[k]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_from_msgpack
|
59
|
+
ActiveSupport.parse_msgpack_times = false
|
60
|
+
|
61
|
+
msgpack = @contact.to_msgpack(:root => true)
|
62
|
+
result = Contact.new.from_msgpack(msgpack, true)
|
63
|
+
|
64
|
+
assert_equal result.name, @contact.name
|
65
|
+
assert_equal result.age, @contact.age
|
66
|
+
assert_equal Time.parse(result.created_at), @contact.created_at
|
67
|
+
assert_equal result.awesome, @contact.awesome
|
68
|
+
assert_equal result.preferences, @contact.preferences
|
69
|
+
end
|
70
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: msgpack_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jingwen Owen Ou
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activemodel
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: msgpack
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -51,15 +65,20 @@ files:
|
|
51
65
|
- README.md
|
52
66
|
- Rakefile
|
53
67
|
- lib/msgpack_rails.rb
|
68
|
+
- lib/msgpack_rails/activemodel/serializers/message_pack.rb
|
54
69
|
- lib/msgpack_rails/activesupport/core_ext/object/to_msgpack.rb
|
55
70
|
- lib/msgpack_rails/activesupport/message_pack.rb
|
71
|
+
- lib/msgpack_rails/activesupport/message_pack/decoding.rb
|
56
72
|
- lib/msgpack_rails/activesupport/message_pack/encoding.rb
|
57
73
|
- lib/msgpack_rails/version.rb
|
58
74
|
- msgpack_rails.gemspec
|
59
|
-
- test/
|
60
|
-
- test/
|
75
|
+
- test/msgpack_rails/core_ext_test.rb
|
76
|
+
- test/msgpack_rails/decoding_test.rb
|
77
|
+
- test/msgpack_rails/encoding_test.rb
|
78
|
+
- test/msgpack_rails/models/contact.rb
|
79
|
+
- test/msgpack_rails/serializer_test.rb
|
61
80
|
- test/test_helper.rb
|
62
|
-
homepage:
|
81
|
+
homepage: https://github.com/jingweno/msgpack_rails
|
63
82
|
licenses:
|
64
83
|
- MIT
|
65
84
|
metadata: {}
|
@@ -82,8 +101,11 @@ rubyforge_project:
|
|
82
101
|
rubygems_version: 2.0.3
|
83
102
|
signing_key:
|
84
103
|
specification_version: 4
|
85
|
-
summary:
|
104
|
+
summary: The Rails way to serialize/deserialize with Message Pack.
|
86
105
|
test_files:
|
87
|
-
- test/
|
88
|
-
- test/
|
106
|
+
- test/msgpack_rails/core_ext_test.rb
|
107
|
+
- test/msgpack_rails/decoding_test.rb
|
108
|
+
- test/msgpack_rails/encoding_test.rb
|
109
|
+
- test/msgpack_rails/models/contact.rb
|
110
|
+
- test/msgpack_rails/serializer_test.rb
|
89
111
|
- test/test_helper.rb
|