Sha256: 98dda0a4cd6c6cadc047a71b47bdf0ecf9ff6ab879b64686062c9e692752f8aa

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

require "test_helper"
require "representable/object"

class ObjectTest < MiniTest::Spec
  Song  = Struct.new(:title, :album)
  Album = Struct.new(:name, :songs)

  representer!(module: Representable::Object) do
    property :title

    property :album, instance: lambda { |fragment, *| fragment } do
      property :name

      collection :songs, instance: lambda { |fragment, *|fragment } do
        property :title
      end
    end
    # TODO: collection
  end

  let (:source) { Song.new("The King Is Dead", Album.new("Ruiner", [Song.new("In Vino Veritas II")])) }
  let (:target) { Song.new }

  it do
    representer.prepare(target).from_object(source)

    target.title.must_equal "The King Is Dead"
    target.album.name.must_equal "Ruiner"
    target.album.songs[0].title.must_equal "In Vino Veritas II"
  end

  # ignore nested object when nil
  it do
    representer.prepare(Song.new("The King Is Dead")).from_object(Song.new)

    target.title.must_equal nil # scalar property gets overridden when nil.
    target.album.must_equal nil # nested property stays nil.
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
representable-2.1.6 test/object_test.rb