Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Expand
titleHow to map a field that is a string in Connec!™, but an id in my app?

Okay so now you're mapping items, which can be either a product or a service. In Connec!™ we have a field type that can be either SERVICE, MANUFACTURED or PURCHASED, but in your app you have a type_id that is a meaningless integer.


A typical way to approach this is to retrieve the information about the types in YourApp and set it to a TYPE_MAPPER constant:

Code Block
languageruby
TYPE_MAPPER = {
  PURCHASED => 1,
  MANUFACTURED => 2, 
  SERVICE => 3
}


And then use the mapper in the after_normalize/denormalize hooks:

Code Block
languageruby
after_normalize do |input, output|
  output[:type_id] = TYPE_MAPPER[input['type']
  output
end

A typical way to approach this is to retrieve the information about the types in YourApp and set it to a TYPE_MAPPER constant:

Code Block
languageruby
TYPE_MAPPER = {
  PURCHASED => 1,
  MANUFACTURED => 2, 
  SERVICE => 3
}

And then use the mapper in the after_normalize/denormalize hooks:

Code Block
languageruby
after_normalize do |input, output|
  output[:type_id] = TYPE_MAPPER[input['type']
  output
end



Expand
titleHow to map entities that contain an array of subentities?

You're trying to map invoices, and you've noticed that invoices can have several lines, and you don't know how to map that.

You could do some loops in your after_normalize and after_denormalize hooks to handle the lines, but there's actually a far better way to do it:

Code Block
languageruby
class InvoiceLineMapper
  extend HashMapper
 
  map from('quantity'), to('quantity')
end
 
class InvoiceMapper
  extend HashMapper
 
  map from('lines'), to('invoice_lines'), using: InvoiceLineMapper
end 

...