json-serializer 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 39bc95ac73ac44ae7a16dc13bf43d52c48499f97
4
+ data.tar.gz: 906faca4908b5c2b5a5a780d741f1e8d95201105
5
+ SHA512:
6
+ metadata.gz: a4bd52faad782b9ade7ef4900547b89d902d3f979f06aa3e5ffce6feae99631b3ff8a3019eb226122057dfc6010f9da7f168756fdc1ba980a57e488fe677866a
7
+ data.tar.gz: ba0673eb2adbf64b68fb1e65b64f83522a74fcbaee38b1d33025acf736f737c7c3bd195393bafbc494c48dfc5ac94b7625b4e0c0ac73635d484a3de88e73982d
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2013 Francesco Rodríguez
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,6 @@
1
+ json-serializer
2
+ ===============
3
+
4
+ `ActiveModel::Serializers` outside Rails.
5
+
6
+ Not ready.
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'json-serializer'
5
+ s.version = '0.0.1'
6
+ s.summary = 'Replaces hash-driven development with object-oriented development'
7
+ s.description = s.summary
8
+ s.authors = ['Francesco Rodríguez']
9
+ s.email = ['lrodriguezsanc@gmail.com']
10
+ s.homepage = 'https://github.com/frodsan/mocoso'
11
+ s.license = 'MIT'
12
+
13
+ s.files = Dir[
14
+ 'LICENSE',
15
+ 'README.md',
16
+ 'lib/**/*.rb',
17
+ '*.gemspec',
18
+ 'test/*.*'
19
+ ]
20
+
21
+ s.add_development_dependency 'cutest'
22
+ end
@@ -0,0 +1,86 @@
1
+ require 'json'
2
+
3
+ class JsonSerializer
4
+ # Specify which attributes you would like to include in the outputted JSON.
5
+ #
6
+ # class UserSerializer < JsonSerializer
7
+ # attribute :id
8
+ # attribute :username
9
+ # end
10
+ #
11
+ # user = User.create username: 'skrillex', admin: true
12
+ #
13
+ # UserSerializer.new(user).to_json
14
+ # # => {"id":1,"username":"skrillex"}
15
+ #
16
+ # By default, before looking up the attribute on the object, checks the presence
17
+ # of a method with the attribute name. This allow serializers to include properties
18
+ # in addition to the object attributes or customize the result of a specified
19
+ # attribute. You can access the object being serialized with the +object+ method.
20
+ #
21
+ # class UserSerializer < JsonSerializer
22
+ # attribute :id
23
+ # attribute :username
24
+ # attribute :full_name
25
+ #
26
+ # def full_name
27
+ # object.first_name + ' ' + object.last_name
28
+ # end
29
+ # end
30
+ #
31
+ # user = User.create username: 'skrillex', first_name: 'sonny', last_name: 'moore'
32
+ #
33
+ # UserSerializer.new(user).to_json
34
+ # # => {"id":1,"username":"skrillex","full_name":"sonny moore"}
35
+ #
36
+ def self.attribute name, serializer = nil
37
+ attributes[name] ||= serializer
38
+ end
39
+
40
+ # Return an array with the specified attributes by +attribute+.
41
+ #
42
+ # class UserSerializer < JsonSerializer
43
+ # attribute :id
44
+ # attribute :username
45
+ # attribute :github
46
+ # end
47
+ #
48
+ # UserSerializer.attributes
49
+ # # => [:id, :username, :github]
50
+ #
51
+ def self.attributes
52
+ @attributes ||= {}
53
+ end
54
+
55
+ attr :object
56
+
57
+ def initialize object
58
+ @object = object
59
+ end
60
+
61
+ def to_json options={}
62
+ if root = options[:root]
63
+ { root => serializable_object }.to_json
64
+ else
65
+ serializable_object.to_json
66
+ end
67
+ end
68
+
69
+ protected
70
+
71
+ def serializable_object # :nodoc:
72
+ if object.respond_to?(:to_ary)
73
+ object.to_ary.map { |item| self.class.new(item).attributes }
74
+ else
75
+ attributes
76
+ end
77
+ end
78
+
79
+ def attributes # :nodoc:
80
+ self.class.attributes.each_with_object({}) do |(name, serializer), hash|
81
+ data = self.class.method_defined?(name) ? self.send(name) : object.send(name)
82
+ data = serializer.new(data).serializable_object if serializer
83
+ hash[name] = data
84
+ end
85
+ end
86
+ end
@@ -0,0 +1 @@
1
+ require 'json-serializer'
@@ -0,0 +1,230 @@
1
+ require 'cutest'
2
+ require_relative '../lib/json-serializer'
3
+
4
+ Post = Struct.new :id, :title, :created_at
5
+
6
+ class PostSerializer < JsonSerializer
7
+ attribute :id
8
+ attribute :title
9
+ attribute :slug
10
+
11
+ def slug
12
+ "#{ object.id }-#{ object.title }"
13
+ end
14
+ end
15
+
16
+ test 'converts defined attributes into json' do
17
+ post = Post.new 1, 'tsunami'
18
+
19
+ result = {
20
+ id: 1,
21
+ title: 'tsunami',
22
+ slug: '1-tsunami'
23
+ }
24
+
25
+ assert_equal result.to_json, PostSerializer.new(post).to_json
26
+ end
27
+
28
+ class User
29
+ attr_accessor :id, :name, :lastname, :organization, :organizations
30
+
31
+ def initialize attrs={}
32
+ attrs.each do |name, value|
33
+ send "#{name}=", value
34
+ end
35
+ end
36
+ end
37
+
38
+ class UserSerializer < JsonSerializer
39
+ attribute :id
40
+ attribute :fullname
41
+
42
+ def fullname
43
+ object.name + ' ' + object.lastname
44
+ end
45
+ end
46
+
47
+ test 'serializes array' do
48
+ users = [
49
+ User.new(id: 1, name: 'sonny', lastname: 'moore'),
50
+ User.new(id: 2, name: 'anton', lastname: 'zaslavski')
51
+ ]
52
+
53
+ result = [
54
+ { id: 1, fullname: 'sonny moore' },
55
+ { id: 2, fullname: 'anton zaslavski' }
56
+ ].to_json
57
+
58
+ assert_equal result, UserSerializer.new(users).to_json
59
+ end
60
+
61
+ Organization = Struct.new :id, :name, :created_at
62
+
63
+ class OrganizationSerializer < JsonSerializer
64
+ attribute :id
65
+ attribute :name
66
+ end
67
+
68
+ class UserWithOrganizationSerializer < JsonSerializer
69
+ attribute :id
70
+ attribute :name
71
+ attribute :organization, OrganizationSerializer
72
+ end
73
+
74
+ test 'serializes object with association' do
75
+ user = User.new id: 1, name: 'sonny'
76
+ user.organization = Organization.new 1, 'enterprise'
77
+
78
+ result = {
79
+ id: 1,
80
+ name: 'sonny',
81
+ organization: {
82
+ id: 1,
83
+ name: 'enterprise'
84
+ }
85
+ }.to_json
86
+
87
+ assert_equal result, UserWithOrganizationSerializer.new(user).to_json
88
+ end
89
+
90
+ test 'serializes array with association' do
91
+ users = [
92
+ User.new(id: 1, name: 'sonny', organization: Organization.new(1, 'enterprise')),
93
+ User.new(id: 2, name: 'anton', organization: Organization.new(2, 'evil'))
94
+ ]
95
+
96
+ result = [
97
+ {
98
+ id: 1,
99
+ name: 'sonny',
100
+ organization: {
101
+ id: 1,
102
+ name: 'enterprise'
103
+ }
104
+ },
105
+ {
106
+ id: 2,
107
+ name: 'anton',
108
+ organization: {
109
+ id: 2,
110
+ name: 'evil'
111
+ }
112
+ }
113
+ ].to_json
114
+
115
+ assert_equal result, UserWithOrganizationSerializer.new(users).to_json
116
+ end
117
+
118
+ class UserWithOrganizationsSerializer < JsonSerializer
119
+ attribute :id
120
+ attribute :name
121
+ attribute :organizations, OrganizationSerializer
122
+ end
123
+
124
+ test 'serializes object with collection' do
125
+ user = User.new id: 1, name: 'sonny'
126
+ user.organizations = [
127
+ Organization.new(1, 'enterprise'),
128
+ Organization.new(2, 'evil')
129
+ ]
130
+
131
+ result = {
132
+ id: 1,
133
+ name: 'sonny',
134
+ organizations: [
135
+ {
136
+ id: 1,
137
+ name: 'enterprise'
138
+ },
139
+ {
140
+ id: 2,
141
+ name: 'evil'
142
+ }
143
+ ]
144
+ }.to_json
145
+
146
+ assert_equal result, UserWithOrganizationsSerializer.new(user).to_json
147
+ end
148
+
149
+ test 'serializes array with nested collections' do
150
+ users = [
151
+ User.new(
152
+ id: 1,
153
+ name: 'sonny',
154
+ organizations: [
155
+ Organization.new(1, 'enterprise'),
156
+ Organization.new(2, 'evil'),
157
+ ]
158
+ ),
159
+ User.new(
160
+ id: 2,
161
+ name: 'anton',
162
+ organizations: [
163
+ Organization.new(3, 'showtek')
164
+ ]
165
+ )
166
+ ]
167
+
168
+ result = [
169
+ {
170
+ id: 1,
171
+ name: 'sonny',
172
+ organizations: [
173
+ {
174
+ id: 1,
175
+ name: 'enterprise'
176
+ },
177
+ {
178
+ id: 2,
179
+ name: 'evil'
180
+ }
181
+ ]
182
+ },
183
+ {
184
+ id: 2,
185
+ name: 'anton',
186
+ organizations: [
187
+ {
188
+ id: 3,
189
+ name: 'showtek'
190
+ }
191
+ ]
192
+ }
193
+ ].to_json
194
+
195
+ assert_equal result, UserWithOrganizationsSerializer.new(users).to_json
196
+ end
197
+
198
+ class UserWithCustomOrganizationSerializer < JsonSerializer
199
+ attribute :organizations, OrganizationSerializer
200
+
201
+ def organizations
202
+ [ Organization.new(1, 'enterprise') ]
203
+ end
204
+ end
205
+
206
+ test 'implements association method and returns different result' do
207
+ user = User.new
208
+
209
+ result = { organizations: [ { id: 1, name: 'enterprise' } ] }.to_json
210
+
211
+ assert_equal result, UserWithCustomOrganizationSerializer.new(user).to_json
212
+ end
213
+
214
+ Person = Struct.new :name
215
+
216
+ class PersonSerialize < JsonSerializer
217
+ attribute :name
218
+ end
219
+
220
+ test 'allows root option' do
221
+ person = Person.new 'sonny'
222
+
223
+ result = { person: person.to_h }.to_json
224
+
225
+ assert_equal result, PersonSerialize.new(person).to_json(root: :person)
226
+
227
+ result = { people: [person.to_h] }.to_json
228
+
229
+ assert_equal result, PersonSerialize.new([person]).to_json(root: :people)
230
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json-serializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Francesco Rodríguez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cutest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Replaces hash-driven development with object-oriented development
28
+ email:
29
+ - lrodriguezsanc@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - lib/json-serializer.rb
37
+ - lib/json_serializer.rb
38
+ - json-serializer.gemspec
39
+ - test/json_serializer_test.rb
40
+ homepage: https://github.com/frodsan/mocoso
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 2.0.3
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Replaces hash-driven development with object-oriented development
64
+ test_files: []