serialize_variants 0.0.7 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -1
- data/Rakefile +1 -1
- data/lib/serialize_variants.rb +10 -12
- metadata +2 -2
data/README.md
CHANGED
@@ -10,11 +10,13 @@ Example
|
|
10
10
|
|
11
11
|
In your model file:
|
12
12
|
|
13
|
-
|
13
|
+
serialize_options :short, :only => [:id, :name]
|
14
|
+
serialize_options :long, :except => [:hidden]
|
14
15
|
|
15
16
|
Anywhere, but typically in your controller file:
|
16
17
|
|
17
18
|
render :json => my_object.to_json(:variant => :short)
|
19
|
+
my_hash = my_object.as_json(:variant => :long)
|
18
20
|
|
19
21
|
|
20
22
|
|
data/Rakefile
CHANGED
data/lib/serialize_variants.rb
CHANGED
@@ -3,13 +3,13 @@ require "active_record"
|
|
3
3
|
module SerializeVariants
|
4
4
|
module Variants
|
5
5
|
module ClassMethods
|
6
|
-
def
|
7
|
-
@
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
@
|
6
|
+
def serialize_variant(variant, options=nil)
|
7
|
+
@serialize_variant ||= {}
|
8
|
+
if options
|
9
|
+
options[:variant] = variant
|
10
|
+
@serialize_variant[variant] = options
|
11
|
+
end
|
12
|
+
@serialize_variant[variant]
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
@@ -22,11 +22,9 @@ module SerializeVariants
|
|
22
22
|
variant = options[:variant]
|
23
23
|
force = options[:force] || {}
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
super((self.class.
|
28
|
-
when :long
|
29
|
-
super((self.class.long_serialize_options || {}).merge(force))
|
25
|
+
if options[:variant]
|
26
|
+
puts self.class.serialize_variant(options[:variant])
|
27
|
+
super((self.class.serialize_variant(options[:variant]) || {}).merge(force))
|
30
28
|
else
|
31
29
|
super(options)
|
32
30
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: serialize_variants
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.1.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Nicolas Goy
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-05-
|
13
|
+
date: 2011-05-31 00:00:00 +02:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|