serializable_decorator 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -0
- data/lib/serializable_decorator.rb +11 -1
- data/lib/serializable_decorator/version.rb +1 -1
- data/spec/serializable_decorator_spec.rb +16 -0
- metadata +2 -2
data/Gemfile
CHANGED
@@ -44,7 +44,17 @@ class SerializableDecorator < Delegator
|
|
44
44
|
__getobj__.instance_variable_get(name) || super
|
45
45
|
end
|
46
46
|
|
47
|
+
def as_json
|
48
|
+
if __getobj__.respond_to?(:as_json) then
|
49
|
+
delegator_instance_vars = Delegator.instance_method(:instance_variables).bind(self).call
|
50
|
+
json_hash = Hash[delegator_instance_vars.map { | name | [name.to_s[1..-1], instance_variable_get(name) ] } ]
|
51
|
+
json_hash.merge!(__getobj__.as_json)
|
52
|
+
else
|
53
|
+
instance_values
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
47
57
|
def to_json
|
48
|
-
|
58
|
+
as_json.to_json
|
49
59
|
end
|
50
60
|
end
|
@@ -4,6 +4,13 @@ class Test
|
|
4
4
|
attr_accessor :foo
|
5
5
|
end
|
6
6
|
|
7
|
+
class TestJson < Test
|
8
|
+
attr_accessor :quux
|
9
|
+
def as_json
|
10
|
+
{ extra: "baz", foo: @foo }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
7
14
|
class TestDecorator < SerializableDecorator
|
8
15
|
attr_accessor :bar
|
9
16
|
end
|
@@ -11,6 +18,7 @@ end
|
|
11
18
|
describe SerializableDecorator do
|
12
19
|
before(:each) do
|
13
20
|
@td = TestDecorator.new(Test.new)
|
21
|
+
@tdj = TestDecorator.new(TestJson.new)
|
14
22
|
end
|
15
23
|
|
16
24
|
it "Original attributes are still available" do
|
@@ -75,4 +83,12 @@ describe SerializableDecorator do
|
|
75
83
|
it "Should not include any extraneous entries in the JSON output" do
|
76
84
|
@td.to_json.should == "{}"
|
77
85
|
end
|
86
|
+
|
87
|
+
it "Should use the decorated object's as_json if it exists" do
|
88
|
+
@tdj.foo = 12
|
89
|
+
@tdj.bar = 13
|
90
|
+
@tdj.to_json.should include('"foo":12')
|
91
|
+
@tdj.to_json.should include('"bar":13')
|
92
|
+
@tdj.to_json.should include('"extra":"baz"')
|
93
|
+
end
|
78
94
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: serializable_decorator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-07-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|