Sha256: 9bd8ab8ccb0561a30547ebec2183be5c19166c5cc4255f95650bf855efa9b388

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module EvilSeed
  # This class constructs customizer callable with simple DSL:
  #
  #     config.anonymize("User")
  #       name  { Faker::Name.name }
  #       email { Faker::Internet.email }
  #     end
  #
  # Resulting object can be called with record attributes and will return modified copy.
  #
  #     attrs = { name: 'Luke', email: 'luke@skywalker.com' }
  #     a.call(attrs)
  #     attrs # => { name: 'John', email: 'bob@example.com' }
  #
  class Anonymizer
    # @param model_name [String] A string containing class name of your ActiveRecord model
    def initialize(model_name, &block)
      @model_class = model_name.constantize
      @changers = {}
      instance_eval(&block)
    end

    # @param attributes [Hash{String=>void}] Record attributes.
    # @return           [Hash{String=>void}] Modified deep copy of +attributes+
    def call(attributes)
      attributes.deep_dup.tap do |attrs|
        @changers.each do |attribute, changer|
          attrs[attribute] = changer.call(attrs[attribute])
        end
      end
    end

    def respond_to_missing?(attribute_name)
      @model_class.column_names.include?(attribute_name.to_s) || super
    end

    private

    def method_missing(attribute_name, &block)
      return super unless @model_class.column_names.include?(attribute_name.to_s)
      @changers[attribute_name.to_s] = block
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
evil-seed-0.6.0 lib/evil_seed/anonymizer.rb
evil-seed-0.5.0 lib/evil_seed/anonymizer.rb
evil-seed-0.4.0 lib/evil_seed/anonymizer.rb
evil-seed-0.3.0 lib/evil_seed/anonymizer.rb