Sha256: 0110eabda2460aafea3416c76901e9bf4f6fc6ad3b50803534ac541329ce0b4f

Contents?: true

Size: 1.64 KB

Versions: 7

Compression:

Stored size: 1.64 KB

Contents

require 'spec_helper'
require 'rom/memory'

describe 'Mappers / Symbolizing atributes' do
  include_context 'container'

  before do
    configuration.relation(:users)
    configuration.relation(:tasks)
  end

  it 'automatically maps all attributes using top-level settings' do
    module Test
      class UserMapper < ROM::Mapper
        relation :users

        symbolize_keys true
        prefix 'user'

        attribute :id

        wrap :details, prefix: 'first' do
          attribute :name
        end

        wrap :contact, prefix: false do
          attribute :email
        end
      end
    end

    configuration.register_mapper(Test::UserMapper)

    container.relations.users << {
      'user_id' => 123,
      'first_name' => 'Jane',
      'email' => 'jane@doe.org'
    }

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

    expect(jane).to eql(
      id: 123, details: { name: 'Jane' }, contact: { email: 'jane@doe.org' }
    )
  end

  it 'automatically maps all attributes using settings for wrap block' do
    module Test
      class TaskMapper < ROM::Mapper
        relation :tasks
        symbolize_keys true

        attribute :title

        wrap :details, prefix: 'task' do
          attribute :priority
          attribute :description
        end
      end
    end

    configuration.register_mapper(Test::TaskMapper)

    container.relations.tasks << {
      'title' => 'Task One',
      'task_priority' => 1,
      'task_description' => 'It is a task'
    }

    task = container.relation(:tasks).as(:tasks).first

    expect(task).to eql(
      title: 'Task One',
      details: { priority: 1, description: 'It is a task' }
    )
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

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