fast_seeder 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ module FastSeeder
2
+ class Error < StandardError; end
3
+ end
@@ -1,3 +1,3 @@
1
1
  module FastSeeder
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/fast_seeder.rb CHANGED
@@ -1,6 +1,24 @@
1
1
  require 'csv'
2
2
  require 'active_support/all'
3
3
 
4
+ # Populates database with seeds using multiple inserts.
5
+ #
6
+ # Usage:
7
+ #
8
+ # CSV file:
9
+ # Kharkov,1654
10
+ # Lviv,1240
11
+ # Kiev,600
12
+ #
13
+ # Seeding from CSV file:
14
+ # FastSeeder.seed_csv!(City, "cities/ukraine.csv", :name, :founded_in, :country => "Ukraine")
15
+ #
16
+ # Seeding in place:
17
+ # FastSeeder.seed!(City, :name, :founded_in, :country => "Ukraine") do
18
+ # record "Kharkov", 1654
19
+ # record "Lviv" , 1240
20
+ # record "Kiev" , 600
21
+ # end
4
22
  module FastSeeder
5
23
  extend self
6
24
  extend ActiveSupport::Autoload
@@ -8,8 +26,15 @@ module FastSeeder
8
26
  autoload :Seeders
9
27
  autoload :Adapters
10
28
  autoload :RecordSet
29
+ autoload :Error
11
30
 
12
-
31
+ # Seed database with data from CSV file
32
+ # Usage:
33
+ # FastSeeder.seed_csv!(City, "cities/ukraine.csv", :name, :founded_in, :country => "Ukraine")
34
+ #
35
+ # @param [ActiveRecord::Base] model_class model which needs to be seeded
36
+ # @param [String] csv_file path to CSV file relative to db/seeds
37
+ # @param [*] colums_and_default_values
13
38
  def self.seed_csv!(model_class, csv_file, *colums_and_default_values)
14
39
  default_values = colums_and_default_values.extract_options!
15
40
  columns = colums_and_default_values
@@ -17,7 +42,20 @@ module FastSeeder
17
42
  seeder.seed!
18
43
  end
19
44
 
45
+ # Seeds database with data defined in block with "record" method.
46
+ # Usage:
47
+ # FastSeeder.seed!(City, :name, :founded_in, :country => "Ukraine") do
48
+ # record "Kharkov", 1654
49
+ # record "Lviv" , 1240
50
+ # record "Kiev" , 600
51
+ # end
52
+ # @param [ActiveRecord::Base] model_class model which needs to be seeded
53
+ # @param [*] colums_and_default_values
20
54
  def self.seed!(model_class, *colums_and_default_values, &block)
55
+ unless block_given?
56
+ raise FastSeeder::Error.new("`FastSeeder.seed!` requires a block to be passed")
57
+ end
58
+
21
59
  default_values = colums_and_default_values.extract_options!
22
60
  columns = colums_and_default_values
23
61
  seeder = Seeders::InlineSeeder.new(model_class, columns, default_values, block)
@@ -25,6 +63,7 @@ module FastSeeder
25
63
  end
26
64
 
27
65
 
66
+ # Path to basic seeds directory
28
67
  def seeds_path
29
68
  Rails.root + "db/seeds"
30
69
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fast_seeder
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sergey Potapov
@@ -53,6 +53,7 @@ files:
53
53
  - lib/fast_seeder/adapters/sqlite_adapter.rb
54
54
  - lib/fast_seeder/adapters/mysql_adapter.rb
55
55
  - lib/fast_seeder/adapters/base_adapter.rb
56
+ - lib/fast_seeder/error.rb
56
57
  - lib/fast_seeder.rb
57
58
  - LGPL-LICENSE
58
59
  - Rakefile