class SeedGimmick::Seed

Public Class Methods

find(options = nil) click to toggle source
# File lib/seed_gimmick/seed.rb, line 6
def find(options = nil)
  options ||= Options.new
  seed_files(options).map {|file|
    new(file, options)
  }.select {|seed|
    options.tables.empty? || options.tables.include?(seed.table_name)
  }
end
new(file_or_model, options = nil) click to toggle source
# File lib/seed_gimmick/seed.rb, line 21
def initialize(file_or_model, options = nil)
  @options = options || Options.new
  @inflector = Inflector.build(@options)
  unless @model = Inflector.model_class(file_or_model)
    @seed_file = Inflector.pathname(file_or_model)
  end
  self_validation!
end

Private Class Methods

seed_files(options) click to toggle source
# File lib/seed_gimmick/seed.rb, line 16
def seed_files(options)
  Pathname.glob(options.seed_dir.join("**", "*")).select(&:file?)
end

Public Instance Methods

bootstrap() click to toggle source
# File lib/seed_gimmick/seed.rb, line 54
def bootstrap
  ActiveRecord::Migration.say_with_time(table_name) do
    model.transaction do
      model.delete_all
      model.import(seed_io.values.map {|hash| model.new(hash) })
    end
    seed_io.values.size
  end
rescue LoadFailed => e
  $stdout.print e.message
end
compare() click to toggle source
# File lib/seed_gimmick/seed.rb, line 74
def compare
  Difference.extraction(self)
end
dump(exclude_columns = []) click to toggle source
# File lib/seed_gimmick/seed.rb, line 66
def dump(exclude_columns = [])
  ActiveRecord::Migration.say_with_time(table_name) do
    seed_io.dump_data(
      model.select(*dump_columns(exclude_columns)).map(&:attributes)
    )
  end
end
dump_columns(exclude_columns = [], all = false) click to toggle source
# File lib/seed_gimmick/seed.rb, line 48
def dump_columns(exclude_columns = [], all = false)
  return model.column_names if all
  exclude_columns = exclude_columns.presence || @options.exclude_columns
  model.column_names - exclude_columns
end
model() click to toggle source
# File lib/seed_gimmick/seed.rb, line 30
def model
  self_validation!
  @model ||= @inflector.model_for(@seed_file)
end
seed_file(ext = nil) click to toggle source
# File lib/seed_gimmick/seed.rb, line 35
def seed_file(ext = nil)
  self_validation!
  @seed_file ||= @inflector.seed_for(@model, (ext || @options.default_ext))
end
seed_io() click to toggle source
# File lib/seed_gimmick/seed.rb, line 40
def seed_io
  @seed_io ||= SeedIO.factory(seed_file)
end
table_name() click to toggle source
# File lib/seed_gimmick/seed.rb, line 44
def table_name
  model.model_name.plural
end

Private Instance Methods

self_validation!() click to toggle source
# File lib/seed_gimmick/seed.rb, line 79
def self_validation!
  @model || @seed_file || (raise SeedGimmickError)
end