Sha256: 18c0cb119a3625f3732c78aaa8c9ca901ffa64580ae218813efd1b1660233635

Contents?: true

Size: 1.67 KB

Versions: 8

Compression:

Stored size: 1.67 KB

Contents

class CompositionTest < ReformSpec
  class SongAndArtist < Reform::Composition
    map({:artist => [:name], :song => [:title]}) #SongAndArtistMap.representable_attrs
  end

  let (:comp) { SongAndArtist.new(:artist => @artist=OpenStruct.new, :song => rio) }

  it "delegates to models as defined" do
    comp.name.must_equal nil
    comp.title.must_equal "Rio"
  end

  it "raises when non-mapped property" do
    assert_raises NoMethodError do
      comp.raise_an_exception
    end
  end

  it "creates readers to models" do
    comp.song.object_id.must_equal rio.object_id
    comp.artist.object_id.must_equal @artist.object_id
  end

  describe "::map_from" do
    it "creates the same mapping" do
      comp =
      Class.new(Reform::Composition) do
        map_from(
          Class.new(Reform::Representer) do
            property :name,  :on => :artist
            property :title, :on => :song
          end
        )
      end.
      new(:artist => duran, :song => rio)

      comp.name.must_equal "Duran Duran"
      comp.title.must_equal "Rio"
    end
  end

  describe "#nested_hash_for" do
    it "returns nested hash" do
      comp.nested_hash_for(:name => "Jimi Hendrix", :title => "Fire").must_equal({:artist=>{:name=>"Jimi Hendrix"}, :song=>{:title=>"Fire"}})
    end

    it "works with strings" do
      comp.nested_hash_for("name" => "Jimi Hendrix", "title" => "Fire").must_equal({:artist=>{:name=>"Jimi Hendrix"}, :song=>{:title=>"Fire"}})
    end

    it "works with strings in map" do
      Class.new(Reform::Composition) do
        map(:artist => ["name"])
      end.new([nil]).nested_hash_for(:name => "Jimi Hendrix").must_equal({:artist=>{:name=>"Jimi Hendrix"}})
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
reform-0.2.7 test/composition_test.rb
reform-0.2.6 test/composition_test.rb
reform-0.2.5 test/composition_test.rb
reform-0.2.4 test/composition_test.rb
reform-0.2.3 test/composition_test.rb
reform-0.2.2 test/composition_test.rb
reform-0.2.1 test/composition_test.rb
reform-0.2.0 test/composition_test.rb