json-serializer 0.0.6 → 0.0.7
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/LICENSE +19 -0
- data/json-serializer.gemspec +2 -2
- data/lib/json_serializer.rb +53 -1
- metadata +4 -5
- data/UNLICENSE +0 -24
- data/lib/json-serializer.rb +0 -53
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 26c95a974c36b11abb9169a072c8f2c5a0a41d7b
|
4
|
+
data.tar.gz: 97ef2774077f9908fd36df3d52a8a890504badf7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d0eb9817d4363e86884020dc2981033554774eef02445fa5f6b54ebd096d03c63e9637f0963b432cf11a43d59a10e5e33786afac673a445538133006cf29f007
|
7
|
+
data.tar.gz: 633d1948f1354bf206ae48b590edd7e47bdfbe740fd0f2148be58729706f9a08c0bc29adacc6f43c856cce458c98755fdcedca4fd1f459924e2f19458a09a68f
|
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2014 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.
|
data/json-serializer.gemspec
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "json-serializer"
|
3
|
-
s.version = "0.0.
|
3
|
+
s.version = "0.0.7"
|
4
4
|
s.summary = "Customize JSON ouput through serializer objects."
|
5
5
|
s.description = s.summary
|
6
6
|
s.authors = ["Francesco Rodríguez"]
|
7
7
|
s.email = ["frodsan@me.com"]
|
8
8
|
s.homepage = "https://github.com/frodsan/json-serializer"
|
9
|
-
s.license = "
|
9
|
+
s.license = "MIT"
|
10
10
|
|
11
11
|
s.files = `git ls-files`.split("\n")
|
12
12
|
|
data/lib/json_serializer.rb
CHANGED
@@ -1 +1,53 @@
|
|
1
|
-
|
1
|
+
require "json"
|
2
|
+
|
3
|
+
class JsonSerializer
|
4
|
+
module Utils
|
5
|
+
def self.const(context, name)
|
6
|
+
case name
|
7
|
+
when Symbol, String
|
8
|
+
context.const_get(name)
|
9
|
+
else name
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.attribute(name, serializer = nil)
|
15
|
+
attributes[name] ||= serializer
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.attributes
|
19
|
+
@attributes ||= {}
|
20
|
+
end
|
21
|
+
|
22
|
+
attr :object
|
23
|
+
|
24
|
+
def initialize(object)
|
25
|
+
@object = object
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_json(options={})
|
29
|
+
if root = options[:root]
|
30
|
+
{ root => serializable_object }.to_json
|
31
|
+
else
|
32
|
+
serializable_object.to_json
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
protected
|
37
|
+
|
38
|
+
def serializable_object
|
39
|
+
if object.kind_of?(Enumerable)
|
40
|
+
object.to_a.map { |item| self.class.new(item).to_hash }
|
41
|
+
else
|
42
|
+
to_hash
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_hash
|
47
|
+
self.class.attributes.each_with_object({}) do |(name, serializer), hash|
|
48
|
+
data = self.class.method_defined?(name) ? self.send(name) : object.send(name)
|
49
|
+
data = Utils.const(self.class, serializer).new(data).serializable_object if serializer
|
50
|
+
hash[name] = data
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json-serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Francesco Rodríguez
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-03-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: cutest
|
@@ -33,10 +33,9 @@ extra_rdoc_files: []
|
|
33
33
|
files:
|
34
34
|
- ".gems"
|
35
35
|
- ".gitignore"
|
36
|
+
- LICENSE
|
36
37
|
- README.md
|
37
|
-
- UNLICENSE
|
38
38
|
- json-serializer.gemspec
|
39
|
-
- lib/json-serializer.rb
|
40
39
|
- lib/json_serializer.rb
|
41
40
|
- makefile
|
42
41
|
- test/association.rb
|
@@ -45,7 +44,7 @@ files:
|
|
45
44
|
- test/root.rb
|
46
45
|
homepage: https://github.com/frodsan/json-serializer
|
47
46
|
licenses:
|
48
|
-
-
|
47
|
+
- MIT
|
49
48
|
metadata: {}
|
50
49
|
post_install_message:
|
51
50
|
rdoc_options: []
|
data/UNLICENSE
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
This is free and unencumbered software released into the public domain.
|
2
|
-
|
3
|
-
Anyone is free to copy, modify, publish, use, compile, sell, or
|
4
|
-
distribute this software, either in source code form or as a compiled
|
5
|
-
binary, for any purpose, commercial or non-commercial, and by any
|
6
|
-
means.
|
7
|
-
|
8
|
-
In jurisdictions that recognize copyright laws, the author or authors
|
9
|
-
of this software dedicate any and all copyright interest in the
|
10
|
-
software to the public domain. We make this dedication for the benefit
|
11
|
-
of the public at large and to the detriment of our heirs and
|
12
|
-
successors. We intend this dedication to be an overt act of
|
13
|
-
relinquishment in perpetuity of all present and future rights to this
|
14
|
-
software under copyright law.
|
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 NONINFRINGEMENT.
|
19
|
-
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
20
|
-
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
21
|
-
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
-
OTHER DEALINGS IN THE SOFTWARE.
|
23
|
-
|
24
|
-
For more information, please refer to <http://unlicense.org/>
|
data/lib/json-serializer.rb
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
require "json"
|
2
|
-
|
3
|
-
class JsonSerializer
|
4
|
-
module Utils
|
5
|
-
def self.const(context, name)
|
6
|
-
case name
|
7
|
-
when Symbol, String
|
8
|
-
context.const_get(name)
|
9
|
-
else name
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.attribute(name, serializer = nil)
|
15
|
-
attributes[name] ||= serializer
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.attributes
|
19
|
-
@attributes ||= {}
|
20
|
-
end
|
21
|
-
|
22
|
-
attr :object
|
23
|
-
|
24
|
-
def initialize(object)
|
25
|
-
@object = object
|
26
|
-
end
|
27
|
-
|
28
|
-
def to_json(options={})
|
29
|
-
if root = options[:root]
|
30
|
-
{ root => serializable_object }.to_json
|
31
|
-
else
|
32
|
-
serializable_object.to_json
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
protected
|
37
|
-
|
38
|
-
def serializable_object
|
39
|
-
if object.kind_of?(Enumerable)
|
40
|
-
object.to_a.map { |item| self.class.new(item).to_hash }
|
41
|
-
else
|
42
|
-
to_hash
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def to_hash
|
47
|
-
self.class.attributes.each_with_object({}) do |(name, serializer), hash|
|
48
|
-
data = self.class.method_defined?(name) ? self.send(name) : object.send(name)
|
49
|
-
data = Utils.const(self.class, serializer).new(data).serializable_object if serializer
|
50
|
-
hash[name] = data
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|