serialize 0.0.1
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.
- data/.gitignore +19 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +90 -0
- data/Rakefile +6 -0
- data/lib/serialize.rb +47 -0
- data/lib/serialize/format.rb +21 -0
- data/lib/serialize/generator.rb +22 -0
- data/lib/serialize/json.rb +9 -0
- data/lib/serialize/version.rb +3 -0
- data/lib/serialize/xml.rb +26 -0
- data/serialize.gemspec +21 -0
- data/test/all.rb +3 -0
- data/test/serialize/generator_test.rb +17 -0
- data/test/serialize_test.rb +64 -0
- data/test/support/mock_resource.rb +11 -0
- data/test/support/mock_user.rb +11 -0
- data/test/support/resource_serializer.rb +9 -0
- data/test/support/user_serializer.rb +16 -0
- data/test/test_helper.rb +6 -0
- metadata +107 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Dane Harrigan
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# Serialize [][1]
|
2
|
+
|
3
|
+
Move serialization logic out of your model
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'serialize'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install serialize
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
class UserSerializer < Serialize
|
22
|
+
# create a default serialization structure
|
23
|
+
structure do
|
24
|
+
{
|
25
|
+
:name => name,
|
26
|
+
:gender => gender,
|
27
|
+
:email => email
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
# create a named serialization structure
|
32
|
+
structure :extended do
|
33
|
+
{
|
34
|
+
:name => name,
|
35
|
+
:gender => gender,
|
36
|
+
:email => email,
|
37
|
+
:height => height,
|
38
|
+
:eye_color => eye_color,
|
39
|
+
:hair_color => hair_color
|
40
|
+
}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
@app = App.find(params[:id])
|
45
|
+
|
46
|
+
# serialize your @app object with the default structure
|
47
|
+
AppSerializer(@app).to_json
|
48
|
+
|
49
|
+
# serialize your @app with a specified structure
|
50
|
+
AppSerializer.new(@app, :as => :extended)
|
51
|
+
|
52
|
+
# serializers can take an array of objects
|
53
|
+
@collection = App.all
|
54
|
+
AppSerializer.new(@collection, :as => :extended)
|
55
|
+
|
56
|
+
## Serialization Formats
|
57
|
+
|
58
|
+
The `serialize` gem comes with JSON and XML support, but you can add
|
59
|
+
your own.
|
60
|
+
|
61
|
+
To do this, you'll have to define a `render` method that accepts
|
62
|
+
`*args`. Because JSON and XML parsing pass different arguments `*args`
|
63
|
+
keeps the interface generic.
|
64
|
+
|
65
|
+
The hash to be serialized will be available as `content`.
|
66
|
+
|
67
|
+
Once your serialization format is built register it with by using the
|
68
|
+
`Serialize.register` method.
|
69
|
+
|
70
|
+
class Serialize::CSV < Serialize::Format
|
71
|
+
def render(*args)
|
72
|
+
if args.empty?
|
73
|
+
# turn hash into CSV
|
74
|
+
else
|
75
|
+
# do something else because arguments are passed in
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
Serialize.register "text/csv", Serialize::CSV
|
81
|
+
|
82
|
+
## Contributing
|
83
|
+
|
84
|
+
1. Fork it
|
85
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
86
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
87
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
88
|
+
5. Create new Pull Request
|
89
|
+
|
90
|
+
[1]: https://secure.travis-ci.org/daneharrigan/serialize
|
data/Rakefile
ADDED
data/lib/serialize.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require "serialize/version"
|
2
|
+
require "serialize/generator"
|
3
|
+
require "serialize/format"
|
4
|
+
require "serialize/json"
|
5
|
+
require "serialize/xml"
|
6
|
+
require "active_support/core_ext"
|
7
|
+
|
8
|
+
class Serialize
|
9
|
+
def initialize(object, options={})
|
10
|
+
key = options[:as] || :default
|
11
|
+
@object = object
|
12
|
+
@block = self.class.structures[key]
|
13
|
+
end
|
14
|
+
|
15
|
+
def responses
|
16
|
+
@@serializers.keys
|
17
|
+
end
|
18
|
+
|
19
|
+
def to(content_type)
|
20
|
+
subtype = content_type.split("/").last
|
21
|
+
send("to_#{subtype}")
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.structure(key=nil, &block)
|
25
|
+
key ||= :default
|
26
|
+
structures[key] = block
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.structures
|
30
|
+
@structures ||= {}
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.register(content_type, klass)
|
34
|
+
@@serializers ||= {}
|
35
|
+
@@serializers[content_type] = klass
|
36
|
+
subtype = content_type.split("/").last
|
37
|
+
|
38
|
+
define_method "to_#{subtype}" do |*args|
|
39
|
+
@@serializers[content_type].new(@object, @block).render(*args)
|
40
|
+
end
|
41
|
+
|
42
|
+
send :alias_method, "as_#{subtype}", "to_#{subtype}"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
Serialize.register "application/json", Serialize::Json
|
47
|
+
Serialize.register "application/xml", Serialize::Xml
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Serialize
|
2
|
+
class Format
|
3
|
+
attr :object
|
4
|
+
private :object
|
5
|
+
|
6
|
+
def initialize(object, block)
|
7
|
+
@object = object
|
8
|
+
@block = block
|
9
|
+
end
|
10
|
+
|
11
|
+
def content
|
12
|
+
if object.respond_to? :map
|
13
|
+
object.map { |obj| Generator.new(obj, @block).to_hash }
|
14
|
+
else
|
15
|
+
Generator.new(object, @block).to_hash
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private :content
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class Serialize
|
2
|
+
class Generator
|
3
|
+
def initialize(object, block)
|
4
|
+
@object = object
|
5
|
+
@block = block
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_hash
|
9
|
+
instance_eval &@block
|
10
|
+
end
|
11
|
+
|
12
|
+
def method_missing(*args, &block)
|
13
|
+
method = args.first
|
14
|
+
|
15
|
+
if @object.respond_to? method
|
16
|
+
@object.send *args, &block
|
17
|
+
else
|
18
|
+
raise NoMethodError
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class Serialize
|
2
|
+
class Xml < Format
|
3
|
+
def render(*args)
|
4
|
+
# args are empty unless called through
|
5
|
+
# another to_xml call
|
6
|
+
options = unless args.empty?
|
7
|
+
args.first
|
8
|
+
else
|
9
|
+
subject = collection? ? object.first.class.name : object.class.name
|
10
|
+
name = subject.include?("::") ? subject.split("::").last : subject
|
11
|
+
name = name.titleize.downcase
|
12
|
+
name = name.pluralize if collection?
|
13
|
+
{ :root => name }
|
14
|
+
end
|
15
|
+
|
16
|
+
content.to_xml(options)
|
17
|
+
end
|
18
|
+
|
19
|
+
def collection?
|
20
|
+
object.respond_to? :map
|
21
|
+
end
|
22
|
+
|
23
|
+
private :collection?
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
data/serialize.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/serialize/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Dane Harrigan"]
|
6
|
+
gem.email = ["dane.harrigan@gmail.com"]
|
7
|
+
gem.description = %q{Simple object serialization}
|
8
|
+
gem.summary = %q{Simple object serialization}
|
9
|
+
gem.homepage = "https://github.com/daneharrigan/serialize"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "serialize"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Serialize::VERSION
|
17
|
+
|
18
|
+
gem.add_runtime_dependency "json"
|
19
|
+
gem.add_runtime_dependency "activesupport"
|
20
|
+
gem.add_runtime_dependency "builder"
|
21
|
+
end
|
data/test/all.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class Serialize::GeneratorTest < MiniTest::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
object = MockUser.new
|
6
|
+
block = proc do
|
7
|
+
{ name: name, age: age }
|
8
|
+
end
|
9
|
+
|
10
|
+
@generator = Serialize::Generator.new(object, block)
|
11
|
+
@hash = { name: object.name, age: object.age }
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_hash_creation_from_block
|
15
|
+
assert_equal @hash, @generator.to_hash
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class SerializeTest < MiniTest::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@user = MockUser.new
|
6
|
+
@resource = MockResource.new
|
7
|
+
@user_hash = { name: @user.name, age: @user.age }
|
8
|
+
@collection = [@user, @user, @user]
|
9
|
+
@hash_collection = [@user_hash, @user_hash, @user_hash]
|
10
|
+
@nested_hash = { name: @resource.name, type: @resource.type, users: @hash_collection }
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_default_serialization
|
14
|
+
serializer = UserSerializer.new(@user)
|
15
|
+
assert_equal @user_hash.to_json, serializer.to_json
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_named_json_serialization
|
19
|
+
@user_hash[:email] = @user.email
|
20
|
+
serializer = UserSerializer.new(@user, as: :extended)
|
21
|
+
assert_equal @user_hash.to_json, serializer.to_json
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_collection_json_serialization
|
25
|
+
serializer = UserSerializer.new(@collection)
|
26
|
+
assert_equal @hash_collection.to_json, serializer.to_json
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_named_collection_json_serialization
|
30
|
+
@hash_collection.each_with_index do |value, index|
|
31
|
+
@hash_collection[index][:email] = @user.email
|
32
|
+
end
|
33
|
+
|
34
|
+
serializer = UserSerializer.new(@collection, as: :extended)
|
35
|
+
assert_equal @hash_collection.to_json, serializer.to_json
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_nested_json_serialization
|
39
|
+
serializer = ResourceSerializer.new(@resource)
|
40
|
+
assert_equal @nested_hash.to_json, serializer.to_json
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_xml_serialization
|
44
|
+
serializer = UserSerializer.new(@user)
|
45
|
+
xml = @user_hash.to_xml(root: "mock-user")
|
46
|
+
|
47
|
+
assert_equal xml, serializer.to_xml
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_nested_xml_serialization
|
51
|
+
serializer = ResourceSerializer.new(@resource)
|
52
|
+
assert_equal @nested_hash.to_xml(root: "mock-resource"), serializer.to_xml
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_default_responses
|
56
|
+
serializer = UserSerializer.new(@user)
|
57
|
+
assert_equal ["application/json", "application/xml"], serializer.responses
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_to_content_type
|
61
|
+
serializer = UserSerializer.new(@user)
|
62
|
+
assert_equal serializer.to("application/json"), serializer.to_json
|
63
|
+
end
|
64
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: serialize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dane Harrigan
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: json
|
16
|
+
requirement: &70363930730240 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70363930730240
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activesupport
|
27
|
+
requirement: &70363930729820 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70363930729820
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: builder
|
38
|
+
requirement: &70363930729380 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70363930729380
|
47
|
+
description: Simple object serialization
|
48
|
+
email:
|
49
|
+
- dane.harrigan@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- .travis.yml
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- lib/serialize.rb
|
61
|
+
- lib/serialize/format.rb
|
62
|
+
- lib/serialize/generator.rb
|
63
|
+
- lib/serialize/json.rb
|
64
|
+
- lib/serialize/version.rb
|
65
|
+
- lib/serialize/xml.rb
|
66
|
+
- serialize.gemspec
|
67
|
+
- test/all.rb
|
68
|
+
- test/serialize/generator_test.rb
|
69
|
+
- test/serialize_test.rb
|
70
|
+
- test/support/mock_resource.rb
|
71
|
+
- test/support/mock_user.rb
|
72
|
+
- test/support/resource_serializer.rb
|
73
|
+
- test/support/user_serializer.rb
|
74
|
+
- test/test_helper.rb
|
75
|
+
homepage: https://github.com/daneharrigan/serialize
|
76
|
+
licenses: []
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ! '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 1.8.11
|
96
|
+
signing_key:
|
97
|
+
specification_version: 3
|
98
|
+
summary: Simple object serialization
|
99
|
+
test_files:
|
100
|
+
- test/all.rb
|
101
|
+
- test/serialize/generator_test.rb
|
102
|
+
- test/serialize_test.rb
|
103
|
+
- test/support/mock_resource.rb
|
104
|
+
- test/support/mock_user.rb
|
105
|
+
- test/support/resource_serializer.rb
|
106
|
+
- test/support/user_serializer.rb
|
107
|
+
- test/test_helper.rb
|