Skip to content

Commit

Permalink
[core] simplify Transformer setup
Browse files Browse the repository at this point in the history
  • Loading branch information
solnic committed Jul 18, 2019
1 parent 680a6b2 commit 01a5fe9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
25 changes: 25 additions & 0 deletions core/lib/rom/transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,31 @@ class Transformer < Transproc::Transformer[ROM::Processor::Transproc::Functions]

defines :relation, :register_as

# Configure relation for the transformer
#
# @example 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)
#
# @param name [Symbol]
# @param options [Hash]
# @option options :as [Symbol] Mapper identifier
#
# @api public
def self.relation(name = Undefined, options = EMPTY_HASH)
return @relation if name.equal?(Undefined) && defined?(@relation)
register_as(options.fetch(:as, name))
super(name)
end

# Define transformation pipeline
#
# @example
Expand Down
3 changes: 1 addition & 2 deletions core/spec/integration/transformer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

let(:mapper_class) do
Class.new(ROM::Transformer) do
relation :users
register_as :default
relation :users, as: :default

map do
rename_keys user_id: :id
Expand Down

0 comments on commit 01a5fe9

Please sign in to comment.