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
|
require "test_helper"
class ParsePipelineTest < Minitest::Spec
Album = Struct.new(:id, :artist, :songs)
Artist = Struct.new(:email)
Song = Struct.new(:title)
describe "transforming nil to [] when parsing" do
representer!(decorator: true) do
collection :songs,
parse_pipeline: ->(*) {
Representable::Pipeline.insert(
parse_functions, # original function list from Binding#parse_functions.
->(input, options) { input.nil? ? [] : input }, # your new function (can be any callable object)..
replace: Representable::OverwriteOnNil # ..that replaces the original function.
)
},
class: Song do
property :title
end
end
it do
representer.new(album = Album.new).from_hash("songs"=>nil)
album.songs.must_equal []
end
it do
representer.new(album = Album.new).from_hash("songs"=>[{"title" => "Business Conduct"}])
album.songs.must_equal [Song.new("Business Conduct")]
end
end
# tests [Collect[Instance, Prepare, Deserialize], Setter]
class Representer < Representable::Decorator
include Representable::Hash
# property :artist, populator: Uber::Options::Value.new(ArtistPopulator.new), pass_options:true do
# property :email
# end
# DISCUSS: rename to populator_pipeline ?
collection :songs, parse_pipeline: ->(*) { [Collect[Instance, Prepare, Deserialize], Setter] }, instance: :instance!, exec_context: :decorator, pass_options: true do
property :title
end
def instance!(*options)
Song.new
end
def songs=(array)
represented.songs=array
end
end
it do
skip "TODO: implement :parse_pipeline and :render_pipeline, and before/after/replace semantics"
album = Album.new
Representer.new(album).from_hash({"artist"=>{"email"=>"yo"}, "songs"=>[{"title"=>"Affliction"}, {"title"=>"Dream Beater"}]})
album.songs.must_equal([Song.new("Affliction"), Song.new("Dream Beater")])
end
end
|