Class: ROM::Transformer

Inherits:
Object
  • Object
show all
Extended by:
Dry::Core::ClassAttributes
Defined in:
core/lib/rom/transformer.rb

Overview

Transformer is a data mapper which uses Transproc's transformer DSL to define transformations.

Class Method Summary collapse

Class Method Details

.base_relationObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This is needed to make transformers compatible with rom setup



82
83
84
# File 'core/lib/rom/transformer.rb', line 82

def self.base_relation
  relation
end

.buildTransformer

Build a mapper instance

Returns:



91
92
93
# File 'core/lib/rom/transformer.rb', line 91

def self.build
  new
end

.map(&block) ⇒ self

Define transformation pipeline

Examples:

class UsersMapper < ROM::Transformer
  map do
    rename_keys user_id: :id
    deep_stringify_keys
  end
end

Returns:

  • (self)


73
74
75
76
77
# File 'core/lib/rom/transformer.rb', line 73

def self.map(&block)
  define! do
    map_array(&block)
  end
end

.register_asSymbol .register_as(name) ⇒ Object

Get or set registration name

Overloads:

  • .register_asSymbol

    Return the registration name

    Returns:

    • (Symbol)
  • .register_as(name) ⇒ Object

    Configure registration name

    Examples:

    class MyMapper < ROM::Transformer
      relation :users
      register_as :my_mapper
    end

    Parameters:

    • name (Symbol)

      The registration name



32
# File 'core/lib/rom/transformer.rb', line 32

defines :register_as

.relation(name = Undefined, options = EMPTY_HASH) ⇒ Object

Configure relation for the transformer

Examples:

with a custom name

class UsersMapper < ROM::Transformer
  relation :users, as: :json_serializer

  map do
    rename_keys user_id: :id
    deep_stringify_keys
  end
end

users.map_with(:json_serializer)

Parameters:

  • name (Symbol) (defaults to: Undefined)
  • options (Hash) (defaults to: EMPTY_HASH)

Options Hash (options):

  • :as (Symbol)

    Mapper identifier



53
54
55
56
57
58
# File 'core/lib/rom/transformer.rb', line 53

def self.relation(name = Undefined, options = EMPTY_HASH)
  return @relation if name.equal?(Undefined) && defined?(@relation)

  register_as(options.fetch(:as, name))
  @relation = name
end