I’m trying to get a resulting hash: { "address_line1": "X Y Z" }. I can’t figure out how to combine the different-keyed value. extract_key requires the key name to be the same, wrap and nest don’t exactly offer what I want.
I’m new to dry-rb so I’m getting a bit brain-knotted, can someone help me out?
I have two difficulties with your question: 1. you mention an array, but your example doesn’t have one, 2. you did not specify what library you’re talking about so it took several readings to deduce you’re using dry-transformer.
So, your first problem is that you’re confusing data types. extract_key is for Arrays, and you’re working with Hashes.
Given the input and output you have defined, it would look like this
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'dry-transformer'
end
require 'dry/transformer'
class AddressTransformer < Dry::Transformer::Pipe
import Dry::Transformer::HashTransformations
define! do
map_value :address, ->(a) { a.values_at(:line1, :line2, :line3).join(" ") }
rename_keys address: :address_line1
end
end
puts AddressTransformer.new.({
address: {
line1: "X",
line2: "Y",
line3: "Z"
}
})