Sha256: a69567b28eb31feda7ff3008fa52a017f7d0f30e93bd6921203dc24e96e362da

Contents?: true

Size: 1.19 KB

Versions: 13

Compression:

Stored size: 1.19 KB

Contents

require 'spec_helper'

describe 'Reusing mappers' do
  it 'allows using another mapper in mapping definitions' do
    class Test::TaskMapper < ROM::Mapper
      attribute :title
    end

    class Test::UserMapper < ROM::Mapper
      attribute :name
      group :tasks, mapper: Test::TaskMapper
    end

    mapper = Test::UserMapper.build
    relation = [{ name: 'Jane', title: 'One' }, { name: 'Jane', title: 'Two' }]
    result = mapper.call(relation)

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

  it 'allows using another mapper in an embbedded hash' do
    class Test::PriorityMapper < ROM::Mapper
      attribute :value, type: :integer
      attribute :desc
    end

    class Test::TaskMapper < ROM::Mapper
      attribute :title
      embedded :priority, type: :hash, mapper: Test::PriorityMapper
    end

    mapper = Test::TaskMapper.build
    relation = [{ title: 'Task One', priority: { value: '1' } }, { title: 'Task Two', priority: { value: '2' } }]
    result = mapper.call(relation)

    expect(result).to eql([
      { title: 'Task One', priority: { value: 1 } },
      { title: 'Task Two', priority: { value: 2 } }
    ])
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rom-2.0.2 spec/integration/mappers/reusing_mappers_spec.rb
rom-2.0.1 spec/integration/mappers/reusing_mappers_spec.rb
rom-2.0.0 spec/integration/mappers/reusing_mappers_spec.rb
rom-1.0.0 spec/integration/mappers/reusing_mappers_spec.rb
rom-1.0.0.rc1 spec/integration/mappers/reusing_mappers_spec.rb
rom-1.0.0.beta2 spec/integration/mappers/reusing_mappers_spec.rb
rom-1.0.0.beta1 spec/integration/mappers/reusing_mappers_spec.rb
rom-0.9.1 spec/integration/mappers/reusing_mappers_spec.rb
rom-0.9.0 spec/integration/mappers/reusing_mappers_spec.rb
rom-0.9.0.rc1 spec/integration/mappers/reusing_mappers_spec.rb
rom-0.9.0.beta1 spec/integration/mappers/reusing_mappers_spec.rb
rom-0.8.1 spec/integration/mappers/reusing_mappers_spec.rb
rom-0.8.0 spec/integration/mappers/reusing_mappers_spec.rb