Module: Superseeder::Seedable

Defined in:
lib/superseeder/seedable.rb

Instance Method Summary collapse

Instance Method Details

#seed(*args, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/superseeder/seedable.rb', line 4

def seed(*args, &block)
  opts = args.extract_options!
  path = if opts[:path].blank?
           Rails.root.join('db', 'seeds', 'data')
         else
           Pathname(opts.delete :path)
         end
  filename = opts.delete :filename
  path = if filename.blank?
           tmp = path.join "#{self.name.underscore.pluralize}*"
           candidates = Dir.glob tmp
           raise ArgumentError.new "Multiple candidate files for #{self.name}: #{candidates}, either leave one candidate file or specify the filename you want to use with the :filename option." if candidates.length > 1
           raise ArgumentError.new "No candidate files for #{self.name}. Either create a #{tmp} file or specify the filename you want to use with the :filename option." if candidates.length == 0
           candidates.first
         else
           tmp = path.join filename
           raise ArgumentError.new "#{filename} does not exist." unless File.exist? tmp
           tmp
         end
  ext = File.extname path
  Dir[Pathname(Gem::Specification.find_by_name('superseeder').gem_dir).join('lib','superseeder','formats','*.rb')].each{ |f| require(f) }
  modules = Superseeder::Formats.constants.select{ |c| Module === Superseeder::Formats.const_get(c) }.map{ |c| Superseeder::Formats.const_get(c) }
  mod = modules.find{ |m| m.send(:extensions).include? ext }
  raise ArgumentError.new "No registered module to support #{ext} extension." if mod.nil?
  puts "Seeding #{self.name.downcase.pluralize.humanize} from #{path}..."
  count = self.count
  self.extend mod
  self.__process path, opts, &block
  puts "Done. (#{self.count - count} entries added)"
end