Sha256: d61b84f92bd42aeceb55e1add5ad61b4273e51729594be2a8d2e338770a23015

Contents?: true

Size: 1.5 KB

Versions: 6

Compression:

Stored size: 1.5 KB

Contents

require 'spec_helper'
require 'rom/memory'

describe 'Mappers / embedded' do
  include_context 'container'

  before do
    configuration.relation(:users)
  end

  it 'allows mapping embedded tuples' do
    configuration.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

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

    jane = container.relation(:users).map_with(:users).first

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

  it 'allows mapping embedded tuple' do
    configuration.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

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

    jane = container.relation(:users).as(:users).first

    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

6 entries across 6 versions & 1 rubygems

Version Path
rom-2.0.1 spec/integration/mappers/embedded_spec.rb
rom-2.0.0 spec/integration/mappers/embedded_spec.rb
rom-1.0.0 spec/integration/mappers/embedded_spec.rb
rom-1.0.0.rc1 spec/integration/mappers/embedded_spec.rb
rom-1.0.0.beta2 spec/integration/mappers/embedded_spec.rb
rom-1.0.0.beta1 spec/integration/mappers/embedded_spec.rb