Sha256: da9775e91df84303d36c25ebb2c52235dff4784933888b2dbfc32c4e62dfd935

Contents?: true

Size: 1.68 KB

Versions: 5

Compression:

Stored size: 1.68 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 { |options| options[:fragment].name.upcase!; options[:fragment] } do
      property :name

      collection :songs, instance: lambda { |options| options[:fragment].title.upcase!; options[: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_be_nil # scalar property gets overridden when nil.
    _(target.album).must_be_nil # nested property stays nil.
  end

  # to_object
  describe "#to_object" do
    representer!(module: Representable::Object) do
      property :title

      property :album, render_filter: lambda { |input, options|input.name = "Live";input } do
        property :name

        collection :songs, render_filter: lambda { |input, options|input[0].title = 1;input } do
          property :title
        end
      end
    end

    it do
      representer.prepare(source).to_object
      _(source.album.name).must_equal "Live"
      _(source.album.songs[0].title).must_equal 1
    end
  end
end

Version data entries

5 entries across 5 versions & 3 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/representable-3.2.0/test/object_test.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/representable-3.2.0/test/object_test.rb
representable-3.2.0 test/object_test.rb
representable-3.1.1 test/object_test.rb
representable-3.1.0 test/object_test.rb