Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Come up with a general way to conditionally process transforms based on data type #112

Open
eightBitter opened this issue Sep 26, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@eightBitter
Copy link
Contributor

There are times when I need to monkey-patch an existing transform to conditionally check and process an array rather than a string. It would be nice if we could come up with a general way for transforms to do this without having to rewrite every transform.

Currently this is a pretty low priority given that this is a rare use-case - I might have to monkey-patch one transform per ArchivesSpace project.

@eightBitter eightBitter added the enhancement New feature or request label Sep 26, 2022
@eightBitter
Copy link
Contributor Author

Here's a case where I monkey-patched Clean::StripFields

# frozen_string_literal: true
module Ahc
  module Transforms
    # Transformations to clean up data
    module Clean

      class StripFields
        def initialize(fields:)
          @fields = [fields].flatten
        end

        # @param row [Hash{ Symbol => String }]
        def process(row)
          @fields.each do |field|
            val = row.fetch(field, nil)
            if val.is_a? Array
              row[field] = val.map{|item| item.strip}
            else
              row[field] = if val.nil? || val.empty?
                            nil
                          else
                            val.strip
                          end
                        end
          end
          row
        end
      end
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant