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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 978039384f8b314761d4a78456e337761ea7f515
4
- data.tar.gz: 04ca4c46104b2ab4410c60944826867fe373b560
3
+ metadata.gz: 7c33873d247c185513bac389334e963d83dd5eba
4
+ data.tar.gz: 3557f4188f1c845095ba49da20910297c7030ab0
5
5
  SHA512:
6
- metadata.gz: cf9e02f18a0fb1cb9e22c6d4ffaae6b8f0be6544ba1ad73ccda6d0c6edc1f86826346c660d998a0e901c46677cf8213fac1cb448fd82b444013ce76dab93a87f
7
- data.tar.gz: ab3f1afec9000c07267ee6176c786f5c42fbc017148a7f6bc9034b3041ecc766f97be72b89c61ee62df1d0b080494a1eb57d54004e62467c439f80ae25c7edf7
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 the attributes method to return the hash you need:
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 :scope
119
+ attr :current_user
120
120
 
121
- def initialize(object, scope)
121
+ def initialize(object, current_user)
122
122
  super(object)
123
- @scope = scope
123
+ @current_user = current_user
124
124
  end
125
125
 
126
- def attributes
126
+ def to_hash
127
127
  hash = super
128
- hash.merge!(admin: object.admin) if scope.admin?
128
+ hash.merge!(admin: object.admin) if current_user.admin?
129
129
  hash
130
130
  end
131
131
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "json-serializer"
3
- s.version = "0.0.5"
3
+ s.version = "0.0.6"
4
4
  s.summary = "Customize JSON ouput through serializer objects."
5
5
  s.description = s.summary
6
6
  s.authors = ["Francesco Rodríguez"]
@@ -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).attributes }
40
+ object.to_a.map { |item| self.class.new(item).to_hash }
31
41
  else
32
- attributes
42
+ to_hash
33
43
  end
34
44
  end
35
45
 
36
- def attributes
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.5
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-20 00:00:00.000000000 Z
11
+ date: 2014-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cutest