dibber 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/README.rdoc +20 -20
- data/Rakefile +5 -1
- data/lib/dibber/seeder.rb +46 -34
- data/lib/dibber/version.rb +7 -4
- data/test/dibber/foo/bar.rb +4 -0
- data/test/dibber/process_log_test.rb +2 -5
- data/test/dibber/seeder_test.rb +66 -10
- data/test/dibber/seeds/foo/bars.yml +2 -0
- data/test/dibber/thing_test.rb +15 -17
- data/test/examples/seeds.rb +21 -15
- data/test/test_helper.rb +10 -0
- metadata +39 -19
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
ZmFkYWQ1NzkxM2NjMDVmMGExYTJhMTBkOTJmZTZkNTljODAwYzdjZA==
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: df49d47736e3cf6e58a22a2b84d77e76912c8d32
|
4
|
+
data.tar.gz: 03277a6b8d5f532f6ba343bfba8dc71a789785ee
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
MjIwNTc3MzUxMmZjZTM2ZTlhOWUxZGJkY2U5MmU4Mjc4OWFkY2MxMTVmNjU4
|
11
|
-
ZGMzNDFkNThjYTgwYzBkZmE0YmNiMjBkNzNjMDJmNjM0ZmQ2N2Y=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
NmJmYTM5NjM0ZWYzNmM1MDBmMGFlMmY3ZGUwY2UyYmFhMWM2OTc1ZTFhYzU4
|
14
|
-
OGNlYjJhMDViMDhlNDdjY2RhYzYwODZiODRmYjExYWEyZjBjOGFlOTJlOWMy
|
15
|
-
YWZiYWIyOTZiMmFiMzZhMmE2MWZhNzIyYTJjMjZjOGExN2RhNDI=
|
6
|
+
metadata.gz: 330909b6d4fe39385438b0eda1984c7f1f9571cae69eeba9732bd0927ae9d027ea0f4a6b7fa6324991d446158f89373235bf34c3685f92097cf41e50e0eb4f0f
|
7
|
+
data.tar.gz: 627b1582b890c6721a35f969c15c338372b1f96aa445e48af4ff4f3a12f9d56690a088557a5034c1c2ba44a32c5f0e7da6a59dbb9dbcffe9ce8235f5fcfd7619
|
data/README.rdoc
CHANGED
@@ -21,9 +21,9 @@ Add this to your Gemfile:
|
|
21
21
|
|
22
22
|
=== Rails Examples
|
23
23
|
|
24
|
-
You have a rails app with a Thing model, and you want to seed it with some
|
25
|
-
things. Thing instances have the attributes
|
26
|
-
You have a YAML file
|
24
|
+
You have a rails app with a `Thing` model, and you want to seed it with some
|
25
|
+
things. `Thing` instances have the attributes `name`, `colour`, `size`.
|
26
|
+
You have a YAML file `db/seeds/things.yml` that looks like this:
|
27
27
|
|
28
28
|
foo:
|
29
29
|
colour: red
|
@@ -36,42 +36,32 @@ You have a YAML file 'db/seeds/things.yml' that looks like this:
|
|
36
36
|
Add this to your 'db/seeds.rb'
|
37
37
|
|
38
38
|
Seeder = Dibber::Seeder
|
39
|
-
Seeder.seed
|
39
|
+
Seeder.seed Thing
|
40
40
|
puts Seeder.report
|
41
41
|
|
42
|
-
Then run
|
42
|
+
Then run `rake db:seed`
|
43
43
|
|
44
|
-
Seeder will create two new things.
|
44
|
+
Seeder will create two new things.
|
45
45
|
|
46
46
|
You'll then be able to do this:
|
47
47
|
|
48
|
-
thing = Thing.
|
48
|
+
thing = Thing.find_by(name: 'foo')
|
49
49
|
thing.colour ---> 'red'
|
50
50
|
|
51
|
-
|
51
|
+
== Report
|
52
52
|
|
53
|
-
Dibber can be used outside of Rails, but in this case you will need to
|
54
|
-
specify the location of the seed files.
|
55
|
-
|
56
|
-
Seeder.seeds_path = "some/path/to/seeds"
|
57
|
-
|
58
|
-
You can also use this technique in Rails if you want to put your seed files
|
59
|
-
in an alternative folder to 'db/seeds'
|
60
|
-
|
61
|
-
== Report
|
62
|
-
|
63
53
|
Seeder.report outputs a report detailing start and end time, and a log of how
|
64
54
|
the number of things has changed
|
65
55
|
|
66
56
|
== Overwriting existing entries
|
67
57
|
|
68
|
-
Seeder#build will not overwrite existing data unless directed to do so.
|
58
|
+
Seeder#build will not overwrite existing data unless directed to do so.
|
69
59
|
|
70
60
|
thing.update_attribute(:colour, 'black')
|
71
61
|
Seeder.seed :thing
|
72
62
|
thing.reload.colour ----> 'black'
|
73
63
|
|
74
|
-
Seeder.seed(:thing, :
|
64
|
+
Seeder.seed(:thing, overwrite: true)
|
75
65
|
thing.reload.colour ----> 'red'
|
76
66
|
|
77
67
|
|
@@ -83,6 +73,16 @@ the class name:
|
|
83
73
|
|
84
74
|
Seeder.new(Thing, 'other_things.yml').build
|
85
75
|
|
76
|
+
== Outside Rails
|
77
|
+
|
78
|
+
Dibber can be used outside of Rails, but in this case you will need to
|
79
|
+
specify the location of the seed files.
|
80
|
+
|
81
|
+
Seeder.seeds_path = "some/path/to/seeds"
|
82
|
+
|
83
|
+
You can also use this technique in Rails if you want to put your seed files
|
84
|
+
in a folder other than 'db/seeds'
|
85
|
+
|
86
86
|
== More examples
|
87
87
|
|
88
88
|
Take a look at test/examples/seeds.rb for some more usage examples.
|
data/Rakefile
CHANGED
@@ -14,6 +14,10 @@ Rake::RDocTask.new do |rdoc|
|
|
14
14
|
rdoc.options << '--line-numbers'
|
15
15
|
end
|
16
16
|
|
17
|
-
Rake::TestTask.new do |t|
|
17
|
+
Rake::TestTask.new(:test) do |t|
|
18
|
+
t.libs << "test"
|
19
|
+
t.libs << "lib"
|
18
20
|
t.test_files = FileList['test/**/*_test.rb']
|
19
21
|
end
|
22
|
+
|
23
|
+
task :default => :test
|
data/lib/dibber/seeder.rb
CHANGED
@@ -5,44 +5,50 @@ module Dibber
|
|
5
5
|
class Seeder
|
6
6
|
attr_accessor :klass, :file, :attribute_method, :name_method, :overwrite
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
8
|
+
class << self
|
9
|
+
|
10
|
+
def seed(klass, args = {})
|
11
|
+
if klass.kind_of?(String) || klass.kind_of?(Symbol)
|
12
|
+
name = klass.to_s
|
13
|
+
class_name = klass.to_s.strip.classify
|
14
|
+
klass = Kernel.const_get(class_name)
|
15
|
+
else
|
16
|
+
name = klass.to_s.underscore
|
17
|
+
end
|
18
|
+
new_file = "#{name.pluralize}.yml"
|
19
|
+
new(klass, new_file, args).build
|
20
|
+
end
|
14
21
|
|
15
|
-
|
16
|
-
|
17
|
-
|
22
|
+
def process_log
|
23
|
+
@process_log ||= start_process_log
|
24
|
+
end
|
18
25
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
return @process_log
|
23
|
-
end
|
26
|
+
def clear_process_log
|
27
|
+
@process_log = nil
|
28
|
+
end
|
24
29
|
|
25
|
-
|
26
|
-
|
27
|
-
|
30
|
+
def report
|
31
|
+
process_log.report + error_report
|
32
|
+
end
|
28
33
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
34
|
+
def monitor(klass)
|
35
|
+
log_name = klass.to_s.tableize.to_sym
|
36
|
+
unless process_log.exists?(log_name)
|
37
|
+
process_log.start(log_name, "#{klass}.count")
|
38
|
+
end
|
33
39
|
end
|
34
|
-
end
|
35
40
|
|
36
|
-
|
37
|
-
|
38
|
-
|
41
|
+
def objects_from(file)
|
42
|
+
YAML.load_file("#{seeds_path}#{file}")
|
43
|
+
end
|
39
44
|
|
40
|
-
|
41
|
-
|
42
|
-
|
45
|
+
def seeds_path
|
46
|
+
@seeds_path || try_to_guess_seeds_path || raise_no_seeds_path_error
|
47
|
+
end
|
43
48
|
|
44
|
-
|
45
|
-
|
49
|
+
def seeds_path=(path)
|
50
|
+
@seeds_path = add_trailing_slash_to(path)
|
51
|
+
end
|
46
52
|
end
|
47
53
|
|
48
54
|
def initialize(klass, file, args = {})
|
@@ -60,7 +66,7 @@ module Dibber
|
|
60
66
|
objects.each do |name, attributes|
|
61
67
|
object = find_or_initialize_by(name)
|
62
68
|
if overwrite or object.new_record?
|
63
|
-
object.send("#{attribute_method}=", attributes)
|
69
|
+
object.send("#{attribute_method}=", attributes)
|
64
70
|
unless object.save
|
65
71
|
self.class.errors << object.errors
|
66
72
|
end
|
@@ -86,14 +92,20 @@ module Dibber
|
|
86
92
|
end
|
87
93
|
|
88
94
|
private
|
95
|
+
def self.start_process_log
|
96
|
+
process_log = ProcessLog.new
|
97
|
+
process_log.start :time, 'Time.now.strftime("%Y-%b-%d %H:%M:%S.%3N %z")'
|
98
|
+
return process_log
|
99
|
+
end
|
100
|
+
|
89
101
|
def self.raise_no_seeds_path_error
|
90
102
|
raise "You must set the path to your seed files via Seeder.seeds_path = 'path/to/seed/files'"
|
91
103
|
end
|
92
|
-
|
104
|
+
|
93
105
|
def check_objects_exist
|
94
106
|
raise "No objects returned from file: #{self.class.seeds_path}#{file}" unless objects
|
95
107
|
end
|
96
|
-
|
108
|
+
|
97
109
|
def find_or_initialize_by(name)
|
98
110
|
if klass.exists?(name_method_sym => name)
|
99
111
|
klass.where(name_method_sym => name).first
|
@@ -101,7 +113,7 @@ module Dibber
|
|
101
113
|
klass.new(name_method_sym => name)
|
102
114
|
end
|
103
115
|
end
|
104
|
-
|
116
|
+
|
105
117
|
def name_method_sym
|
106
118
|
name_method.to_sym
|
107
119
|
end
|
data/lib/dibber/version.rb
CHANGED
@@ -1,12 +1,15 @@
|
|
1
1
|
module Dibber
|
2
|
-
VERSION = "0.
|
2
|
+
VERSION = "0.5.0"
|
3
3
|
end
|
4
4
|
|
5
5
|
# History
|
6
6
|
# =======
|
7
|
+
# 0.5.0 Allow the `Seeder.seed` method to take a Class or the string/symbol
|
8
|
+
# representation of the class name.
|
9
|
+
#
|
7
10
|
# 0.4.0 Removes dependency on find_or_initialize_by_name type ActiveRecord
|
8
11
|
# dynamic methods that are no longer supported in Rails
|
9
|
-
#
|
12
|
+
#
|
10
13
|
# 0.3.1 Adds error messages when object save fails
|
11
14
|
#
|
12
15
|
# 0.3.0 Adds seed method
|
@@ -19,11 +22,11 @@ end
|
|
19
22
|
#
|
20
23
|
# 0.2.1 Fixes bug in overwrite code
|
21
24
|
# System was not correctly identifying when an object was a new record
|
22
|
-
#
|
25
|
+
#
|
23
26
|
# 0.2.0 Stops overwriting existing entries unless explicitly directed to
|
24
27
|
# Now need to specify :overwrite => true when creating a new Seeder if
|
25
28
|
# existing entries are to be overwritten.
|
26
|
-
#
|
29
|
+
#
|
27
30
|
# 0.1.1 Working version
|
28
31
|
# No History before this point
|
29
32
|
#
|
@@ -1,13 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'test/unit'
|
1
|
+
require 'test_helper'
|
4
2
|
require 'dibber'
|
5
3
|
|
6
4
|
module Dibber
|
7
|
-
class ProcessLogTest < Test
|
5
|
+
class ProcessLogTest < Minitest::Test
|
8
6
|
def setup
|
9
7
|
@process_log = ProcessLog.new
|
10
|
-
|
11
8
|
end
|
12
9
|
|
13
10
|
def test_one
|
data/test/dibber/seeder_test.rb
CHANGED
@@ -1,12 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'test/unit'
|
4
|
-
require 'dibber'
|
1
|
+
require 'test_helper'
|
5
2
|
require_relative 'thing'
|
3
|
+
require_relative 'foo/bar'
|
6
4
|
|
7
5
|
module Dibber
|
8
6
|
|
9
|
-
class SeederTest < Test
|
7
|
+
class SeederTest < Minitest::Test
|
10
8
|
|
11
9
|
def setup
|
12
10
|
Seeder.seeds_path = File.join(File.dirname(__FILE__), 'seeds')
|
@@ -14,6 +12,7 @@ module Dibber
|
|
14
12
|
|
15
13
|
def teardown
|
16
14
|
Thing.clear_all
|
15
|
+
Foo::Bar.clear_all
|
17
16
|
end
|
18
17
|
|
19
18
|
def test_process_log
|
@@ -31,8 +30,15 @@ module Dibber
|
|
31
30
|
end
|
32
31
|
|
33
32
|
def test_monitor
|
33
|
+
Seeder.clear_process_log
|
34
34
|
Seeder.monitor(Thing)
|
35
|
-
assert_equal({:
|
35
|
+
assert_equal({command: 'Thing.count', start: Thing.count}, Seeder.process_log.raw[:things])
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_clear_process_log
|
39
|
+
test_monitor
|
40
|
+
Seeder.clear_process_log
|
41
|
+
assert_equal(nil, Seeder.process_log.raw[:things])
|
36
42
|
end
|
37
43
|
|
38
44
|
def test_seeds_path
|
@@ -56,7 +62,7 @@ module Dibber
|
|
56
62
|
|
57
63
|
def test_build_with_no_objects
|
58
64
|
thing_seeder = Seeder.new(Thing, 'empty.yml')
|
59
|
-
|
65
|
+
assert_raises RuntimeError do
|
60
66
|
thing_seeder.build
|
61
67
|
end
|
62
68
|
end
|
@@ -142,21 +148,71 @@ module Dibber
|
|
142
148
|
end
|
143
149
|
|
144
150
|
def test_seed_with_non_existent_class
|
145
|
-
|
151
|
+
assert_raises NameError do
|
146
152
|
Seeder.seed(:non_existent_class)
|
147
153
|
end
|
148
154
|
end
|
149
155
|
|
150
156
|
def test_seed_with_non_existent_seed_file
|
151
157
|
no_file_found_error = Errno::ENOENT
|
152
|
-
|
158
|
+
assert_raises no_file_found_error do
|
153
159
|
Seeder.seed(:array)
|
154
160
|
end
|
155
161
|
end
|
156
162
|
|
163
|
+
def test_seed_with_sub_class_string
|
164
|
+
assert_equal(0, Foo::Bar.count)
|
165
|
+
Seeder.seed('foo/bar')
|
166
|
+
assert_equal(1, Foo::Bar.count)
|
167
|
+
bar = Foo::Bar.find_or_initialize_by(name: :some)
|
168
|
+
assert_equal([bar], Foo::Bar.saved)
|
169
|
+
assert_equal({'title' => 'thing'}, bar.attributes)
|
170
|
+
end
|
171
|
+
|
172
|
+
def test_seed_with_class
|
173
|
+
assert_equal(0, Thing.count)
|
174
|
+
Seeder.seed(Thing)
|
175
|
+
assert_equal(2, Thing.count)
|
176
|
+
foo = Thing.find_or_initialize_by(name: :foo)
|
177
|
+
bar = Thing.find_or_initialize_by(name: :bar)
|
178
|
+
assert_equal([foo, bar], Thing.saved)
|
179
|
+
assert_equal({'title' => 'one'}, foo.attributes)
|
180
|
+
end
|
181
|
+
|
182
|
+
def test_seed_with_class_with_alternative_name_method
|
183
|
+
Seeder.seed(Thing, :name_method => 'other_method')
|
184
|
+
assert_equal(2, Thing.count)
|
185
|
+
foo = Thing.find_or_initialize_by(other_method: :foo)
|
186
|
+
bar = Thing.find_or_initialize_by(other_method: :bar)
|
187
|
+
assert_equal([foo, bar], Thing.saved)
|
188
|
+
assert_equal({'title' => 'one'}, foo.attributes)
|
189
|
+
end
|
190
|
+
|
191
|
+
def test_seed_with_class_that_does_not_exist
|
192
|
+
assert_raises NameError do
|
193
|
+
Seeder.seed(NonExistentClass)
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
def test_seed_with_class_with_non_existent_seed_file
|
198
|
+
no_file_found_error = Errno::ENOENT
|
199
|
+
assert_raises no_file_found_error do
|
200
|
+
Seeder.seed(Array)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
def test_seed_with_sub_class
|
205
|
+
assert_equal(0, Foo::Bar.count)
|
206
|
+
Seeder.seed(Foo::Bar)
|
207
|
+
assert_equal(1, Foo::Bar.count)
|
208
|
+
bar = Foo::Bar.find_or_initialize_by(name: :some)
|
209
|
+
assert_equal([bar], Foo::Bar.saved)
|
210
|
+
assert_equal({'title' => 'thing'}, bar.attributes)
|
211
|
+
end
|
212
|
+
|
157
213
|
def test_seeds_path_with_none_set
|
158
214
|
Seeder.seeds_path = nil
|
159
|
-
|
215
|
+
assert_raises RuntimeError do
|
160
216
|
Seeder.seeds_path
|
161
217
|
end
|
162
218
|
end
|
data/test/dibber/thing_test.rb
CHANGED
@@ -1,22 +1,20 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'test/unit'
|
1
|
+
require 'test_helper'
|
4
2
|
require_relative 'thing'
|
5
3
|
|
6
4
|
module Dibber
|
7
5
|
|
8
|
-
class ThingTest < Test
|
9
|
-
|
6
|
+
class ThingTest < Minitest::Test
|
7
|
+
|
10
8
|
def teardown
|
11
9
|
Thing.clear_all
|
12
10
|
end
|
13
|
-
|
11
|
+
|
14
12
|
def test_find_or_initialize_by_name
|
15
13
|
@name = 'foo'
|
16
14
|
@thing = Thing.find_or_initialize_by(name: @name)
|
17
15
|
assert_equal(@name, @thing.name)
|
18
16
|
end
|
19
|
-
|
17
|
+
|
20
18
|
def test_find_or_initialize_by_name_with_symbol
|
21
19
|
test_find_or_initialize_by_name
|
22
20
|
assert_equal(1, Thing.count)
|
@@ -24,33 +22,33 @@ module Dibber
|
|
24
22
|
assert_equal(1, Thing.count)
|
25
23
|
assert_equal('foo', thing.name)
|
26
24
|
end
|
27
|
-
|
25
|
+
|
28
26
|
def test_find_or_initialize_by_name_when_thing_exists
|
29
27
|
test_find_or_initialize_by_name
|
30
28
|
test_find_or_initialize_by_name
|
31
29
|
assert_equal(1, Thing.count)
|
32
30
|
end
|
33
|
-
|
31
|
+
|
34
32
|
def test_count
|
35
33
|
assert_equal(0, Thing.count)
|
36
34
|
Thing.find_or_initialize_by(name: :thing_for_count_test)
|
37
35
|
assert_equal(1, Thing.count)
|
38
36
|
end
|
39
|
-
|
37
|
+
|
40
38
|
def test_attributes
|
41
39
|
stuff = %w{this that}
|
42
40
|
test_find_or_initialize_by_name
|
43
41
|
@thing.attributes = stuff
|
44
42
|
assert_equal(stuff, @thing.attributes)
|
45
43
|
end
|
46
|
-
|
44
|
+
|
47
45
|
def test_other_method
|
48
46
|
stuff = %w{come home mother}
|
49
47
|
test_find_or_initialize_by_name
|
50
48
|
@thing.other_method = stuff
|
51
49
|
assert_equal(stuff, @thing.other_method)
|
52
50
|
end
|
53
|
-
|
51
|
+
|
54
52
|
def test_save_and_saved
|
55
53
|
thing = Thing.find_or_initialize_by(name: :thing_for_save_test)
|
56
54
|
assert !Thing.saved.include?(thing), 'saved things should not include thing'
|
@@ -59,23 +57,23 @@ module Dibber
|
|
59
57
|
thing.save
|
60
58
|
assert_equal(1, Thing.saved.select{|t| t == thing}.length)
|
61
59
|
end
|
62
|
-
|
60
|
+
|
63
61
|
def test_clear
|
64
62
|
test_find_or_initialize_by_name
|
65
63
|
@thing.save
|
66
|
-
|
67
|
-
|
64
|
+
refute_equal(0, Thing.count)
|
65
|
+
refute_equal([], Thing.saved)
|
68
66
|
Thing.clear_all
|
69
67
|
assert_equal(0, Thing.count)
|
70
68
|
assert_equal([], Thing.saved)
|
71
69
|
end
|
72
|
-
|
70
|
+
|
73
71
|
def test_find_or_initialize_by_other_method
|
74
72
|
thing = Thing.find_or_initialize_by(other_method: :something)
|
75
73
|
assert_nil(thing.name)
|
76
74
|
assert_equal('something', thing.other_method)
|
77
75
|
end
|
78
|
-
|
76
|
+
|
79
77
|
end
|
80
78
|
|
81
79
|
end
|
data/test/examples/seeds.rb
CHANGED
@@ -9,11 +9,11 @@ Seeder = Dibber::Seeder
|
|
9
9
|
# Set up the path to seed YAML files
|
10
10
|
Seeder.seeds_path = File.expand_path('seeds', File.dirname(__FILE__))
|
11
11
|
|
12
|
-
# Example 1. Seeder is used to monitor the process
|
12
|
+
# Example 1. Seeder is used to monitor the process
|
13
13
|
# and grab the attributes from the YAML file
|
14
14
|
Seeder.monitor Borough
|
15
15
|
Seeder.objects_from("boroughs.yml").each do |holder, borough|
|
16
|
-
Borough.
|
16
|
+
Borough.find_or_initialize_by(name: borough).save
|
17
17
|
end
|
18
18
|
|
19
19
|
# Example 2. Seeder is only used to monitor the process
|
@@ -21,15 +21,15 @@ Seeder.monitor AdminUser
|
|
21
21
|
admin_email = 'admin@undervale.co.uk'
|
22
22
|
password = 'change_me'
|
23
23
|
AdminUser.create!(
|
24
|
-
:
|
25
|
-
:
|
26
|
-
:
|
27
|
-
) unless AdminUser.exists?(:
|
28
|
-
|
29
|
-
# Example 3. Seeder grabs the attributes from the YAML and builds a
|
30
|
-
# set of Fee objects with those attributes (or updates them if
|
31
|
-
# they already exist).
|
32
|
-
# Note that the build process defaults to using a 'name' field to store
|
24
|
+
email: admin_email,
|
25
|
+
password: password,
|
26
|
+
password_confirmation: password
|
27
|
+
) unless AdminUser.exists?(email: admin_email)
|
28
|
+
|
29
|
+
# Example 3. Seeder grabs the attributes from the YAML and builds a
|
30
|
+
# set of Fee objects with those attributes (or updates them if
|
31
|
+
# they already exist).
|
32
|
+
# Note that the build process defaults to using a 'name' field to store
|
33
33
|
# the root key.
|
34
34
|
Seeder.new(Fee, 'fees.yml').build
|
35
35
|
|
@@ -38,7 +38,10 @@ Seeder.new(Fee, 'fees.yml', :name_method => :title).build
|
|
38
38
|
|
39
39
|
# Example 5. If the seed file's name is the lower case plural of the class name
|
40
40
|
# you can use the seed method:
|
41
|
-
Seeder.seed
|
41
|
+
Seeder.seed :fee
|
42
|
+
|
43
|
+
# Alternatively pass in the class:
|
44
|
+
Seeder.seed Fee
|
42
45
|
|
43
46
|
# Example 6. Seeder working with a name-spaced object
|
44
47
|
Seeder.new(Disclaimer::Document, 'disclaimer/documents.yml').build
|
@@ -46,7 +49,10 @@ Seeder.new(Disclaimer::Document, 'disclaimer/documents.yml').build
|
|
46
49
|
# Example 7. You can also use the seed method with name-spaced objects.
|
47
50
|
# In this case the seed files need to be in a name-spaced path (see previous
|
48
51
|
# example)
|
49
|
-
Seeder.seed
|
52
|
+
Seeder.seed 'disclaimer/document'
|
53
|
+
|
54
|
+
# or if you prefer to pass in the class:
|
55
|
+
Seeder.seed Disclaimer::Document
|
50
56
|
|
51
57
|
# Example 8. Seeder using values in the yaml file to set a single field
|
52
58
|
Seeder.seed(:category, 'description')
|
@@ -54,8 +60,8 @@ Seeder.seed(:category, 'description')
|
|
54
60
|
# Example 9. Seeder using alternative name and attributes fields
|
55
61
|
Seeder.seed(
|
56
62
|
:category,
|
57
|
-
:
|
58
|
-
:
|
63
|
+
name_method: :title,
|
64
|
+
attributes_method: :description
|
59
65
|
)
|
60
66
|
|
61
67
|
# Example 10. You can also access Seeders attached process log, and set up a
|
data/test/test_helper.rb
ADDED
metadata
CHANGED
@@ -1,29 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dibber
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rob Nichols
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-04-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.0'
|
27
41
|
description: Packages up code needed to pull data from YAML files when seeding, and
|
28
42
|
adds a process log.
|
29
43
|
email:
|
@@ -39,9 +53,11 @@ files:
|
|
39
53
|
- lib/dibber/process_log.rb
|
40
54
|
- lib/dibber/seeder.rb
|
41
55
|
- lib/dibber/version.rb
|
56
|
+
- test/dibber/foo/bar.rb
|
42
57
|
- test/dibber/process_log_test.rb
|
43
58
|
- test/dibber/seeder_test.rb
|
44
59
|
- test/dibber/seeds/empty.yml
|
60
|
+
- test/dibber/seeds/foo/bars.yml
|
45
61
|
- test/dibber/seeds/things.yml
|
46
62
|
- test/dibber/thing.rb
|
47
63
|
- test/dibber/thing_test.rb
|
@@ -57,6 +73,7 @@ files:
|
|
57
73
|
- test/examples/seeds/disclaimer/documents.yml
|
58
74
|
- test/examples/seeds/fees.yml
|
59
75
|
- test/not_quite_active_record.rb
|
76
|
+
- test/test_helper.rb
|
60
77
|
homepage: https://github.com/reggieb/Dibber
|
61
78
|
licenses:
|
62
79
|
- MIT-LICENSE
|
@@ -67,36 +84,39 @@ require_paths:
|
|
67
84
|
- lib
|
68
85
|
required_ruby_version: !ruby/object:Gem::Requirement
|
69
86
|
requirements:
|
70
|
-
- -
|
87
|
+
- - ">="
|
71
88
|
- !ruby/object:Gem::Version
|
72
89
|
version: '0'
|
73
90
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
91
|
requirements:
|
75
|
-
- -
|
92
|
+
- - ">="
|
76
93
|
- !ruby/object:Gem::Version
|
77
94
|
version: '0'
|
78
95
|
requirements: []
|
79
96
|
rubyforge_project:
|
80
|
-
rubygems_version: 2.
|
97
|
+
rubygems_version: 2.4.8
|
81
98
|
signing_key:
|
82
99
|
specification_version: 4
|
83
100
|
summary: Tool for seeding database from YAML.
|
84
101
|
test_files:
|
85
|
-
- test/
|
102
|
+
- test/dibber/foo/bar.rb
|
103
|
+
- test/dibber/seeder_test.rb
|
104
|
+
- test/dibber/thing.rb
|
105
|
+
- test/dibber/process_log_test.rb
|
106
|
+
- test/dibber/seeds/foo/bars.yml
|
107
|
+
- test/dibber/seeds/things.yml
|
108
|
+
- test/dibber/seeds/empty.yml
|
109
|
+
- test/dibber/thing_test.rb
|
110
|
+
- test/examples/seeds.rb
|
111
|
+
- test/examples/models/borough.rb
|
86
112
|
- test/examples/models/disclaimer.rb
|
87
|
-
- test/examples/models/fee.rb
|
88
113
|
- test/examples/models/category.rb
|
89
|
-
- test/examples/models/
|
90
|
-
- test/examples/
|
114
|
+
- test/examples/models/fee.rb
|
115
|
+
- test/examples/models/admin_user.rb
|
91
116
|
- test/examples/seeds/fees.yml
|
117
|
+
- test/examples/seeds/categories.yml
|
92
118
|
- test/examples/seeds/disclaimer/documents.yml
|
93
119
|
- test/examples/seeds/boroughs.yml
|
94
|
-
- test/examples/seeds.rb
|
95
120
|
- test/examples/process_logs.rb
|
96
121
|
- test/not_quite_active_record.rb
|
97
|
-
- test/
|
98
|
-
- test/dibber/thing_test.rb
|
99
|
-
- test/dibber/seeds/empty.yml
|
100
|
-
- test/dibber/seeds/things.yml
|
101
|
-
- test/dibber/thing.rb
|
102
|
-
- test/dibber/process_log_test.rb
|
122
|
+
- test/test_helper.rb
|