Upgrading to Transproc 1.1.0, issue with class methods

Happy friday!

We are trying to upgrade to hanami 1.3.2, which in turn is making us upgrade Transproc to 1.1.0. We have some mappers which relied on Transformer class methods being available with on declaration, but now we are getting undefined method errors.

Reproducible Code

# test.rb

require 'bundler/inline'

gemfile do
  source "https://rubygems.org"

  gem "rom"

  # doesnt work on 1.1.0
  gem "transproc", ENV.fetch('TRANSPROC_VERSION')
end

require 'rom/transformer'

class ProductMapper < ROM::Transformer
  class << self
    def prepare(value)
      value.to_s.strip
    end
  end

  map_array do
    map_value :name, method(:prepare)
  end
end

puts ProductMapper.new.call([name: ' test '])

To illustrate the issue, run the following commands:

TRANSPROC_VERSION=1.0.2 rb test.rb # this works
TRANSPROC_VERSION=1.1.0 rb test.rb # this does not work

Question

What is the best pattern for defining methods in a Transformer which can be used in declarations? i.e. how could I refactor the above code to work on 1.1.0?

Thank you for your hard work and time :heart:

Hmm weird, looks like a regression. Anyhow, you can now use instance methods with shorter syntax:

require 'rom/transformer'

class ProductMapper < ROM::Transformer
  map do
    map_value(:name, &:prepare)
  end

  def prepare(value)
    value.to_s.strip
  end
end

:exclamation: Notice map instead of map_array :exclamation:

@solnic Using map does not seem to work for me. Getting this error:

Traceback (most recent call last):
	2: from tmp/test.rb:16:in `<main>'
	1: from tmp/test.rb:17:in `<class:ProductMapper>'
/home/ianks/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/transproc-1.1.0/lib/transproc/transformer/deprecated/class_interface.rb:53:in `method_missing': undefined method `map' for ProductMapper:Class (NoMethodError)

Looks like this will work as well:

require 'rom/transformer'

class ProductMapper < ROM::Transformer
  define! do
    map_array do
      map_value :name, &:prepare
    end
  end

  def prepare(value)
    value.to_s.strip
  end
end

Ah sorry, forgot to mention this requires rom 5.1.0.

No problem. Issue has now been resolved, thank you!

1 Like