simple_serializer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +15 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +56 -0
- data/Rakefile +2 -0
- data/lib/simple_serializer/deserializer.rb +68 -0
- data/lib/simple_serializer/serializer.rb +73 -0
- data/lib/simple_serializer/version.rb +3 -0
- data/lib/simple_serializer.rb +4 -0
- data/simple_serializer.gemspec +24 -0
- data/spec/unit/simple_serializer/deserializer_spec.rb +68 -0
- data/spec/unit/simple_serializer/serializer_spec.rb +50 -0
- data/spec/unit_spec_helper.rb +4 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZGU3YmExMWM5OGZjZjc0ZGZjMDM5YzQxZTc0MDdmZWZkNDI1NmExYQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MjcyZmFhZTUzZjkxZjcwYjZlOGYwM2M0NTMzNDRlMWU1ZmI2Njg2NQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YTE5MmQ4NmVjMWMzYjg1MjVlNDVkZGYzZjIyMmNhYTdjMTViMGViZTlkNzE5
|
10
|
+
MDE1NjBjMTk3MDMyMDAyZjdjYmE5NzFmMTY5NDU2YmIyYThhNTc2ODAxMzE1
|
11
|
+
MzM3ZmE1NmQ0OTYwYjBhMjE0MzMxNzE0Y2VmYzFhZmNmN2U5ZmY=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NzU5YzE0YzFiZTkyOWM2NzU3YzFmNDg4NDEyMDVkOWMzZDRhMjllZTllODM4
|
14
|
+
NDY2MTdkYWI1ZmQ1MWMzMzRmZGYyMDgxOGMzMDFhOGRlYmE5YjgzMTRlNjQz
|
15
|
+
MWVjMTBkMGRlMWY3Y2NiMzA5NjRmNWIxZGIxNjQ4YjQxNmEyM2Q=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Nulogy Corporation
|
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,56 @@
|
|
1
|
+
# SimpleSerializer
|
2
|
+
|
3
|
+
Simple framelet for serializing/deserializing objects to hashes.
|
4
|
+
|
5
|
+
API compatible with ActiveModel::Serializer but without all the complexity
|
6
|
+
and dependence on ActiveModel
|
7
|
+
|
8
|
+
# Serializer Usage
|
9
|
+
|
10
|
+
```
|
11
|
+
class SomeSerializer < SimpleSerializer::Serializer
|
12
|
+
attributes :id, :name, :category_id, :errors
|
13
|
+
|
14
|
+
def category_id
|
15
|
+
object.category.try(:id)
|
16
|
+
end
|
17
|
+
|
18
|
+
def errors
|
19
|
+
ActiveModelErrorsSerializer.serialize_errors(object.errors, true) if object.errors.any?
|
20
|
+
end
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
Use it:
|
25
|
+
|
26
|
+
```
|
27
|
+
SomeSerializer.serialize(object)
|
28
|
+
SomeSerializer.new(object).serialize
|
29
|
+
|
30
|
+
SomeSerializer.serialize_array([object])
|
31
|
+
```
|
32
|
+
|
33
|
+
# Deserializer Usage
|
34
|
+
|
35
|
+
```
|
36
|
+
class SomeDeserializer < SimpleSerializer::Deserializer
|
37
|
+
data_attributes :site_id, :name, :category_id, :integration_key
|
38
|
+
|
39
|
+
def integration_key(old_integration_key)
|
40
|
+
"XX#{@data[:other_attr]}XX#{old_integration_key}XX"
|
41
|
+
end
|
42
|
+
|
43
|
+
def set_category_id(category_id)
|
44
|
+
object.category = InventoryStatusCategory.from_id(category_id)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
Use it:
|
50
|
+
|
51
|
+
```
|
52
|
+
SomeDeserializer.deserialize(object, data)
|
53
|
+
SomeDeserializer.new(object, data).deserialize
|
54
|
+
|
55
|
+
SomeDeserializer.deserialize_array([object1, object2, ...], [data1, data2, ...])
|
56
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# Simple framelet for deserialization
|
2
|
+
#
|
3
|
+
# class SomeDeserializer < SimpleSerializer::Deserializer
|
4
|
+
# data_attributes :site_id, :name, :category_id, :integration_key
|
5
|
+
#
|
6
|
+
# def integration_key(old_integration_key)
|
7
|
+
# "XX#{@data[:other_attr]}XX#{old_integration_key}XX"
|
8
|
+
# end
|
9
|
+
#
|
10
|
+
# def set_category_id(category_id)
|
11
|
+
# object.category = InventoryStatusCategory.from_id(category_id)
|
12
|
+
# end
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# Usage:
|
16
|
+
#
|
17
|
+
# SomeDeserializer.deserialize(object, data)
|
18
|
+
# SomeDeserializer.new(object, data).deserialize
|
19
|
+
#
|
20
|
+
# SomeDeserializer.deserialize_array([object1, object2, ...], [data1, data2, ...])
|
21
|
+
#
|
22
|
+
module SimpleSerializer
|
23
|
+
class Deserializer
|
24
|
+
class << self
|
25
|
+
attr_accessor :_attributes
|
26
|
+
|
27
|
+
def inherited(base)
|
28
|
+
base._attributes = []
|
29
|
+
end
|
30
|
+
|
31
|
+
def data_attributes(*attrs)
|
32
|
+
@_attributes.concat attrs
|
33
|
+
|
34
|
+
attrs.each do |attr|
|
35
|
+
define_method attr do |datum|
|
36
|
+
datum
|
37
|
+
end unless method_defined?(attr)
|
38
|
+
|
39
|
+
define_method "set_#{attr}" do |datum|
|
40
|
+
object.send("#{attr}=", send(attr, datum))
|
41
|
+
end unless method_defined?("set_#{attr}")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def deserialize_array(objects, data)
|
46
|
+
objects.zip(data).map { |obj, datum| deserialize(obj, datum) }
|
47
|
+
end
|
48
|
+
|
49
|
+
def deserialize(object, data)
|
50
|
+
self.new(object, data).deserialize
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
attr_accessor :object
|
55
|
+
|
56
|
+
def initialize(object, data)
|
57
|
+
@object = object
|
58
|
+
@data = data
|
59
|
+
end
|
60
|
+
|
61
|
+
def deserialize
|
62
|
+
self.class._attributes.dup.each do |name|
|
63
|
+
send("set_#{name}", @data[name])
|
64
|
+
end
|
65
|
+
object
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# Simple framelet for serialization.
|
2
|
+
#
|
3
|
+
# API compatible with ActiveModel::Serializer but without all the complexity
|
4
|
+
# and dependence on ActiveModel
|
5
|
+
#
|
6
|
+
# class SomeSerializer < SimpleSerializer::Serializer
|
7
|
+
# attributes :id, :name, :category_id, :errors
|
8
|
+
#
|
9
|
+
# def category_id
|
10
|
+
# object.category.try(:id)
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# def errors
|
14
|
+
# ActiveModelErrorsSerializer.serialize_errors(object.errors, true) if object.errors.any?
|
15
|
+
# end
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# Usage:
|
19
|
+
#
|
20
|
+
# SomeSerializer.serialize(object)
|
21
|
+
# SomeSerializer.new(object).serialize
|
22
|
+
#
|
23
|
+
# SomeSerializer.serialize_array([object])
|
24
|
+
#
|
25
|
+
module SimpleSerializer
|
26
|
+
class Serializer
|
27
|
+
class << self
|
28
|
+
attr_accessor :_attributes
|
29
|
+
|
30
|
+
def inherited(base)
|
31
|
+
base._attributes = []
|
32
|
+
end
|
33
|
+
|
34
|
+
def attributes(*attrs)
|
35
|
+
@_attributes.concat attrs
|
36
|
+
|
37
|
+
attrs.each do |attr|
|
38
|
+
define_method attr do
|
39
|
+
object.send(attr)
|
40
|
+
end unless method_defined?(attr)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def serialize_array(objects)
|
45
|
+
objects.map { |obj| serialize(obj) }
|
46
|
+
end
|
47
|
+
|
48
|
+
def serialize(object)
|
49
|
+
self.new(object).serialize
|
50
|
+
end
|
51
|
+
|
52
|
+
alias :as_json :serialize
|
53
|
+
end
|
54
|
+
|
55
|
+
attr_accessor :object
|
56
|
+
|
57
|
+
def initialize(object, _={})
|
58
|
+
@object = object
|
59
|
+
end
|
60
|
+
|
61
|
+
def attributes
|
62
|
+
self.class._attributes.dup.each_with_object({}) do |name, hash|
|
63
|
+
hash[name] = send(name)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def serialize(_={})
|
68
|
+
return nil if object.nil?
|
69
|
+
attributes
|
70
|
+
end
|
71
|
+
alias :as_json :serialize
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'simple_serializer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "simple_serializer"
|
8
|
+
spec.version = SimpleSerializer::VERSION
|
9
|
+
spec.authors = ["Sean Kirby"]
|
10
|
+
spec.email = ["seank@nulogy.com"]
|
11
|
+
spec.summary = %q{Very simple framelet for serializing/deserializing objects to hashes.}
|
12
|
+
spec.description = %q{Very simple framelet for serializing/deserializing objects to hashes.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(spec)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'unit_spec_helper'
|
2
|
+
require 'simple_serializer/deserializer'
|
3
|
+
|
4
|
+
describe SimpleSerializer::Deserializer do
|
5
|
+
|
6
|
+
let(:object) { double }
|
7
|
+
|
8
|
+
class TestDeserializer < SimpleSerializer::Deserializer
|
9
|
+
data_attributes :field1
|
10
|
+
end
|
11
|
+
|
12
|
+
describe 'deserialize' do
|
13
|
+
it 'sets attributes' do
|
14
|
+
attrs = {field1: 1}
|
15
|
+
expect(object).to receive(:field1=).with(1)
|
16
|
+
|
17
|
+
result = TestDeserializer.deserialize(object, attrs)
|
18
|
+
expect(result).to be object
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 'attribute value transformation' do
|
22
|
+
class TestTransformingDeserializer < SimpleSerializer::Deserializer
|
23
|
+
data_attributes :field1
|
24
|
+
|
25
|
+
def field1(datum)
|
26
|
+
datum + 1
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'maps values' do
|
31
|
+
attrs = {field1: 1}
|
32
|
+
expect(object).to receive(:field1=).with(2)
|
33
|
+
|
34
|
+
TestTransformingDeserializer.deserialize(object, attrs)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "attribute key transformation" do
|
39
|
+
class TestMappingDeserializer < SimpleSerializer::Deserializer
|
40
|
+
data_attributes :association_id
|
41
|
+
|
42
|
+
def set_association_id(datum)
|
43
|
+
object.association = datum
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'maps keys' do
|
48
|
+
attrs = {association_id: 1}
|
49
|
+
expect(object).to receive(:association=).with(1)
|
50
|
+
|
51
|
+
TestMappingDeserializer.deserialize(object, attrs)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe 'deserialize_array' do
|
57
|
+
it 'deserializes many objects' do
|
58
|
+
data = [{field1: 1}, {field1: 3}]
|
59
|
+
expect(object).to receive(:field1=).with(1)
|
60
|
+
|
61
|
+
object2 = double
|
62
|
+
expect(object2).to receive(:field1=).with(3)
|
63
|
+
|
64
|
+
result = TestDeserializer.deserialize_array([object, object2], data)
|
65
|
+
expect(result).to eq [object, object2]
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'unit_spec_helper'
|
2
|
+
require 'simple_serializer/serializer'
|
3
|
+
|
4
|
+
describe SimpleSerializer::Serializer do
|
5
|
+
|
6
|
+
let(:object) { double }
|
7
|
+
|
8
|
+
class TestSerializer < SimpleSerializer::Serializer
|
9
|
+
attributes :field1, :field2
|
10
|
+
end
|
11
|
+
|
12
|
+
describe 'serialize' do
|
13
|
+
it 'sets attributes' do
|
14
|
+
object = double(field1: 1, field2: 2)
|
15
|
+
|
16
|
+
result = TestSerializer.serialize(object)
|
17
|
+
|
18
|
+
expect(result).to eq({field1: 1, field2: 2})
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 'value transformation' do
|
22
|
+
class TestTransformingSerializer < SimpleSerializer::Serializer
|
23
|
+
attributes :field1
|
24
|
+
|
25
|
+
def field1
|
26
|
+
object.field1 + 1
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'maps values' do
|
31
|
+
object = double(field1: 1)
|
32
|
+
|
33
|
+
result = TestTransformingSerializer.serialize(object)
|
34
|
+
|
35
|
+
expect(result).to eq({field1: 2})
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe 'serialize_array' do
|
41
|
+
it 'serializes many objects' do
|
42
|
+
object1 = double(field1: 1, field2: 2)
|
43
|
+
object2 = double(field1: 3, field2: 4)
|
44
|
+
|
45
|
+
result = TestSerializer.serialize_array([object1, object2])
|
46
|
+
|
47
|
+
expect(result).to eq([{field1: 1, field2: 2}, {field1: 3, field2: 4}])
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_serializer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sean Kirby
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Very simple framelet for serializing/deserializing objects to hashes.
|
56
|
+
email:
|
57
|
+
- seank@nulogy.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/simple_serializer.rb
|
68
|
+
- lib/simple_serializer/deserializer.rb
|
69
|
+
- lib/simple_serializer/serializer.rb
|
70
|
+
- lib/simple_serializer/version.rb
|
71
|
+
- simple_serializer.gemspec
|
72
|
+
- spec/unit/simple_serializer/deserializer_spec.rb
|
73
|
+
- spec/unit/simple_serializer/serializer_spec.rb
|
74
|
+
- spec/unit_spec_helper.rb
|
75
|
+
homepage: ''
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ! '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.4.5
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Very simple framelet for serializing/deserializing objects to hashes.
|
99
|
+
test_files:
|
100
|
+
- spec/unit/simple_serializer/deserializer_spec.rb
|
101
|
+
- spec/unit/simple_serializer/serializer_spec.rb
|
102
|
+
- spec/unit_spec_helper.rb
|