representable 3.0.4 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/ci.yml +17 -0
  3. data/CHANGES.md +6 -0
  4. data/Gemfile +4 -7
  5. data/LICENSE +1 -1
  6. data/README.md +4 -3
  7. data/Rakefile +1 -6
  8. data/lib/representable.rb +18 -25
  9. data/lib/representable/binding.rb +32 -12
  10. data/lib/representable/cached.rb +1 -1
  11. data/lib/representable/coercion.rb +8 -6
  12. data/lib/representable/config.rb +8 -3
  13. data/lib/representable/debug.rb +23 -15
  14. data/lib/representable/declarative.rb +8 -3
  15. data/lib/representable/decorator.rb +1 -1
  16. data/lib/representable/definition.rb +7 -2
  17. data/lib/representable/deserializer.rb +4 -3
  18. data/lib/representable/for_collection.rb +1 -1
  19. data/lib/representable/hash.rb +6 -2
  20. data/lib/representable/hash/allow_symbols.rb +9 -11
  21. data/lib/representable/hash/binding.rb +1 -0
  22. data/lib/representable/hash/collection.rb +4 -2
  23. data/lib/representable/hash_methods.rb +3 -2
  24. data/lib/representable/insert.rb +1 -1
  25. data/lib/representable/json.rb +5 -7
  26. data/lib/representable/json/collection.rb +3 -0
  27. data/lib/representable/json/hash.rb +1 -0
  28. data/lib/representable/object.rb +1 -1
  29. data/lib/representable/object/binding.rb +5 -1
  30. data/lib/representable/option.rb +19 -0
  31. data/lib/representable/pipeline.rb +3 -2
  32. data/lib/representable/pipeline_factories.rb +4 -2
  33. data/lib/representable/populator.rb +1 -1
  34. data/lib/representable/represent.rb +1 -0
  35. data/lib/representable/serializer.rb +2 -1
  36. data/lib/representable/version.rb +1 -1
  37. data/lib/representable/virtus_coercion.rb +38 -0
  38. data/lib/representable/xml.rb +7 -10
  39. data/lib/representable/xml/binding.rb +2 -3
  40. data/lib/representable/yaml.rb +3 -3
  41. data/lib/representable/yaml/binding.rb +1 -0
  42. data/representable.gemspec +3 -3
  43. data/test/as_test.rb +4 -4
  44. data/test/binding_test.rb +10 -10
  45. data/test/cached_test.rb +19 -19
  46. data/test/class_test.rb +7 -7
  47. data/test/coercion_test.rb +33 -22
  48. data/test/config/inherit_test.rb +14 -14
  49. data/test/config_test.rb +18 -18
  50. data/test/decorator_scope_test.rb +3 -3
  51. data/test/decorator_test.rb +17 -17
  52. data/test/default_test.rb +7 -7
  53. data/test/definition_test.rb +32 -32
  54. data/test/{example.rb → examples/example.rb} +0 -0
  55. data/test/exec_context_test.rb +6 -6
  56. data/test/features_test.rb +3 -3
  57. data/test/filter_test.rb +6 -6
  58. data/test/for_collection_test.rb +2 -2
  59. data/test/generic_test.rb +3 -3
  60. data/test/getter_setter_test.rb +5 -5
  61. data/test/hash_test.rb +19 -19
  62. data/test/heritage_test.rb +4 -4
  63. data/test/if_test.rb +6 -6
  64. data/test/include_exclude_test.rb +12 -12
  65. data/test/inherit_test.rb +15 -15
  66. data/test/inline_test.rb +11 -11
  67. data/test/instance_test.rb +29 -29
  68. data/test/is_representable_test.rb +10 -10
  69. data/test/json_test.rb +7 -7
  70. data/test/lonely_test.rb +16 -16
  71. data/test/nested_test.rb +7 -7
  72. data/test/object_test.rb +7 -7
  73. data/test/option_test.rb +36 -0
  74. data/test/parse_pipeline_test.rb +3 -3
  75. data/test/pipeline_test.rb +43 -43
  76. data/test/populator_test.rb +15 -15
  77. data/test/prepare_test.rb +2 -2
  78. data/test/private_options_test.rb +2 -2
  79. data/test/reader_writer_test.rb +2 -2
  80. data/test/render_nil_test.rb +2 -2
  81. data/test/represent_test.rb +4 -4
  82. data/test/representable_test.rb +27 -27
  83. data/test/schema_test.rb +5 -5
  84. data/test/serialize_deserialize_test.rb +2 -2
  85. data/test/skip_test.rb +10 -10
  86. data/test/stringify_hash_test.rb +3 -3
  87. data/test/test_helper.rb +4 -2
  88. data/test/uncategorized_test.rb +10 -10
  89. data/test/user_options_test.rb +4 -4
  90. data/test/virtus_coercion_test.rb +52 -0
  91. data/test/wrap_test.rb +11 -11
  92. data/test/xml_namespace_test.rb +1 -1
  93. data/test/xml_test.rb +6 -6
  94. data/test/yaml_test.rb +20 -20
  95. metadata +79 -19
  96. data/.travis.yml +0 -16
  97. data/lib/representable/autoload.rb +0 -14
  98. data/test/mongoid_test.rb +0 -31
@@ -8,7 +8,7 @@ class RenderNilTest < MiniTest::Spec
8
8
  property :title, render_nil: true
9
9
  end
10
10
 
11
- it { Song.new.extend(representer).to_hash.must_equal({"title"=>nil}) }
11
+ it { _(Song.new.extend(representer).to_hash).must_equal({"title"=>nil}) }
12
12
  end
13
13
 
14
14
  describe "with :extend it shouldn't extend nil" do
@@ -16,6 +16,6 @@ class RenderNilTest < MiniTest::Spec
16
16
  property :title, render_nil: true, extend: Class
17
17
  end
18
18
 
19
- it { Song.new.extend(representer).to_hash.must_equal({"title"=>nil}) }
19
+ it { _(Song.new.extend(representer).to_hash).must_equal({"title"=>nil}) }
20
20
  end
21
21
  end
@@ -24,7 +24,7 @@ class RepresentTest < MiniTest::Spec
24
24
  }
25
25
 
26
26
  it { render(representer.represent(songs)).must_equal_document output }
27
- it { parse(representer.represent([]), input).must_equal songs }
27
+ it { _(parse(representer.represent([]), input)).must_equal songs }
28
28
  end
29
29
 
30
30
  # Decorator.represents detects collection.
@@ -40,7 +40,7 @@ class RepresentTest < MiniTest::Spec
40
40
  }
41
41
 
42
42
  it { render(representer.represent(songs)).must_equal_document output }
43
- it("ficken") { parse(representer.represent([]), input).must_equal songs }
43
+ it("ficken") { _(parse(representer.represent([]), input)).must_equal songs }
44
44
  end
45
45
  end
46
46
 
@@ -65,7 +65,7 @@ class RepresentTest < MiniTest::Spec
65
65
  }
66
66
 
67
67
  it { render(representer.represent(song)).must_equal_document output }
68
- it { parse(representer.represent(Song.new), input).must_equal song }
68
+ it { _(parse(representer.represent(Song.new), input)).must_equal song }
69
69
  end
70
70
 
71
71
 
@@ -82,7 +82,7 @@ class RepresentTest < MiniTest::Spec
82
82
  }
83
83
 
84
84
  it { render(representer.represent(song)).must_equal_document output }
85
- it { parse(representer.represent(Song.new), input).must_equal song }
85
+ it { _(parse(representer.represent(Song.new), input)).must_equal song }
86
86
  end
87
87
  end
88
88
  end
@@ -132,8 +132,8 @@ class RepresentableTest < MiniTest::Spec
132
132
 
133
133
  # test ::inherited.
134
134
  it do
135
- BaseClass.respond_to?(:other).must_equal false
136
- SubClass.respond_to?(:other).must_equal true
135
+ _(BaseClass.respond_to?(:other)).must_equal false
136
+ _(SubClass.respond_to?(:other)).must_equal true
137
137
  end
138
138
 
139
139
  module DifferentIncluded
@@ -164,13 +164,13 @@ class RepresentableTest < MiniTest::Spec
164
164
  it "doesn't modify options hash" do
165
165
  options = {}
166
166
  representer.property(:title, options)
167
- options.must_equal({})
167
+ _(options).must_equal({})
168
168
  end
169
169
 
170
170
  representer! {}
171
171
 
172
172
  it "returns the Definition instance" do
173
- representer.property(:name).must_be_kind_of Representable::Definition
173
+ _(representer.property(:name)).must_be_kind_of Representable::Definition
174
174
  end
175
175
  end
176
176
 
@@ -233,7 +233,7 @@ class RepresentableTest < MiniTest::Spec
233
233
 
234
234
  it "includes false attributes" do
235
235
  @band.from_hash({"groupies"=>false})
236
- assert_equal false, @band.groupies
236
+ refute @band.groupies
237
237
  end
238
238
 
239
239
  it "ignores properties not present in the incoming document" do
@@ -300,14 +300,14 @@ class RepresentableTest < MiniTest::Spec
300
300
  end
301
301
 
302
302
  it "#to_hash propagates to nested objects" do
303
- OpenStruct.new(track: OpenStruct.new(nr: 1, length: OpenStruct.new(seconds: nil))).extend(representer).extend(Representable::Debug).
304
- to_hash(user_options: {nr: 9}).must_equal({"track"=>{"nr"=>9, "length"=>{seconds: 9}}})
303
+ _(OpenStruct.new(track: OpenStruct.new(nr: 1, length: OpenStruct.new(seconds: nil))).extend(representer).extend(Representable::Debug).
304
+ to_hash(user_options: {nr: 9})).must_equal({"track"=>{"nr"=>9, "length"=>{seconds: 9}}})
305
305
  end
306
306
 
307
307
  it "#from_hash propagates to nested objects" do
308
308
  song = OpenStruct.new.extend(representer).from_hash({"track"=>{"nr" => "replace me", "length"=>{"seconds"=>"replacing"}}}, user_options: {nr: 9})
309
- song.track.nr.must_equal 9
310
- song.track.length.seconds.must_equal 9
309
+ _(song.track.nr).must_equal 9
310
+ _(song.track.length.seconds).must_equal 9
311
311
  end
312
312
  end
313
313
  end
@@ -364,12 +364,12 @@ class RepresentableTest < MiniTest::Spec
364
364
  end
365
365
 
366
366
  it "executes lambda in represented instance context" do
367
- Song.new("Carnage").instance_eval do
367
+ _(Song.new("Carnage").instance_eval do
368
368
  def compute_representer(name)
369
369
  UpcaseRepresenter
370
370
  end
371
371
  self
372
- end.extend(representer).to_hash.must_equal({"name" => "CARNAGE"})
372
+ end.extend(representer).to_hash).must_equal({"name" => "CARNAGE"})
373
373
  end
374
374
  end
375
375
 
@@ -382,8 +382,8 @@ class RepresentableTest < MiniTest::Spec
382
382
 
383
383
  it "uses object from :instance but still extends it" do
384
384
  song = Song.new.extend(representer).from_hash("name" => "Eric's Had A Bad Day")
385
- song.name.must_equal obj
386
- song.name.must_be_kind_of mod
385
+ _(song.name).must_equal obj
386
+ _(song.name).must_be_kind_of mod
387
387
  end
388
388
  end
389
389
 
@@ -398,8 +398,8 @@ class RepresentableTest < MiniTest::Spec
398
398
  end
399
399
 
400
400
  it "uses lambda when parsing" do
401
- Song.new.extend(representer).from_hash({"name" => "You Make Me Thick"}).name.must_equal "you make me thick"
402
- Song.new.extend(representer).from_hash({"name" => "Stepstranger"}).name.must_equal "stepstranger" # DISCUSS: we compare "".is_a?(UpcaseString)
401
+ _(Song.new.extend(representer).from_hash({"name" => "You Make Me Thick"}).name).must_equal "you make me thick"
402
+ _(Song.new.extend(representer).from_hash({"name" => "Stepstranger"}).name).must_equal "stepstranger" # DISCUSS: we compare "".is_a?(UpcaseString)
403
403
  end
404
404
 
405
405
  describe "with :class lambda" do
@@ -410,12 +410,12 @@ class RepresentableTest < MiniTest::Spec
410
410
 
411
411
  it "creates instance from :class lambda when parsing" do
412
412
  song = OpenStruct.new.extend(representer).from_hash({"name" => "Quitters Never Win"})
413
- song.name.must_be_kind_of UpcaseString
414
- song.name.must_equal "QUITTERS NEVER WIN"
413
+ _(song.name).must_be_kind_of UpcaseString
414
+ _(song.name).must_equal "QUITTERS NEVER WIN"
415
415
 
416
416
  song = OpenStruct.new.extend(representer).from_hash({"name" => "Still Failing?"})
417
- song.name.must_be_kind_of String
418
- song.name.must_equal "still failing?"
417
+ _(song.name).must_be_kind_of String
418
+ _(song.name).must_equal "still failing?"
419
419
  end
420
420
  end
421
421
  end
@@ -427,12 +427,12 @@ class RepresentableTest < MiniTest::Spec
427
427
  end
428
428
 
429
429
  it "uses lambda for each item when rendering" do
430
- Album.new([UpcaseString.new("Dean Martin"), "Charlie Still Smirks"]).extend(representer).to_hash.must_equal("songs"=>["DEAN MARTIN", "charlie still smirks"])
430
+ _(Album.new([UpcaseString.new("Dean Martin"), "Charlie Still Smirks"]).extend(representer).to_hash).must_equal("songs"=>["DEAN MARTIN", "charlie still smirks"])
431
431
  end
432
432
 
433
433
  it "uses lambda for each item when parsing" do
434
434
  album = Album.new.extend(representer).from_hash("songs"=>["DEAN MARTIN", "charlie still smirks"])
435
- album.songs.must_equal ["dean martin", "charlie still smirks"] # DISCUSS: we compare "".is_a?(UpcaseString)
435
+ _(album.songs).must_equal ["dean martin", "charlie still smirks"] # DISCUSS: we compare "".is_a?(UpcaseString)
436
436
  end
437
437
 
438
438
  describe "with :class lambda" do
@@ -443,7 +443,7 @@ class RepresentableTest < MiniTest::Spec
443
443
 
444
444
  it "creates instance from :class lambda for each item when parsing" do
445
445
  album = Album.new.extend(representer).from_hash("songs"=>["Still Failing?", "charlie still smirks"])
446
- album.songs.must_equal ["still failing?", "CHARLIE STILL SMIRKS"]
446
+ _(album.songs).must_equal ["still failing?", "CHARLIE STILL SMIRKS"]
447
447
  end
448
448
  end
449
449
  end
@@ -454,7 +454,7 @@ class RepresentableTest < MiniTest::Spec
454
454
  let(:songs) { [Song.new("Bloody Mary")] }
455
455
 
456
456
  it "is aliased to :extend" do
457
- Album.new(songs).extend(extend_rpr).to_hash.must_equal Album.new(songs).extend(decorator_rpr).to_hash
457
+ _(Album.new(songs).extend(extend_rpr).to_hash).must_equal Album.new(songs).extend(decorator_rpr).to_hash
458
458
  end
459
459
  end
460
460
 
@@ -477,15 +477,15 @@ class RepresentableTest < MiniTest::Spec
477
477
  it "uses :extend strategy" do
478
478
  album_rpr = Module.new { include Representable::Hash; collection :songs, :class => Song, :extend => SongRepresenter}
479
479
 
480
- album_rpr.prepare(album).to_hash.must_equal({"songs"=>[{"name"=>"Still Friends In The End"}]})
481
- album.must_respond_to :to_hash
480
+ _(album_rpr.prepare(album).to_hash).must_equal({"songs"=>[{"name"=>"Still Friends In The End"}]})
481
+ _(album).must_respond_to :to_hash
482
482
  end
483
483
  end
484
484
 
485
485
  describe "Decorator subclass" do
486
486
  it "uses :decorate strategy" do
487
- AlbumRepresentation.prepare(album).to_hash.must_equal({"songs"=>[{"name"=>"Still Friends In The End"}]})
488
- album.wont_respond_to :to_hash
487
+ _(AlbumRepresentation.prepare(album).to_hash).must_equal({"songs"=>[{"name"=>"Still Friends In The End"}]})
488
+ _(album).wont_respond_to :to_hash
489
489
  end
490
490
  end
491
491
  end
data/test/schema_test.rb CHANGED
@@ -61,10 +61,10 @@ class SchemaTest < MiniTest::Spec
61
61
  let(:label) { OpenStruct.new(:name => "Epitaph", :location => OpenStruct.new(:city => "Sanfran", :name => "DON'T SHOW ME!")) }
62
62
 
63
63
  # Module does correctly include features in inlines.
64
- it { band.extend(Module).to_hash.must_equal({"label"=>{"name"=>"Epitaph", "location"=>{"city"=>"Sanfran"}}, "genre"=>"Punkrock"}) }
64
+ it { _(band.extend(Module).to_hash).must_equal({"label"=>{"name"=>"Epitaph", "location"=>{"city"=>"Sanfran"}}, "genre"=>"Punkrock"}) }
65
65
 
66
66
  # Decorator does correctly include features in inlines.
67
- it { Decorator.new(band).to_hash.must_equal({"label"=>{"name"=>"Epitaph", "location"=>{"city"=>"Sanfran"}}, "genre"=>"Punkrock"}) }
67
+ it { _(Decorator.new(band).to_hash).must_equal({"label"=>{"name"=>"Epitaph", "location"=>{"city"=>"Sanfran"}}, "genre"=>"Punkrock"}) }
68
68
  end
69
69
 
70
70
 
@@ -84,7 +84,7 @@ class SchemaTest < MiniTest::Spec
84
84
  # it { FlatlinersDecorator.new( OpenStruct.new(label: OpenStruct.new) ).
85
85
  # to_hash.must_equal({}) }
86
86
  it do
87
- Decorator.new(band).to_hash.must_equal({"genre"=>"Punkrock", "label"=>{"name"=>"Fat Wreck", "location"=>{"city"=>"Sanfran"}}})
87
+ _(Decorator.new(band).to_hash).must_equal({"genre"=>"Punkrock", "label"=>{"name"=>"Fat Wreck", "location"=>{"city"=>"Sanfran"}}})
88
88
  end
89
89
 
90
90
 
@@ -103,7 +103,7 @@ class SchemaTest < MiniTest::Spec
103
103
  end
104
104
 
105
105
  it do
106
- InheritDecorator.new(band).to_hash.must_equal({"genre"=>"Punkrock", "label"=>{"name"=>"Fat Wreck", "city"=>"San Francisco", "location"=>{"city"=>"Sanfran"}}})
106
+ _(InheritDecorator.new(band).to_hash).must_equal({"genre"=>"Punkrock", "label"=>{"name"=>"Fat Wreck", "city"=>"San Francisco", "location"=>{"city"=>"Sanfran"}}})
107
107
  end
108
108
 
109
109
 
@@ -117,7 +117,7 @@ class SchemaTest < MiniTest::Spec
117
117
  end
118
118
 
119
119
  it do
120
- InheritFromDecorator.new(band).to_hash.must_equal({"genre"=>"Punkrock", "label"=>{"name"=>"Fat Wreck", "city"=>"San Francisco", "employees"=>[{"name"=>"Mike"}], "location"=>{"city"=>"Sanfran"}}})
120
+ _(InheritFromDecorator.new(band).to_hash).must_equal({"genre"=>"Punkrock", "label"=>{"name"=>"Fat Wreck", "city"=>"San Francisco", "employees"=>[{"name"=>"Mike"}], "location"=>{"city"=>"Sanfran"}}})
121
121
  end
122
122
  end
123
123
 
@@ -13,7 +13,7 @@ class SerializeDeserializeTest < BaseTest
13
13
  }
14
14
  end
15
15
 
16
- it { subject.from_hash({"song" => Object}, user_options: {volume: 9}).song.must_equal "OBJECT Object {:volume=>9}" }
16
+ it { _(subject.from_hash({"song" => Object}, user_options: {volume: 9}).song).must_equal "OBJECT Object {:volume=>9}" }
17
17
  end
18
18
 
19
19
  describe "serialize" do
@@ -28,6 +28,6 @@ class SerializeDeserializeTest < BaseTest
28
28
 
29
29
  before { subject.song = "Arrested In Shanghai" }
30
30
 
31
- it { subject.to_hash(user_options: {volume: 9}).must_equal({"song"=>"Arrested In Shanghai {:volume=>9}"}) }
31
+ it { _(subject.to_hash(user_options: {volume: 9})).must_equal({"song"=>"Arrested In Shanghai {:volume=>9}"}) }
32
32
  end
33
33
  end
data/test/skip_test.rb CHANGED
@@ -24,9 +24,9 @@ class SkipParseTest < MiniTest::Spec
24
24
  "airplays" => [{"station" => "JJJ"}]
25
25
  }, user_options: { skip?: true })
26
26
 
27
- song.title.must_equal "Victim Of Fate"
28
- song.band.name.must_equal "Mute 98"
29
- song.airplays[0].station.must_equal "JJJ"
27
+ _(song.title).must_equal "Victim Of Fate"
28
+ _(song.band.name).must_equal "Mute 98"
29
+ _(song.airplays[0].station).must_equal "JJJ"
30
30
  end
31
31
 
32
32
  # skip parsing.
@@ -39,9 +39,9 @@ class SkipParseTest < MiniTest::Spec
39
39
  "airplays" => [{}, {"station" => "JJJ"}, {}],
40
40
  }, user_options: { skip?: true })
41
41
 
42
- song.title.must_be_nil
43
- song.band.must_be_nil
44
- song.airplays.must_equal [airplay]
42
+ _(song.title).must_be_nil
43
+ _(song.band).must_be_nil
44
+ _(song.airplays).must_equal [airplay]
45
45
  end
46
46
  end
47
47
 
@@ -63,19 +63,19 @@ class SkipRenderTest < MiniTest::Spec
63
63
  let(:skip_song) { OpenStruct.new(title: "Time Bomb", band: OpenStruct.new(name: "Rancid")).extend(representer) }
64
64
 
65
65
  # do render.
66
- it { song.to_hash(user_options: { skip?: true }).must_equal({"title"=>"Black Night", "band"=>{"name"=>"Time Again"}}) }
66
+ it { _(song.to_hash(user_options: { skip?: true })).must_equal({"title"=>"Black Night", "band"=>{"name"=>"Time Again"}}) }
67
67
  # skip.
68
- it { skip_song.to_hash(user_options: { skip?: true }).must_equal({"title"=>"Time Bomb"}) }
68
+ it { _(skip_song.to_hash(user_options: { skip?: true })).must_equal({"title"=>"Time Bomb"}) }
69
69
 
70
70
  # do render all collection items.
71
71
  it do
72
72
  song = OpenStruct.new(airplays: [OpenStruct.new(station: "JJJ"), OpenStruct.new(station: "ABC")]).extend(representer)
73
- song.to_hash(user_options: { skip?: true }).must_equal({"airplays"=>[{"station"=>"JJJ"}, {"station"=>"ABC"}]})
73
+ _(song.to_hash(user_options: { skip?: true })).must_equal({"airplays"=>[{"station"=>"JJJ"}, {"station"=>"ABC"}]})
74
74
  end
75
75
 
76
76
  # skip middle item.
77
77
  it do
78
78
  song = OpenStruct.new(airplays: [OpenStruct.new(station: "JJJ"), OpenStruct.new(station: "Radio Dreyeckland"), OpenStruct.new(station: "ABC")]).extend(representer)
79
- song.to_hash(user_options: { skip?: true }).must_equal({"airplays"=>[{"station"=>"JJJ"}, {"station"=>"ABC"}]})
79
+ _(song.to_hash(user_options: { skip?: true })).must_equal({"airplays"=>[{"station"=>"JJJ"}, {"station"=>"ABC"}]})
80
80
  end
81
81
  end
@@ -17,11 +17,11 @@ describe "#from_hash" do
17
17
  end
18
18
 
19
19
  it "parses symbols, too" do
20
- OpenStruct.new.extend(representer).from_hash({:song => {:title => "Der Optimist"}}).song.title.must_equal "Der Optimist"
20
+ _(OpenStruct.new.extend(representer).from_hash({:song => {:title => "Der Optimist"}}).song.title).must_equal "Der Optimist"
21
21
  end
22
22
 
23
23
  it "still parses strings" do
24
- OpenStruct.new.extend(representer).from_hash({"song" => {"title" => "Der Optimist"}}).song.title.must_equal "Der Optimist"
24
+ _(OpenStruct.new.extend(representer).from_hash({"song" => {"title" => "Der Optimist"}}).song.title).must_equal "Der Optimist"
25
25
  end
26
26
 
27
27
  describe "with :wrap" do
@@ -33,7 +33,7 @@ describe "#from_hash" do
33
33
  end
34
34
 
35
35
  it "parses symbols, too" do
36
- OpenStruct.new.extend(representer).from_hash({:album => {:song => {:title => "Der Optimist"}}}).song.title.must_equal "Der Optimist"
36
+ _(OpenStruct.new.extend(representer).from_hash({:album => {:song => {:title => "Der Optimist"}}}).song.title).must_equal "Der Optimist"
37
37
  end
38
38
  end
39
39
  end
data/test/test_helper.rb CHANGED
@@ -1,13 +1,15 @@
1
+ require 'pry-byebug'
1
2
  require 'representable'
2
3
 
3
4
  require 'minitest/autorun'
4
5
  require 'test_xml/mini_test'
5
6
 
6
7
  require "representable/debug"
8
+ require 'minitest/assertions'
7
9
 
8
10
  module MiniTest::Assertions
9
11
  def assert_equal_xml(text, subject)
10
- subject.gsub("\n", "").gsub(/(\s\s+)/, "").must_equal(text.gsub("\n", "").gsub(/(\s\s+)/, ""))
12
+ assert_equal (text.gsub("\n", "").gsub(/(\s\s+)/, "")), subject.gsub("\n", "").gsub(/(\s\s+)/, "")
11
13
  end
12
14
  end
13
15
  String.infect_an_assertion :assert_equal_xml, :must_xml
@@ -47,7 +49,7 @@ module AssertJson
47
49
  module Assertions
48
50
  def assert_json(expected, actual, msg=nil)
49
51
  msg = message(msg, "") { diff expected, actual }
50
- assert(expected.split("").sort == actual.split("").sort, msg)
52
+ assert_equal(expected.split("").sort, actual.split("").sort, msg)
51
53
  end
52
54
  end
53
55
  end
@@ -15,12 +15,12 @@ class StopWhenIncomingObjectFragmentIsNilTest < MiniTest::Spec
15
15
 
16
16
  it do
17
17
  album = Album.new
18
- representer.new(album).from_hash({"id"=>1, "songs"=>[{"title"=>"Walkie Talkie"}]}).songs.must_equal [Song.new("Walkie Talkie")]
18
+ _(representer.new(album).from_hash({"id"=>1, "songs"=>[{"title"=>"Walkie Talkie"}]}).songs).must_equal [Song.new("Walkie Talkie")]
19
19
  end
20
20
 
21
21
  it do
22
22
  album = Album.new(2, ["original"])
23
- representer.new(album).from_hash({"id"=>1, "songs"=>nil}).songs.must_equal ["original"]
23
+ _(representer.new(album).from_hash({"id"=>1, "songs"=>nil}).songs).must_equal ["original"]
24
24
  end
25
25
 
26
26
  end
@@ -35,13 +35,13 @@ class RenderPipelineOptionTest < MiniTest::Spec
35
35
  end
36
36
  end
37
37
 
38
- it { representer.new(Album.new).to_hash(user_options: {function: NilToNA}).must_equal({"id"=>"n/a"}) }
39
- it { representer.new(Album.new(1)).to_hash(user_options: {function: NilToNA}).must_equal({"id"=>1}) }
38
+ it { _(representer.new(Album.new).to_hash(user_options: {function: NilToNA})).must_equal({"id"=>"n/a"}) }
39
+ it { _(representer.new(Album.new(1)).to_hash(user_options: {function: NilToNA})).must_equal({"id"=>1}) }
40
40
 
41
41
  it "is cached" do
42
42
  decorator = representer.new(Album.new)
43
- decorator.to_hash(user_options: {function: NilToNA}).must_equal({"id"=>"n/a"})
44
- decorator.to_hash(user_options: {function: nil}).must_equal({"id"=>"n/a"}) # non-sense function is not applied.
43
+ _(decorator.to_hash(user_options: {function: NilToNA})).must_equal({"id"=>"n/a"})
44
+ _(decorator.to_hash(user_options: {function: nil})).must_equal({"id"=>"n/a"}) # non-sense function is not applied.
45
45
  end
46
46
 
47
47
  end
@@ -56,12 +56,12 @@ class ParsePipelineOptionTest < MiniTest::Spec
56
56
  end
57
57
  end
58
58
 
59
- it { representer.new(Album.new).from_hash({"id"=>nil}, user_options: {function: NilToNA}).id.must_equal "n/a" }
60
- it { representer.new(Album.new(1)).to_hash(user_options: {function: NilToNA}).must_equal({"id"=>1}) }
59
+ it { _(representer.new(Album.new).from_hash({"id"=>nil}, user_options: {function: NilToNA}).id).must_equal "n/a" }
60
+ it { _(representer.new(Album.new(1)).to_hash(user_options: {function: NilToNA})).must_equal({"id"=>1}) }
61
61
 
62
62
  it "is cached" do
63
63
  decorator = representer.new(Album.new)
64
- decorator.from_hash({"id"=>nil}, user_options: {function: NilToNA}).id.must_equal "n/a"
65
- decorator.from_hash({"id"=>nil}, user_options: {function: "nonsense"}).id.must_equal "n/a" # non-sense function is not applied.
64
+ _(decorator.from_hash({"id"=>nil}, user_options: {function: NilToNA}).id).must_equal "n/a"
65
+ _(decorator.from_hash({"id"=>nil}, user_options: {function: "nonsense"}).id).must_equal "n/a" # non-sense function is not applied.
66
66
  end
67
67
  end
@@ -7,9 +7,9 @@ class UserOptionsTest < Minitest::Spec
7
7
  property :title, if: ->(options) { options[:user_options][:visible] }
8
8
  end
9
9
 
10
- it { Song.new("Run With It").extend(representer).to_hash.must_equal({}) }
11
- it { Song.new("Run With It").extend(representer).to_hash(user_options: {visible: true}).must_equal({"title"=>"Run With It"}) }
12
- it { Song.new("Run With It").extend(representer).from_hash("title"=>"Business Conduct").title.must_equal "Run With It" }
13
- it { Song.new("Run With It").extend(representer).from_hash({"title"=>"Business Conduct"}, user_options: {visible: true}).title.must_equal "Business Conduct" }
10
+ it { _(Song.new("Run With It").extend(representer).to_hash).must_equal({}) }
11
+ it { _(Song.new("Run With It").extend(representer).to_hash(user_options: {visible: true})).must_equal({"title"=>"Run With It"}) }
12
+ it { _(Song.new("Run With It").extend(representer).from_hash("title"=>"Business Conduct").title).must_equal "Run With It" }
13
+ it { _(Song.new("Run With It").extend(representer).from_hash({"title"=>"Business Conduct"}, user_options: {visible: true}).title).must_equal "Business Conduct" }
14
14
  end
15
15
  # Representable.deprecations=false
@@ -0,0 +1,52 @@
1
+ require 'test_helper'
2
+ require 'representable/virtus_coercion'
3
+
4
+ class VirtusCoercionTest < MiniTest::Spec
5
+ representer! do
6
+ include Representable::VirtusCoercion
7
+
8
+ property :title # no coercion.
9
+ property :length, :type => Float
10
+
11
+ property :band, :class => OpenStruct do
12
+ property :founded, :type => Integer
13
+ end
14
+
15
+ collection :songs, :class => OpenStruct do
16
+ property :ok, :type => Virtus::Attribute::Boolean
17
+ end
18
+ end
19
+
20
+ let(:album) { OpenStruct.new(:title => "Dire Straits", :length => 41.34,
21
+ :band => OpenStruct.new(:founded => "1977"),
22
+ :songs => [OpenStruct.new(:ok => 1), OpenStruct.new(:ok => 0)]) }
23
+
24
+ it { _(album.extend(representer).to_hash).must_equal({"title"=>"Dire Straits", "length"=>41.34, "band"=>{"founded"=>1977}, "songs"=>[{"ok"=>true}, {"ok"=>false}]}) }
25
+
26
+ it {
27
+ album = OpenStruct.new
28
+ album.extend(representer)
29
+ album.from_hash({"title"=>"Dire Straits", "length"=>"41.34", "band"=>{"founded"=>"1977"}, "songs"=>[{"ok"=>1}, {"ok"=>0}]})
30
+
31
+ # it
32
+ _(album.length).must_equal 41.34
33
+ _(album.band.founded).must_equal 1977
34
+ _(album.songs[0].ok).must_equal true
35
+ }
36
+
37
+
38
+ describe "with user :parse_filter and :render_filter" do
39
+ representer! do
40
+ include Representable::VirtusCoercion
41
+
42
+ property :length, :type => Float,
43
+ :parse_filter => lambda { |input, options| "#{input}.1" }, # happens BEFORE coercer.
44
+ :render_filter => lambda { |fragment,*| "#{fragment}.1" }
45
+ end
46
+
47
+ # user's :parse_filter(s) are run before coercion.
48
+ it { _(OpenStruct.new.extend(representer).from_hash("length"=>"1").length).must_equal 1.1 }
49
+ # user's :render_filter(s) are run before coercion.
50
+ it { _(OpenStruct.new(:length=>1).extend(representer).to_hash).must_equal({"length" => 1.1}) }
51
+ end
52
+ end