Sha256: ee70034c2d11c1d37179369419afb671697b51f89ddf7102d5d9244f9fb544bb

Contents?: true

Size: 1.63 KB

Versions: 3

Compression:

Stored size: 1.63 KB

Contents

require 'spec_helper'
require 'rom/memory'

describe 'Mappers / embedded' do
  let(:setup) { ROM.setup(:memory) }
  let(:rom) { setup.finalize }

  it 'allows mapping embedded tuples' do
    setup.relation(:users)

    setup.mappers do
      define(:users) do
        model name: 'Test::User'

        attribute :name, from: 'name'

        embedded :tasks, from: 'tasks' do
          attribute :title, from: 'title'
        end
      end
    end

    rom.relations.users << {
      'name' => 'Jane',
      'tasks' => [{ 'title' => 'Task One' }, { 'title' => 'Task Two' }]
    }

    jane = rom.read(:users).to_a.first

    expect(rom.read(:users).mapper.header.map(&:name)).to eql([:name, :tasks])

    expect(jane.name).to eql('Jane')
    expect(jane.tasks).to eql([{ title: 'Task One' }, { title: 'Task Two' }])
  end

  it 'allows mapping embedded tuple' do
    setup.relation(:users)

    setup.mappers do
      define(:users) do
        model name: 'Test::User'

        attribute :name, from: 'name'

        embedded :address, from: 'address', type: :hash do
          model name: 'Test::Address'
          attribute :street, from: 'street'
          attribute :city, from: 'city'
        end
      end
    end

    rom.relations.users << {
      'name' => 'Jane',
      'address' => { 'street' => 'Somewhere 1', 'city' => 'NYC' }
    }

    jane = rom.read(:users).to_a.first

    expect(rom.read(:users).mapper.header.map(&:name)).to eql([:name, :address])

    Test::Address.send(:include, Equalizer.new(:street, :city))

    expect(jane.name).to eql('Jane')
    expect(jane.address).to eql(Test::Address.new(street: 'Somewhere 1', city: 'NYC'))
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rom-0.6.0.beta3 spec/integration/mappers/embedded_spec.rb
rom-0.6.0.beta2 spec/integration/mappers/embedded_spec.rb
rom-0.6.0.beta1 spec/integration/mappers/embedded_spec.rb