Sha256: eae567686d99c102b7ce93a4478c991ac8f9c92847e880e6638eef37498cbe44

Contents?: true

Size: 1.37 KB

Versions: 4

Compression:

Stored size: 1.37 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
        end
      end
    end

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

    private

    def method_missing(attribute_name, &block)
      return super unless @model_class.attribute_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.1.3 lib/evil_seed/anonymizer.rb
evil-seed-0.1.2 lib/evil_seed/anonymizer.rb
evil-seed-0.1.1 lib/evil_seed/anonymizer.rb
evil-seed-0.1.0 lib/evil_seed/anonymizer.rb