module SeedDump::DumpMethods

Public Instance Methods

dump(records, options = {}) click to toggle source
# File lib/seed_dump/dump_methods.rb, line 5
def dump(records, options = {})
  return nil if records.count == 0

  io = open_io(options)

  write_records_to_io(records, io, options)

  ensure
    io.close if io.present?
end

Private Instance Methods

active_record_import_options(options) click to toggle source
# File lib/seed_dump/dump_methods.rb, line 102
def active_record_import_options(options)
  return unless options[:import] && options[:import].is_a?(Hash)

  ', ' + options[:import].map { |key, value| "#{key}: #{value}" }.join(', ')
end
attribute_names(records, options) click to toggle source
# File lib/seed_dump/dump_methods.rb, line 108
def attribute_names(records, options)
  attribute_names = if records.is_a?(ActiveRecord::Relation) || records.is_a?(Class)
                      records.attribute_names
                    else
                      records[0].attribute_names
                    end

  attribute_names.select {|name| !options[:exclude].include?(name.to_sym)}
end
dump_attribute_new(attribute, value, options) click to toggle source
# File lib/seed_dump/dump_methods.rb, line 33
def dump_attribute_new(attribute, value, options)
  options[:import] ? value_to_s(value) : "#{attribute}: #{value_to_s(value)}"
end
dump_record(record, options) click to toggle source
# File lib/seed_dump/dump_methods.rb, line 18
def dump_record(record, options)
  attribute_strings = []

  # We select only string attribute names to avoid conflict
  # with the composite_primary_keys gem (it returns composite
  # primary key attribute names as hashes).
  record.attributes.select {|key| key.is_a?(String) || key.is_a?(Symbol) }.each do |attribute, value|
    attribute_strings << dump_attribute_new(attribute, value, options) unless options[:exclude].include?(attribute.to_sym)
  end

  open_character, close_character = options[:import] ? ['[', ']'] : ['{', '}']

  "#{open_character}#{attribute_strings.join(", ")}#{close_character}"
end
model_for(records) click to toggle source
# File lib/seed_dump/dump_methods.rb, line 118
def model_for(records)
  if records.is_a?(Class)
    records
  elsif records.respond_to?(:model)
    records.model
  else
    records[0].class
  end
end
open_io(options) click to toggle source
# File lib/seed_dump/dump_methods.rb, line 60
def open_io(options)
  if options[:file].present?
    mode = options[:append] ? 'a+' : 'w+'

    File.open(options[:file], mode)
  else
    StringIO.new('', 'w+')
  end
end
range_to_string(object) click to toggle source
# File lib/seed_dump/dump_methods.rb, line 54
def range_to_string(object)
  from = object.begin.respond_to?(:infinite?) && object.begin.infinite? ? '' : object.begin
  to   = object.end.respond_to?(:infinite?) && object.end.infinite? ? '' : object.end
  "[#{from},#{to}#{object.exclude_end? ? ')' : ']'}"
end
value_to_s(value) click to toggle source
# File lib/seed_dump/dump_methods.rb, line 37
def value_to_s(value)
  value = case value
          when BigDecimal, IPAddr
            value.to_s
          when Date, Time, DateTime
            value.to_s(:db)
          when Range
            range_to_string(value)
          when ->(v) { v.class.ancestors.map(&:to_s).include?('RGeo::Feature::Instance') }
            value.to_s
          else
            value
          end

  value.inspect
end
write_records_to_io(records, io, options) click to toggle source
# File lib/seed_dump/dump_methods.rb, line 70
def write_records_to_io(records, io, options)
  options[:exclude] ||= [:id, :created_at, :updated_at]

  method = options[:import] ? 'import' : 'create!'
  io.write("#{model_for(records)}.#{method}(")
  if options[:import]
    io.write("[#{attribute_names(records, options).map {|name| name.to_sym.inspect}.join(', ')}], ")
  end
  io.write("[\n  ")

  enumeration_method = if records.is_a?(ActiveRecord::Relation) || records.is_a?(Class)
                         :active_record_enumeration
                       else
                         :enumerable_enumeration
                       end

  send(enumeration_method, records, io, options) do |record_strings, last_batch|
    io.write(record_strings.join(",\n  "))

    io.write(",\n  ") unless last_batch
  end

  io.write("\n]#{active_record_import_options(options)})\n")

  if options[:file].present?
    nil
  else
    io.rewind
    io.read
  end
end