json-serializer 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -6
- data/json-serializer.gemspec +1 -1
- data/lib/json-serializer.rb +13 -13
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7c33873d247c185513bac389334e963d83dd5eba
|
4
|
+
data.tar.gz: 3557f4188f1c845095ba49da20910297c7030ab0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1a9025cf3c228ad212ed59ae5e4a085efa7142464d503d54d325b107ebc4be2301aea86fd1a5f71bc28c5267c4881e6897fb25b02126df1102d42ffcc767d964
|
7
|
+
data.tar.gz: aa9415bb56888fd40edde23551e40c7c7b4df38822dc60c87f316b26cafa209fbb83f6b9662536329b7a6d46fba5fb122516c61847d88b10b257beff5fbe6dde
|
data/README.md
CHANGED
@@ -106,7 +106,7 @@ UserSerializer.new(user).to_json
|
|
106
106
|
```
|
107
107
|
|
108
108
|
If you would like direct, low-level control of attribute serialization, you can
|
109
|
-
completely override
|
109
|
+
completely override `to_hash` method to return the hash you need:
|
110
110
|
|
111
111
|
```ruby
|
112
112
|
require "json_serializer"
|
@@ -116,16 +116,16 @@ class UserSerializer < JsonSerializer
|
|
116
116
|
attribute :first_name
|
117
117
|
attribute :last_name
|
118
118
|
|
119
|
-
attr :
|
119
|
+
attr :current_user
|
120
120
|
|
121
|
-
def initialize(object,
|
121
|
+
def initialize(object, current_user)
|
122
122
|
super(object)
|
123
|
-
@
|
123
|
+
@current_user = current_user
|
124
124
|
end
|
125
125
|
|
126
|
-
def
|
126
|
+
def to_hash
|
127
127
|
hash = super
|
128
|
-
hash.merge!(admin: object.admin) if
|
128
|
+
hash.merge!(admin: object.admin) if current_user.admin?
|
129
129
|
hash
|
130
130
|
end
|
131
131
|
end
|
data/json-serializer.gemspec
CHANGED
data/lib/json-serializer.rb
CHANGED
@@ -1,6 +1,16 @@
|
|
1
1
|
require "json"
|
2
2
|
|
3
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
|
+
|
4
14
|
def self.attribute(name, serializer = nil)
|
5
15
|
attributes[name] ||= serializer
|
6
16
|
end
|
@@ -27,27 +37,17 @@ class JsonSerializer
|
|
27
37
|
|
28
38
|
def serializable_object
|
29
39
|
if object.kind_of?(Enumerable)
|
30
|
-
object.to_a.map { |item| self.class.new(item).
|
40
|
+
object.to_a.map { |item| self.class.new(item).to_hash }
|
31
41
|
else
|
32
|
-
|
42
|
+
to_hash
|
33
43
|
end
|
34
44
|
end
|
35
45
|
|
36
|
-
def
|
46
|
+
def to_hash
|
37
47
|
self.class.attributes.each_with_object({}) do |(name, serializer), hash|
|
38
48
|
data = self.class.method_defined?(name) ? self.send(name) : object.send(name)
|
39
49
|
data = Utils.const(self.class, serializer).new(data).serializable_object if serializer
|
40
50
|
hash[name] = data
|
41
51
|
end
|
42
52
|
end
|
43
|
-
|
44
|
-
module Utils
|
45
|
-
def self.const(context, name)
|
46
|
-
case name
|
47
|
-
when Symbol, String
|
48
|
-
context.const_get(name)
|
49
|
-
else name
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
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.6
|
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-01-
|
11
|
+
date: 2014-01-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: cutest
|