Sha256: f895a9865600e5fa8ef9c38bc296ce8f1751306a4488076d57102db2d9e3b8b9

Contents?: true

Size: 818 Bytes

Versions: 6

Compression:

Stored size: 818 Bytes

Contents

# frozen_string_literal: true

#   Usage:
#
#     records = AnonymousActiveRecord.factory(source_data: [{name: 'Bob McGurdy'}], columns: ['name']) do
#                 def flowery_name
#                   "🌸#{name}🌸"
#                 end
#               end
#     records.first.flowery_name # => "🌸Bob McGurdy🌸"
#
module AnonymousActiveRecord
  # Factory initializes instances of a pseudo anonymous class in a particular namespace of your choosing.
  class Factory
    attr_reader :klass

    def initialize(source_data, klass)
      @source_data = source_data # an array of hashes
      @klass = klass
    end

    def run
      @source_data.map do |hash|
        klass.create(hash)
      end
    end

    def run!
      @source_data.map do |hash|
        klass.create!(hash)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
anonymous_active_record-1.0.9 lib/anonymous_active_record/factory.rb
anonymous_active_record-1.0.8 lib/anonymous_active_record/factory.rb
anonymous_active_record-1.0.7 lib/anonymous_active_record/factory.rb
anonymous_active_record-1.0.6 lib/anonymous_active_record/factory.rb
anonymous_active_record-1.0.5 lib/anonymous_active_record/factory.rb
anonymous_active_record-1.0.4 lib/anonymous_active_record/factory.rb