1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
require 'test_helper'
class SkipParseTest < Minitest::Spec
representer! do
property :title, skip_parse: lambda { |options| options[:user_options][:skip?] and options[:fragment] == "skip me" }
property :band,
skip_parse: lambda { |options| options[:user_options][:skip?] and options[:fragment]["name"].nil? }, class: OpenStruct do
property :name
end
collection :airplays,
skip_parse: lambda { |options| options[:user_options][:skip?] and options[:fragment]["station"].nil? }, class: OpenStruct do
property :station
end
end
let(:song) { OpenStruct.new.extend(representer) }
# do parse.
it do
song.from_hash({
"title" => "Victim Of Fate",
"band" => {"name" => "Mute 98"},
"airplays" => [{"station" => "JJJ"}]
}, user_options: { skip?: true })
song.title.must_equal "Victim Of Fate"
song.band.name.must_equal "Mute 98"
song.airplays[0].station.must_equal "JJJ"
end
# skip parsing.
let(:airplay) { OpenStruct.new(station: "JJJ") }
it do
song.from_hash({
"title" => "skip me",
"band" => {},
"airplays" => [{}, {"station" => "JJJ"}, {}],
}, user_options: { skip?: true })
song.title.must_be_nil
song.band.must_be_nil
song.airplays.must_equal [airplay]
end
end
class SkipRenderTest < Minitest::Spec
representer! do
property :title
property :band,
skip_render: lambda { |options| options[:user_options][:skip?] and options[:input].name == "Rancid" } do
property :name
end
collection :airplays,
skip_render: lambda { |options| options[:user_options][:skip?] and options[:input].station == "Radio Dreyeckland" } do
property :station
end
end
let(:song) { OpenStruct.new(title: "Black Night", band: OpenStruct.new(name: "Time Again")).extend(representer) }
let(:skip_song) { OpenStruct.new(title: "Time Bomb", band: OpenStruct.new(name: "Rancid")).extend(representer) }
# do render.
it { song.to_hash(user_options: { skip?: true }).must_equal({"title"=>"Black Night", "band"=>{"name"=>"Time Again"}}) }
# skip.
it { skip_song.to_hash(user_options: { skip?: true }).must_equal({"title"=>"Time Bomb"}) }
# do render all collection items.
it do
song = OpenStruct.new(airplays: [OpenStruct.new(station: "JJJ"), OpenStruct.new(station: "ABC")]).extend(representer)
song.to_hash(user_options: { skip?: true }).must_equal({"airplays"=>[{"station"=>"JJJ"}, {"station"=>"ABC"}]})
end
# skip middle item.
it do
song = OpenStruct.new(airplays: [OpenStruct.new(station: "JJJ"), OpenStruct.new(station: "Radio Dreyeckland"), OpenStruct.new(station: "ABC")]).extend(representer)
song.to_hash(user_options: { skip?: true }).must_equal({"airplays"=>[{"station"=>"JJJ"}, {"station"=>"ABC"}]})
end
end
|