Skip to content

How to extend the Kiba DSL

Thibaut Barrère edited this page Feb 10, 2020 · 1 revision

The default Kiba DSL only includes a very limited set of DSL keywords, namely:

  • source
  • transform
  • destination
  • pre_process
  • post_process

It can be useful, though, to create new keywords for scenarios which involve a lot of reuse.

Here is how to create modular blocks of DSL reuse, in a way that restricts conflicts & pollution:

module DSLExtensions
  module FieldTransforms
    def set_if_blank(field, default_value)
      transform do |row|
        if row.fetch(field).blank?
          row.merge(field => default_value)
        else
          row
        end
      end
    end
    
    def copy_field(from:, to:)
      transform do |row|
        row.merge(from => row.fetch(to))
      end
    end
  end
end

You can then use this with:

Kiba.parse do
  extend DSLExtensions::FieldTransforms
  
  source ...

  copy_field from: 'id_du_client', to: :client_id
  set_if_blank :locale, 'en-US'

  destination ...
end

More examples

See examples of this use of pattern:

Next: Can Kiba handle multiple sources and destinations?