representable 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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! do |a,b|
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.to_json(:wrap => false)}}) # hier name=> wech.
50
+ hash.merge! ({accessor => value.collect {|v| v.to_hash(:wrap => false)}}) # hier name=> wech.
51
51
  else
52
- hash.merge! value.to_json
52
+ hash.merge! value.to_hash
53
53
  end
54
54
  end
55
55
 
@@ -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
- # Returns a Nokogiri::XML object representing this object.
52
- def to_json(options={})
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(root, value) if value
57
+ ref.update_json(attrs, value) if value
59
58
  end
60
59
  end
61
60
 
62
- options[:wrap] == false ? attributes : {self.class.representation_name => attributes}
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
@@ -1,3 +1,3 @@
1
1
  module Representable
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -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 "{\"band\"=>{\"name\"=>\"Cigar\"}}", band.to_json.to_s
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 "{\"cd\"=>{\"songs\"=>[\"Out in the cold\", \"Microphone\"]}}", cd.to_json.to_s
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 "{\"album\"=>{\"label\"=>{\"name\"=>\"Fat Wreck\"}}}", album.to_json.to_s
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 "{\"compilation\"=>{\"bands\"=>[{\"name\"=>\"Diesel Boy\"}, {\"name\"=>\"Bad Religion\"}]}}", cd.to_json.to_s
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
- - 1
9
- version: 0.0.1
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-05-09 00:00:00 +02:00
17
+ date: 2011-06-03 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency