representable 1.3.0 → 1.3.1
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/CHANGES.textile +4 -0
- data/README.md +1 -3
- data/lib/representable/definition.rb +1 -1
- data/lib/representable/version.rb +1 -1
- data/test/coercion_test.rb +0 -4
- data/test/json_test.rb +38 -14
- data/test/representable_test.rb +5 -0
- metadata +2 -2
data/CHANGES.textile
CHANGED
data/README.md
CHANGED
@@ -454,8 +454,7 @@ Since representable-1.2 `false` values _are_ considered when parsing and renderi
|
|
454
454
|
|
455
455
|
If you want `nil` values to be included when rendering, use the `:render_nil` option.
|
456
456
|
|
457
|
-
|
458
|
-
|
457
|
+
property :track, render_nil: true
|
459
458
|
|
460
459
|
## Coercion
|
461
460
|
|
@@ -469,7 +468,6 @@ Use the `:type` option to specify the conversion target. Note that `:default` st
|
|
469
468
|
|
470
469
|
module SongRepresenter
|
471
470
|
include Representable::JSON
|
472
|
-
include Virtus
|
473
471
|
include Representable::Coercion
|
474
472
|
|
475
473
|
property :title
|
data/test/coercion_test.rb
CHANGED
@@ -25,9 +25,7 @@ class VirtusCoercionTest < MiniTest::Spec
|
|
25
25
|
|
26
26
|
describe "on class level" do
|
27
27
|
class ImmigrantSong
|
28
|
-
attr_accessor :track
|
29
28
|
include Representable::JSON
|
30
|
-
include Virtus
|
31
29
|
include Representable::Coercion
|
32
30
|
|
33
31
|
property :composed_at, :type => DateTime, :default => "May 12th, 2012"
|
@@ -35,8 +33,6 @@ class VirtusCoercionTest < MiniTest::Spec
|
|
35
33
|
end
|
36
34
|
|
37
35
|
it "coerces into the provided type" do
|
38
|
-
skip "Virtus is still not correctly treating coercion on class level?"
|
39
|
-
|
40
36
|
song = ImmigrantSong.new.from_json("{\"composed_at\":\"November 18th, 1983\",\"track\":\"18\"}")
|
41
37
|
assert_equal DateTime.parse("Fri, 18 Nov 1983 00:00:00 +0000"), song.composed_at
|
42
38
|
assert_equal 18, song.track
|
data/test/json_test.rb
CHANGED
@@ -286,6 +286,24 @@ module JsonTest
|
|
286
286
|
end
|
287
287
|
end
|
288
288
|
|
289
|
+
describe ":as => :songName" do
|
290
|
+
class Song
|
291
|
+
include Representable::JSON
|
292
|
+
property :name, :as => :songName
|
293
|
+
attr_accessor :name
|
294
|
+
end
|
295
|
+
|
296
|
+
it "respects :as in #from_json" do
|
297
|
+
song = Song.from_json({:songName => "Run To The Hills"}.to_json)
|
298
|
+
assert_equal "Run To The Hills", song.name
|
299
|
+
end
|
300
|
+
|
301
|
+
it "respects :as in #to_json" do
|
302
|
+
song = Song.new; song.name = "22 Acacia Avenue"
|
303
|
+
assert_json '{"songName":"22 Acacia Avenue"}', song.to_json
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
289
307
|
describe ":default => :value" do
|
290
308
|
before do
|
291
309
|
@Album = Class.new do
|
@@ -417,29 +435,35 @@ end
|
|
417
435
|
|
418
436
|
class HashTest < MiniTest::Spec
|
419
437
|
describe "hash :songs" do
|
420
|
-
|
421
|
-
|
422
|
-
include Representable::JSON
|
423
|
-
hash :songs
|
424
|
-
end
|
425
|
-
|
426
|
-
class SongList
|
427
|
-
attr_accessor :songs
|
428
|
-
end
|
429
|
-
|
430
|
-
@list = SongList.new.extend(representer)
|
438
|
+
representer!(Representable::JSON) do
|
439
|
+
hash :songs
|
431
440
|
end
|
432
441
|
|
442
|
+
subject { OpenStruct.new.extend(representer) }
|
443
|
+
|
433
444
|
it "renders with #to_json" do
|
434
|
-
|
435
|
-
assert_json "{\"songs\":{\"one\":\"65\",\"two\":\"Emo Boy\"}}",
|
445
|
+
subject.songs = {:one => "65", :two => "Emo Boy"}
|
446
|
+
assert_json "{\"songs\":{\"one\":\"65\",\"two\":\"Emo Boy\"}}", subject.to_json
|
436
447
|
end
|
437
448
|
|
438
449
|
it "parses with #from_json" do
|
439
|
-
assert_equal({"one" => "65", "two" => ["Emo Boy"]},
|
450
|
+
assert_equal({"one" => "65", "two" => ["Emo Boy"]}, subject.from_json("{\"songs\":{\"one\":\"65\",\"two\":[\"Emo Boy\"]}}").songs)
|
440
451
|
end
|
441
452
|
end
|
442
453
|
|
454
|
+
describe "hash :songs, :class => Song" do
|
455
|
+
representer!(Representable::JSON) do
|
456
|
+
hash :songs, :extend => Module.new { include Representable::JSON; property :name }, :class => Song
|
457
|
+
end
|
458
|
+
|
459
|
+
it "renders" do
|
460
|
+
OpenStruct.new(:songs => {"7" => Song.new("Contemplation")}).extend(representer).to_hash.must_equal("songs"=>{"7"=>{"name"=>"Contemplation"}})
|
461
|
+
end
|
462
|
+
|
463
|
+
it "parses" do
|
464
|
+
OpenStruct.new.extend(representer).from_hash("songs"=>{"7"=>{"name"=>"Contemplation"}}).songs.must_equal({"7"=>Song.new("Contemplation")})
|
465
|
+
end
|
466
|
+
end
|
443
467
|
end
|
444
468
|
|
445
469
|
|
data/test/representable_test.rb
CHANGED
@@ -161,6 +161,11 @@ class RepresentableTest < MiniTest::Spec
|
|
161
161
|
band = Class.new(Band) { property :friends, :from => :friend }
|
162
162
|
assert_equal "friend", band.representable_attrs.last.from
|
163
163
|
end
|
164
|
+
|
165
|
+
it "can be set explicitly with as" do
|
166
|
+
band = Class.new(Band) { property :friends, :as => :friend }
|
167
|
+
assert_equal "friend", band.representable_attrs.last.from
|
168
|
+
end
|
164
169
|
|
165
170
|
it "is infered from the name implicitly" do
|
166
171
|
band = Class.new(Band) { property :friends }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: representable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.1
|
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: 2013-01-
|
12
|
+
date: 2013-01-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|