Class: SeedGimmick::SeedFile

Inherits:
Object
  • Object
show all
Includes:
Finder
Defined in:
lib/seed_gimmick/seed_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seed_dir, seed_file) ⇒ SeedFile

Returns a new instance of SeedFile.



7
8
9
10
# File 'lib/seed_gimmick/seed_file.rb', line 7

def initialize(seed_dir, seed_file)
  @inflector = Inflector.new(seed_dir)
  @seed_file = seed_file
end

Instance Attribute Details

#inflectorObject (readonly)

Returns the value of attribute inflector.



5
6
7
# File 'lib/seed_gimmick/seed_file.rb', line 5

def inflector
  @inflector
end

#seed_fileObject (readonly)

Returns the value of attribute seed_file.



5
6
7
# File 'lib/seed_gimmick/seed_file.rb', line 5

def seed_file
  @seed_file
end

Instance Method Details

#bootstrap!Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/seed_gimmick/seed_file.rb', line 16

def bootstrap!
  custom_action do |model, data|
    ActiveRecord::Migration.say_with_time(model.model_name.plural) do
      model.transaction do
        model.delete_all
        model.import(data.map {|rec| model.new(rec) })
      end
    end
  end
rescue LoadFailed => e
  puts e.message
end

#custom_action {|_model, load_file| ... } ⇒ Object

Yields:



29
30
31
# File 'lib/seed_gimmick/seed_file.rb', line 29

def custom_action
  yield(_model, load_file) if block_given?
end

#load_fileObject



12
13
14
# File 'lib/seed_gimmick/seed_file.rb', line 12

def load_file
  SeedIO.get(seed_file).load_data
end