representable 0.0.1 → 0.0.2
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.
- data/README.rdoc +1 -4
- data/lib/representable/bindings/json_bindings.rb +2 -2
- data/lib/representable/json.rb +11 -6
- data/lib/representable/version.rb +1 -1
- data/test/json_test.rb +10 -5
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -56,11 +56,8 @@ The cool thing with object-oriented representations is: you can treat them just
|
|
56
56
|
# ...
|
57
57
|
|
58
58
|
def sort_items
|
59
|
-
items.sort!
|
60
|
-
a.name <=> b.name
|
61
|
-
end
|
59
|
+
items.sort! { |a, b| a.name <=> b.name }
|
62
60
|
end
|
63
|
-
end
|
64
61
|
|
65
62
|
|
66
63
|
== Generating Representations
|
@@ -47,9 +47,9 @@ module Representable
|
|
47
47
|
|
48
48
|
def update_json(hash, value)
|
49
49
|
if array?
|
50
|
-
hash.merge! ({accessor => value.collect {|v| v.
|
50
|
+
hash.merge! ({accessor => value.collect {|v| v.to_hash(:wrap => false)}}) # hier name=> wech.
|
51
51
|
else
|
52
|
-
hash.merge! value.
|
52
|
+
hash.merge! value.to_hash
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
data/lib/representable/json.rb
CHANGED
@@ -28,7 +28,7 @@ module Representable
|
|
28
28
|
data = ::JSON[data] if data.is_a?(String) # DISCUSS: #from_json sometimes receives a string (in nestings).
|
29
29
|
data ||= {}
|
30
30
|
|
31
|
-
data = data[representation_name] unless options[:wrap] == false
|
31
|
+
data = data[representation_name.to_s] unless options[:wrap] == false
|
32
32
|
|
33
33
|
create_from_json.tap do |inst|
|
34
34
|
refs = representable_attrs.map {|attr| JSON.binding_for_definition(attr) }
|
@@ -48,18 +48,23 @@ module Representable
|
|
48
48
|
end
|
49
49
|
|
50
50
|
module InstanceMethods # :nodoc:
|
51
|
-
|
52
|
-
|
53
|
-
attributes = {}.tap do |root|
|
51
|
+
def to_hash(options={})
|
52
|
+
hash = {}.tap do |attrs|
|
54
53
|
refs = self.class.representable_attrs.map {|attr| JSON.binding_for_definition(attr) }
|
55
54
|
|
56
55
|
refs.each do |ref|
|
57
56
|
value = public_send(ref.accessor) # DISCUSS: eventually move back to Ref.
|
58
|
-
ref.update_json(
|
57
|
+
ref.update_json(attrs, value) if value
|
59
58
|
end
|
60
59
|
end
|
61
60
|
|
62
|
-
|
61
|
+
# DISCUSS: where to wrap?
|
62
|
+
options[:wrap] == false ? hash : {self.class.representation_name => hash}
|
63
|
+
end
|
64
|
+
|
65
|
+
# Returns a Nokogiri::XML object representing this object.
|
66
|
+
def to_json(options={})
|
67
|
+
to_hash(options).to_json
|
63
68
|
end
|
64
69
|
end
|
65
70
|
end # Xml
|
data/test/json_test.rb
CHANGED
@@ -30,12 +30,18 @@ module JsonTest
|
|
30
30
|
band = Band.from_json({:band => {:name => "Bombshell Rocks"}}.to_json)
|
31
31
|
assert_equal "Bombshell Rocks", band.name
|
32
32
|
end
|
33
|
+
|
34
|
+
it "#from_json accepts hash, too" do
|
35
|
+
band = Band.from_json({"band" => {"name" => "Bombshell Rocks"}})
|
36
|
+
assert_equal "Bombshell Rocks", band.name
|
37
|
+
end
|
38
|
+
|
33
39
|
|
34
40
|
it "#to_json serializes correctly" do
|
35
41
|
band = Band.new
|
36
42
|
band.name = "Cigar"
|
37
43
|
|
38
|
-
assert_equal
|
44
|
+
assert_equal '{"band":{"name":"Cigar"}}', band.to_json
|
39
45
|
end
|
40
46
|
end
|
41
47
|
|
@@ -54,7 +60,7 @@ module JsonTest
|
|
54
60
|
cd = CD.new
|
55
61
|
cd.songs = ["Out in the cold", "Microphone"]
|
56
62
|
|
57
|
-
assert_equal
|
63
|
+
assert_equal '{"cd":{"songs":["Out in the cold","Microphone"]}}', cd.to_json
|
58
64
|
end
|
59
65
|
end
|
60
66
|
end
|
@@ -81,7 +87,7 @@ module JsonTest
|
|
81
87
|
label = Label.new; label.name = "Fat Wreck"
|
82
88
|
album = Album.new; album.label = label
|
83
89
|
|
84
|
-
assert_equal
|
90
|
+
assert_equal '{"album":{"label":{"name":"Fat Wreck"}}}', album.to_json
|
85
91
|
end
|
86
92
|
end
|
87
93
|
end
|
@@ -122,9 +128,8 @@ module JsonTest
|
|
122
128
|
cd = Compilation.new
|
123
129
|
cd.bands = [Band.new("Diesel Boy"), Band.new("Bad Religion")]
|
124
130
|
|
125
|
-
assert_equal
|
131
|
+
assert_equal '{"compilation":{"bands":[{"name":"Diesel Boy"},{"name":"Bad Religion"}]}}', cd.to_json
|
126
132
|
end
|
127
133
|
end
|
128
|
-
|
129
134
|
end
|
130
135
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Nick Sutterer
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-
|
17
|
+
date: 2011-06-03 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|