active_serializer 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +6 -8
- data/lib/active_serializer/serialization_rules_validator.rb +15 -0
- data/lib/active_serializer/serializers/hash_serializer.rb +13 -0
- data/lib/active_serializer/serializers/object_serializer.rb +13 -0
- data/lib/active_serializer/support/args_validator.rb +6 -0
- data/lib/active_serializer/version.rb +1 -1
- data/spec/active_serializer/serializable_hash_spec.rb +28 -3
- data/spec/active_serializer/serializable_object_spec.rb +28 -2
- data/spec/spec_helper.rb +1 -1
- metadata +26 -40
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c6e724544450ac4f3893b2749024b3df5b1b94b2
|
4
|
+
data.tar.gz: cc15ac049b78e10fa470a691fdad447bd4300a96
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7aa8beb8c419ae20b5dc4ef9fbf10bb9685b4d5de5cbe63ee6e3af5e00ee08fd9a6e7c8befcc370ad2014cb9e8c6176547377a21199c22ab7d3b1c22788c7cac
|
7
|
+
data.tar.gz: 92cbfb349a5c6b3ebaf0fded01000b272571dca10db542dffef51b7513fa9ec5bff14fd3967015caa64a6df724bd7bd41b5308083c9812dedd7fe92c2d984aa8
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
active_serializer (0.1.
|
4
|
+
active_serializer (0.1.1)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -13,13 +13,11 @@ GEM
|
|
13
13
|
thread_safe (~> 0.1)
|
14
14
|
tzinfo (~> 0.3.37)
|
15
15
|
atomic (1.1.14)
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
debugger-ruby_core_source (~> 1.2.3)
|
16
|
+
byebug (3.1.2)
|
17
|
+
columnize (~> 0.8)
|
18
|
+
debugger-linecache (~> 1.2)
|
19
|
+
columnize (0.8.9)
|
21
20
|
debugger-linecache (1.2.0)
|
22
|
-
debugger-ruby_core_source (1.2.3)
|
23
21
|
diff-lcs (1.2.4)
|
24
22
|
i18n (0.6.5)
|
25
23
|
minitest (4.7.5)
|
@@ -44,6 +42,6 @@ DEPENDENCIES
|
|
44
42
|
active_serializer!
|
45
43
|
activesupport
|
46
44
|
bundler (~> 1.3)
|
47
|
-
|
45
|
+
byebug
|
48
46
|
rake
|
49
47
|
rspec
|
@@ -13,6 +13,21 @@ class ActiveSerializer::SerializationRulesValidator
|
|
13
13
|
ActiveSerializer::Support::ArgsValidator.is_symbol!(name, 'attributes')
|
14
14
|
end
|
15
15
|
|
16
|
+
def serialize_collection(*attrs)
|
17
|
+
key = attrs[0]
|
18
|
+
object = attrs[1]
|
19
|
+
klass = attrs[2]
|
20
|
+
ActiveSerializer::Support::ArgsValidator.is_symbol!(key, 'collection name')
|
21
|
+
ActiveSerializer::Support::ArgsValidator.is_class!(klass, 'serializer class')
|
22
|
+
end
|
23
|
+
|
24
|
+
def serialize_entity(*attrs)
|
25
|
+
key = attrs[0]
|
26
|
+
klass = attrs[2]
|
27
|
+
ActiveSerializer::Support::ArgsValidator.is_symbol!(key, 'collection name')
|
28
|
+
ActiveSerializer::Support::ArgsValidator.is_class!(klass, 'serializer class')
|
29
|
+
end
|
30
|
+
|
16
31
|
def attributes(*attrs)
|
17
32
|
attrs.delete_at(-1)
|
18
33
|
ActiveSerializer::Support::ArgsValidator.is_array_of_symbols!(attrs, 'attributes')
|
@@ -16,6 +16,19 @@ class ActiveSerializer::Serializers::HashSerializer
|
|
16
16
|
set_value(name, value)
|
17
17
|
end
|
18
18
|
|
19
|
+
def serialize_collection(name, objects, klass, *args)
|
20
|
+
raise ArgumentError, "You should provide serializer klass" if !klass
|
21
|
+
self.serialized_data[name] = []
|
22
|
+
objects.each do |object|
|
23
|
+
self.serialized_data[name] << klass.serialize(object, *args)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def serialize_entity(name, object, klass, *args)
|
28
|
+
raise ArgumentError, "You should provide serializer klass" if !klass
|
29
|
+
self.serialized_data[name] = klass.serialize(object, *args)
|
30
|
+
end
|
31
|
+
|
19
32
|
def attributes(*serialized_data, &block)
|
20
33
|
if !serialized_data.last.is_a?(Symbol)
|
21
34
|
source_hash = serialized_data.delete_at(-1)
|
@@ -6,6 +6,19 @@ class ActiveSerializer::Serializers::ObjectSerializer
|
|
6
6
|
@attrs = {}
|
7
7
|
end
|
8
8
|
|
9
|
+
def serialize_collection(name, objects, klass, *args)
|
10
|
+
raise ArgumentError, "You should provide serializer klass" if !klass
|
11
|
+
self.attrs[name] = []
|
12
|
+
objects.each do |object|
|
13
|
+
self.attrs[name] << klass.serialize(object, *args)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def serialize_entity(name, object, klass, *args)
|
18
|
+
raise ArgumentError, "You should provide serializer klass" if !klass
|
19
|
+
self.attrs[name] = klass.serialize(object, *args)
|
20
|
+
end
|
21
|
+
|
9
22
|
def namespace(name, &block)
|
10
23
|
serializer = self.class.new(@object, @options)
|
11
24
|
serializer.instance_exec(@object, &block)
|
@@ -6,6 +6,12 @@ module ActiveSerializer::Support::ArgsValidator
|
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
9
|
+
def is_class!(obj, obj_name)
|
10
|
+
unless obj.is_a?(Class)
|
11
|
+
raise ArgumentError, "#{obj_name} should be a Class"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
9
15
|
def is_array!(obj, obj_name)
|
10
16
|
unless obj.is_a?(Array)
|
11
17
|
raise ArgumentError, "#{obj_name} should be an Array"
|
@@ -2,16 +2,27 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
module SerializableHashTest
|
4
4
|
describe ActiveSerializer::SerializableHash do
|
5
|
+
class PhoneNumbersSerializer
|
6
|
+
include ActiveSerializer::SerializableHash
|
7
|
+
|
8
|
+
serialization_rules do |phone_numbers|
|
9
|
+
attributes :number
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
5
13
|
class ContactSerializer
|
6
14
|
include ActiveSerializer::SerializableHash
|
7
15
|
|
8
|
-
serialization_rules do |contact, home_address, contact_emails|
|
16
|
+
serialization_rules do |contact, home_address, contact_emails, phone_numbers|
|
9
17
|
attributes :first_name, :last_name, contact
|
10
18
|
|
11
19
|
attribute :full_name do
|
12
20
|
"#{contact[:first_name]} #{contact[:last_name]}"
|
13
21
|
end
|
14
22
|
|
23
|
+
serialize_collection :phone_numbers, phone_numbers, PhoneNumbersSerializer
|
24
|
+
serialize_entity :phone_number, phone_numbers.first, PhoneNumbersSerializer
|
25
|
+
|
15
26
|
resource :address, home_address do |address|
|
16
27
|
attributes :country, :city, :street, address
|
17
28
|
end
|
@@ -43,7 +54,13 @@ module SerializableHashTest
|
|
43
54
|
street: 'Kosmonavton',
|
44
55
|
}
|
45
56
|
|
46
|
-
|
57
|
+
phone_numbers = [
|
58
|
+
{
|
59
|
+
number: '123456'
|
60
|
+
}
|
61
|
+
]
|
62
|
+
|
63
|
+
serialized_contact = ContactSerializer.serialize(contact, home_address, contact_emails, phone_numbers)
|
47
64
|
serialized_contact.should == {
|
48
65
|
first_name: "John",
|
49
66
|
last_name: "Smith",
|
@@ -56,7 +73,15 @@ module SerializableHashTest
|
|
56
73
|
emails: [
|
57
74
|
{ email: "test@test.com", type: "home" },
|
58
75
|
{ email: "test2@test.com", type: "home" }
|
59
|
-
]
|
76
|
+
],
|
77
|
+
phone_numbers: [
|
78
|
+
{
|
79
|
+
number: '123456'
|
80
|
+
}
|
81
|
+
],
|
82
|
+
phone_number: {
|
83
|
+
number: '123456'
|
84
|
+
}
|
60
85
|
}
|
61
86
|
end
|
62
87
|
end
|
@@ -14,10 +14,22 @@ module SerializableObjectTest
|
|
14
14
|
attr_accessor :email
|
15
15
|
end
|
16
16
|
|
17
|
+
class PhoneNumber
|
18
|
+
attr_accessor :number
|
19
|
+
end
|
20
|
+
|
21
|
+
class PhoneNumbersSerializer
|
22
|
+
include ActiveSerializer::SerializableObject
|
23
|
+
|
24
|
+
serialization_rules do |phone_numbers|
|
25
|
+
attributes :number
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
17
29
|
class ContactSerializer
|
18
30
|
include ActiveSerializer::SerializableObject
|
19
31
|
|
20
|
-
serialization_rules do |contact, home_address, contact_emails|
|
32
|
+
serialization_rules do |contact, home_address, contact_emails, phone_numbers|
|
21
33
|
attributes :first_name, :last_name, contact
|
22
34
|
|
23
35
|
attribute :full_name do
|
@@ -28,6 +40,9 @@ module SerializableObjectTest
|
|
28
40
|
attributes :country, :city, :street, address
|
29
41
|
end
|
30
42
|
|
43
|
+
serialize_collection :phone_numbers, phone_numbers, PhoneNumbersSerializer
|
44
|
+
serialize_entity :phone_number, phone_numbers.first, PhoneNumbersSerializer
|
45
|
+
|
31
46
|
resources :emails, contact_emails do |email|
|
32
47
|
attributes :email
|
33
48
|
attribute :type do
|
@@ -54,7 +69,10 @@ module SerializableObjectTest
|
|
54
69
|
home_address.city = 'Kazan'
|
55
70
|
home_address.street = 'Kosmonavton'
|
56
71
|
|
57
|
-
|
72
|
+
phone_number = PhoneNumber.new
|
73
|
+
phone_number.number = '123456'
|
74
|
+
|
75
|
+
serialized_contact = ContactSerializer.serialize(contact, home_address, contact_emails, [phone_number])
|
58
76
|
serialized_contact.should == {
|
59
77
|
first_name: "John",
|
60
78
|
last_name: "Smith",
|
@@ -67,6 +85,14 @@ module SerializableObjectTest
|
|
67
85
|
emails: [
|
68
86
|
{ email: "test@test.com", type: "home" },
|
69
87
|
{ email: "test2@test.com", type: "home" }
|
88
|
+
],
|
89
|
+
phone_number: {
|
90
|
+
number: '123456'
|
91
|
+
},
|
92
|
+
phone_numbers: [
|
93
|
+
{
|
94
|
+
number: '123456'
|
95
|
+
}
|
70
96
|
]
|
71
97
|
}
|
72
98
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
version: 0.1.0
|
4
|
+
version: 0.1.1
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Albert Gazizov
|
@@ -10,56 +9,50 @@ authors:
|
|
10
9
|
autorequire:
|
11
10
|
bindir: bin
|
12
11
|
cert_chain: []
|
13
|
-
date: 2014-
|
12
|
+
date: 2014-05-25 00:00:00.000000000 Z
|
14
13
|
dependencies:
|
15
14
|
- !ruby/object:Gem::Dependency
|
16
|
-
prerelease: false
|
17
15
|
name: bundler
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
20
17
|
requirements:
|
21
|
-
- - ~>
|
18
|
+
- - "~>"
|
22
19
|
- !ruby/object:Gem::Version
|
23
20
|
version: '1.3'
|
24
|
-
|
25
|
-
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
24
|
requirements:
|
27
|
-
- - ~>
|
25
|
+
- - "~>"
|
28
26
|
- !ruby/object:Gem::Version
|
29
27
|
version: '1.3'
|
30
|
-
none: false
|
31
28
|
- !ruby/object:Gem::Dependency
|
32
|
-
prerelease: false
|
33
29
|
name: rake
|
34
|
-
|
35
|
-
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
36
31
|
requirements:
|
37
|
-
- -
|
32
|
+
- - ">="
|
38
33
|
- !ruby/object:Gem::Version
|
39
34
|
version: '0'
|
40
|
-
|
41
|
-
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
38
|
requirements:
|
43
|
-
- -
|
39
|
+
- - ">="
|
44
40
|
- !ruby/object:Gem::Version
|
45
41
|
version: '0'
|
46
|
-
none: false
|
47
42
|
- !ruby/object:Gem::Dependency
|
48
|
-
prerelease: false
|
49
43
|
name: activesupport
|
50
|
-
|
51
|
-
version_requirements: !ruby/object:Gem::Requirement
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
52
45
|
requirements:
|
53
|
-
- -
|
46
|
+
- - ">="
|
54
47
|
- !ruby/object:Gem::Version
|
55
48
|
version: '0'
|
56
|
-
|
57
|
-
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
52
|
requirements:
|
59
|
-
- -
|
53
|
+
- - ">="
|
60
54
|
- !ruby/object:Gem::Version
|
61
55
|
version: '0'
|
62
|
-
none: false
|
63
56
|
description: Object to Hash serializer
|
64
57
|
email:
|
65
58
|
- deeper4k@gmail.com
|
@@ -68,7 +61,7 @@ executables: []
|
|
68
61
|
extensions: []
|
69
62
|
extra_rdoc_files: []
|
70
63
|
files:
|
71
|
-
- .gitignore
|
64
|
+
- ".gitignore"
|
72
65
|
- Gemfile
|
73
66
|
- Gemfile.lock
|
74
67
|
- LICENSE.txt
|
@@ -94,33 +87,26 @@ files:
|
|
94
87
|
homepage: http://github.com/droidlabs/active_serializer
|
95
88
|
licenses:
|
96
89
|
- MIT
|
90
|
+
metadata: {}
|
97
91
|
post_install_message:
|
98
92
|
rdoc_options: []
|
99
93
|
require_paths:
|
100
94
|
- lib
|
101
95
|
required_ruby_version: !ruby/object:Gem::Requirement
|
102
96
|
requirements:
|
103
|
-
- -
|
97
|
+
- - ">="
|
104
98
|
- !ruby/object:Gem::Version
|
105
|
-
segments:
|
106
|
-
- 0
|
107
|
-
hash: 3700777157026269907
|
108
99
|
version: '0'
|
109
|
-
none: false
|
110
100
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
101
|
requirements:
|
112
|
-
- -
|
102
|
+
- - ">="
|
113
103
|
- !ruby/object:Gem::Version
|
114
|
-
segments:
|
115
|
-
- 0
|
116
|
-
hash: 3700777157026269907
|
117
104
|
version: '0'
|
118
|
-
none: false
|
119
105
|
requirements: []
|
120
106
|
rubyforge_project:
|
121
|
-
rubygems_version:
|
107
|
+
rubygems_version: 2.2.0
|
122
108
|
signing_key:
|
123
|
-
specification_version:
|
109
|
+
specification_version: 4
|
124
110
|
summary: Adds simple DSL to convert ruby objects to hash
|
125
111
|
test_files:
|
126
112
|
- spec/active_serializer/serializable_hash_spec.rb
|