dibber 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +21 -0
- data/lib/dibber/seeder.rb +12 -3
- data/lib/dibber/version.rb +4 -1
- data/test/dibber/dynamic_thing_test.rb +18 -0
- data/test/dibber/seeder_test.rb +13 -3
- data/test/dibber/seeds/dynamic_things.yml +2 -0
- metadata +16 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4e8e76d2b2715019b02b55446e6445a4f12fd16c
|
4
|
+
data.tar.gz: f4fd83aabdc6286b9c3a64b2fa50ce017eff50fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d8bfef835c9cf35d2f556e4c731b3e0a396faacc1e7a5e3061b3b9b8d6b9a10efb96ae1d00b1a54aa5c853548eabfa325bd778771ce6d41aa13c97a874ac582b
|
7
|
+
data.tar.gz: 18dabf89066e0cf69f0763ebfee6ac333af2e27a932bb68003bb09f869911d3ec6e28b73a7f480298ba49e151555b44445f7ea86c3a19368504c35cf47823bf9
|
data/README.rdoc
CHANGED
@@ -48,6 +48,27 @@ You'll then be able to do this:
|
|
48
48
|
thing = Thing.find_by(name: 'foo')
|
49
49
|
thing.colour ---> 'red'
|
50
50
|
|
51
|
+
=== Pass the name of the class to seed
|
52
|
+
|
53
|
+
All of these are equivalent commands
|
54
|
+
|
55
|
+
Dibber::Seeder.seed Thing
|
56
|
+
Dibber::Seeder.seed :thing
|
57
|
+
Dibber::Seeder.seed 'Thing'
|
58
|
+
|
59
|
+
=== Dynamic content
|
60
|
+
|
61
|
+
Seed content can also have dynamic content added via ERB. For example, if
|
62
|
+
you want the colour of the `foo` thing to be either 'red' or 'yellow', change
|
63
|
+
`db/seeds/things.yml` to
|
64
|
+
|
65
|
+
foo:
|
66
|
+
colour: <%= ['red', 'yellow'].sample %>
|
67
|
+
size: large
|
68
|
+
|
69
|
+
Then when we run `Dibber::Seeder.seed :thing` a new `Thing` will be created
|
70
|
+
with the colour set to either 'red' or 'yellow'.
|
71
|
+
|
51
72
|
== Report
|
52
73
|
|
53
74
|
Seeder.report outputs a report detailing start and end time, and a log of how
|
data/lib/dibber/seeder.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'active_support/inflector'
|
2
2
|
require 'yaml'
|
3
|
+
require 'erb'
|
3
4
|
|
4
5
|
module Dibber
|
5
6
|
class Seeder
|
@@ -9,7 +10,7 @@ module Dibber
|
|
9
10
|
|
10
11
|
def seed(klass, args = {})
|
11
12
|
if klass.kind_of?(String) || klass.kind_of?(Symbol)
|
12
|
-
name = klass.to_s
|
13
|
+
name = klass.to_s.underscore
|
13
14
|
class_name = klass.to_s.strip.classify
|
14
15
|
klass = Kernel.const_get(class_name)
|
15
16
|
else
|
@@ -39,7 +40,15 @@ module Dibber
|
|
39
40
|
end
|
40
41
|
|
41
42
|
def objects_from(file)
|
42
|
-
YAML.
|
43
|
+
YAML.load erb_processed(file_content(file))
|
44
|
+
end
|
45
|
+
|
46
|
+
def erb_processed(content)
|
47
|
+
ERB.new(content).result
|
48
|
+
end
|
49
|
+
|
50
|
+
def file_content(file)
|
51
|
+
File.read File.join(seeds_path, file)
|
43
52
|
end
|
44
53
|
|
45
54
|
def seeds_path
|
@@ -130,4 +139,4 @@ module Dibber
|
|
130
139
|
end
|
131
140
|
|
132
141
|
end
|
133
|
-
end
|
142
|
+
end
|
data/lib/dibber/version.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
module Dibber
|
2
|
-
VERSION = "0.
|
2
|
+
VERSION = "0.6.0"
|
3
3
|
end
|
4
4
|
|
5
5
|
# History
|
6
6
|
# =======
|
7
|
+
# 0.6.0 ERB parse the source files before passing it to YAML. Allows dynamic
|
8
|
+
# content to be used.
|
9
|
+
#
|
7
10
|
# 0.5.0 Allow the `Seeder.seed` method to take a Class or the string/symbol
|
8
11
|
# representation of the class name.
|
9
12
|
#
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require_relative 'thing'
|
3
|
+
|
4
|
+
module Dibber
|
5
|
+
class DynamicThingTest < Minitest::Test
|
6
|
+
|
7
|
+
def setup
|
8
|
+
Seeder.seeds_path = File.join(File.dirname(__FILE__), 'seeds')
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
def test_objects_from
|
13
|
+
hash = Seeder.objects_from('dynamic_things.yml')
|
14
|
+
assert_equal 'something', hash['one']['title']
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
data/test/dibber/seeder_test.rb
CHANGED
@@ -38,7 +38,7 @@ module Dibber
|
|
38
38
|
def test_clear_process_log
|
39
39
|
test_monitor
|
40
40
|
Seeder.clear_process_log
|
41
|
-
|
41
|
+
assert_nil Seeder.process_log.raw[:things], "Log should be empty"
|
42
42
|
end
|
43
43
|
|
44
44
|
def test_seeds_path
|
@@ -179,7 +179,17 @@ module Dibber
|
|
179
179
|
assert_equal({'title' => 'one'}, foo.attributes)
|
180
180
|
end
|
181
181
|
|
182
|
-
|
182
|
+
def test_seed_with_class_name
|
183
|
+
assert_equal(0, Thing.count)
|
184
|
+
Seeder.seed('Thing')
|
185
|
+
assert_equal(2, Thing.count)
|
186
|
+
foo = Thing.find_or_initialize_by(name: :foo)
|
187
|
+
bar = Thing.find_or_initialize_by(name: :bar)
|
188
|
+
assert_equal([foo, bar], Thing.saved)
|
189
|
+
assert_equal({'title' => 'one'}, foo.attributes)
|
190
|
+
end
|
191
|
+
|
192
|
+
def test_seed_with_class_with_alternative_name_method
|
183
193
|
Seeder.seed(Thing, :name_method => 'other_method')
|
184
194
|
assert_equal(2, Thing.count)
|
185
195
|
foo = Thing.find_or_initialize_by(other_method: :foo)
|
@@ -244,7 +254,7 @@ module Dibber
|
|
244
254
|
end
|
245
255
|
|
246
256
|
def thing_file_path
|
247
|
-
File.join(File.dirname(__FILE__),'seeds', 'things.yml')
|
257
|
+
File.join(File.dirname(__FILE__), 'seeds', 'things.yml')
|
248
258
|
end
|
249
259
|
end
|
250
260
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dibber
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.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: 2017-03-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -53,9 +53,11 @@ files:
|
|
53
53
|
- lib/dibber/process_log.rb
|
54
54
|
- lib/dibber/seeder.rb
|
55
55
|
- lib/dibber/version.rb
|
56
|
+
- test/dibber/dynamic_thing_test.rb
|
56
57
|
- test/dibber/foo/bar.rb
|
57
58
|
- test/dibber/process_log_test.rb
|
58
59
|
- test/dibber/seeder_test.rb
|
60
|
+
- test/dibber/seeds/dynamic_things.yml
|
59
61
|
- test/dibber/seeds/empty.yml
|
60
62
|
- test/dibber/seeds/foo/bars.yml
|
61
63
|
- test/dibber/seeds/things.yml
|
@@ -99,24 +101,26 @@ signing_key:
|
|
99
101
|
specification_version: 4
|
100
102
|
summary: Tool for seeding database from YAML.
|
101
103
|
test_files:
|
102
|
-
- test/dibber/foo/bar.rb
|
103
|
-
- test/dibber/seeder_test.rb
|
104
104
|
- test/dibber/thing.rb
|
105
|
+
- test/dibber/seeder_test.rb
|
106
|
+
- test/dibber/dynamic_thing_test.rb
|
107
|
+
- test/dibber/foo/bar.rb
|
105
108
|
- test/dibber/process_log_test.rb
|
109
|
+
- test/dibber/thing_test.rb
|
110
|
+
- test/dibber/seeds/dynamic_things.yml
|
111
|
+
- test/dibber/seeds/empty.yml
|
106
112
|
- test/dibber/seeds/foo/bars.yml
|
107
113
|
- test/dibber/seeds/things.yml
|
108
|
-
- test/
|
109
|
-
- test/dibber/thing_test.rb
|
110
|
-
- test/examples/seeds.rb
|
114
|
+
- test/examples/process_logs.rb
|
111
115
|
- test/examples/models/borough.rb
|
112
|
-
- test/examples/models/disclaimer.rb
|
113
|
-
- test/examples/models/category.rb
|
114
116
|
- test/examples/models/fee.rb
|
117
|
+
- test/examples/models/disclaimer.rb
|
115
118
|
- test/examples/models/admin_user.rb
|
119
|
+
- test/examples/models/category.rb
|
120
|
+
- test/examples/seeds.rb
|
121
|
+
- test/examples/seeds/boroughs.yml
|
116
122
|
- test/examples/seeds/fees.yml
|
117
123
|
- test/examples/seeds/categories.yml
|
118
124
|
- test/examples/seeds/disclaimer/documents.yml
|
119
|
-
- test/examples/seeds/boroughs.yml
|
120
|
-
- test/examples/process_logs.rb
|
121
|
-
- test/not_quite_active_record.rb
|
122
125
|
- test/test_helper.rb
|
126
|
+
- test/not_quite_active_record.rb
|