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

.buildTransformer

Build a mapper instance

Returns:



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

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)


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

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



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

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



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

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