sprig 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +33 -11
- data/lib/generators/sprig/install_generator.rb +5 -1
- data/lib/sprig.rb +5 -1
- data/lib/sprig/configuration.rb +19 -2
- data/lib/sprig/helpers.rb +15 -3
- data/lib/sprig/version.rb +8 -1
- data/spec/feature/configurations_spec.rb +5 -5
- data/spec/fixtures/seeds/shared/comments.yml +4 -0
- data/spec/fixtures/seeds/shared/files/cat.png +0 -0
- data/spec/fixtures/seeds/shared/invalid_users.yml +4 -0
- data/spec/fixtures/seeds/shared/legacy_posts.yml +4 -0
- data/spec/fixtures/seeds/shared/posts.csv +2 -0
- data/spec/fixtures/seeds/shared/posts.json +1 -0
- data/spec/fixtures/seeds/shared/posts.md +5 -0
- data/spec/fixtures/seeds/shared/posts.yml +4 -0
- data/spec/fixtures/seeds/shared/posts_delete_existing_by.yml +7 -0
- data/spec/fixtures/seeds/shared/posts_find_existing_by_missing.yml +7 -0
- data/spec/fixtures/seeds/shared/posts_find_existing_by_multiple.yml +8 -0
- data/spec/fixtures/seeds/shared/posts_find_existing_by_single.yml +7 -0
- data/spec/fixtures/seeds/shared/posts_missing_dependency.yml +5 -0
- data/spec/fixtures/seeds/shared/posts_missing_record.yml +5 -0
- data/spec/fixtures/seeds/shared/posts_partially_dynamic_value.yml +4 -0
- data/spec/fixtures/seeds/shared/posts_with_cyclic_dependencies.yml +4 -0
- data/spec/fixtures/seeds/shared/posts_with_files.yml +5 -0
- data/spec/fixtures/seeds/shared/posts_with_habtm.yml +7 -0
- data/spec/fixtures/seeds/shared/tags.yml +5 -0
- data/spec/lib/generators/sprig/install_generator_spec.rb +6 -2
- data/spec/lib/sprig/configuration_spec.rb +6 -6
- data/spec/lib/sprig/directive_list_spec.rb +4 -4
- data/spec/lib/sprig/directive_spec.rb +19 -9
- data/spec/lib/sprig/null_record_spec.rb +2 -2
- data/spec/lib/sprig/parser/base_spec.rb +1 -1
- data/spec/lib/sprig/process_notifier_spec.rb +1 -1
- data/spec/lib/sprig/seed/entry_spec.rb +3 -3
- data/spec/lib/sprig/seed/record_spec.rb +3 -3
- data/spec/lib/sprig/source_spec.rb +2 -2
- data/spec/lib/sprig_spec.rb +8 -8
- data/spec/spec_helper.rb +24 -8
- data/spec/sprig_shared_spec.rb +467 -0
- data/spec/sprig_spec.rb +37 -37
- data/spec/support/helpers/logger_mock.rb +2 -1
- metadata +89 -58
- data/lib/sprig/data.rb +0 -6
- data/spec/db/activerecord.db +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a451bb75a9f2b2b04daafc72e40c2a92340d155
|
4
|
+
data.tar.gz: eb289195cee146a5b58709e215e2cc10e9c7916d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3978471e61ded01e7372b921c996d20434c91e012cd1157be3c2e1c9e46de827478c33382289d5476fcff399d47ad63fca3a88b2274e38e687e7d8b128869820
|
7
|
+
data.tar.gz: 16d1051e32a5d2c090f501c517bc7b87a20975c82b72713937fd75a009f95d1cdbaed62722eac150cc404cdce9f1626ae17d0071b7d71bfa287cd00be78dd16a
|
data/README.md
CHANGED
@@ -14,9 +14,9 @@ Add into your Gemfile
|
|
14
14
|
```ruby
|
15
15
|
gem "sprig"
|
16
16
|
```
|
17
|
-
Use `rails generate sprig:install` to create environment-specific seed directories.
|
17
|
+
Use `rails generate sprig:install` to create environment-specific and shared seed directories.
|
18
18
|
|
19
|
-
##The Sprig Directive
|
19
|
+
## The Sprig Directive
|
20
20
|
|
21
21
|
Within your seed file, you can use the `sprig` directive to initiate Sprig's dark magicks. A simple directive might look like this.
|
22
22
|
|
@@ -26,17 +26,30 @@ Within your seed file, you can use the `sprig` directive to initiate Sprig's dar
|
|
26
26
|
include Sprig::Helpers
|
27
27
|
|
28
28
|
sprig [User, Post, Comment]
|
29
|
+
|
30
|
+
For shared seeds:
|
31
|
+
sprig_shared [User, Post, Comment]
|
29
32
|
```
|
30
33
|
|
31
34
|
This directive tells Sprig to go find your datafiles for the `User`, `Post`, and `Comment` seed resources, build records from the data entries, and insert them into the database. Sprig will automatically detect known datafile types like `.yml`, `.json`, or `.csv` within your environment-specific seed directory.
|
32
35
|
|
33
|
-
##Environment
|
36
|
+
## Environment
|
34
37
|
|
35
38
|
Seed files are unique to the environment in which your Rails application is running. Within `db/seeds` create an environment-specific directory (i.e. `/development` for your 'development' environment).
|
36
39
|
|
37
|
-
|
40
|
+
### Shared
|
41
|
+
|
42
|
+
Shared seed files default directory is `shared` (eg `db/seeds/shared`)
|
43
|
+
You can change it by settings`
|
44
|
+
|
45
|
+
To insert env specific together with shared seeds use:
|
46
|
+
```ruby
|
47
|
+
sprig [User]
|
48
|
+
sprig_shared [User]
|
49
|
+
```
|
50
|
+
This will insert `:env/users` and `shared/users` seeds
|
38
51
|
|
39
|
-
##Seed files
|
52
|
+
## Seed files
|
40
53
|
|
41
54
|
Hang your seed definitions on a `records` key for *yaml* and *json* files.
|
42
55
|
|
@@ -50,7 +63,7 @@ records:
|
|
50
63
|
first_name: 'Lawson'
|
51
64
|
last_name: 'Kurtz'
|
52
65
|
username: 'lawdawg'
|
53
|
-
- sprig_id:
|
66
|
+
- sprig_id: 'ryan' # Note: Sprig IDs can be integers or strings
|
54
67
|
first_name: 'Ryan'
|
55
68
|
last_name: 'Foster'
|
56
69
|
username: 'mc_rubs'
|
@@ -96,7 +109,7 @@ For `has_and_belongs_to_many` (HABTM) relationships, you may define relation ids
|
|
96
109
|
#posts.yml
|
97
110
|
|
98
111
|
records:
|
99
|
-
- sprig_id:
|
112
|
+
- sprig_id: 42
|
100
113
|
title: 'All About Brains'
|
101
114
|
content: 'Lorem ipsum...'
|
102
115
|
tag_ids:
|
@@ -107,9 +120,9 @@ records:
|
|
107
120
|
#tags.yml
|
108
121
|
|
109
122
|
records:
|
110
|
-
- sprig_id:
|
123
|
+
- sprig_id: 'bio'
|
111
124
|
name: 'Biology'
|
112
|
-
- sprig_id:
|
125
|
+
- sprig_id: 'neuro'
|
113
126
|
name: 'Neuroscience'
|
114
127
|
```
|
115
128
|
**Note: For namespaced or STI classes, you'll need to include the namespace with the class name in the seed file name. For example `Users::HeadManager` would need to be `users_head_managers.yml`**
|
@@ -146,7 +159,7 @@ records:
|
|
146
159
|
published_at: "<%= 1.week.ago %>"
|
147
160
|
```
|
148
161
|
|
149
|
-
##Custom Sources and Parsers
|
162
|
+
## Custom Sources and Parsers
|
150
163
|
|
151
164
|
If all your data is in `.wat` files, fear not. You can tell Sprig where to look for your data, and point it toward a custom parser class for turning your data into records. The example below tells Sprig to read `User` seed data from a Google Spreadsheet, and parse it accordingly.
|
152
165
|
|
@@ -166,13 +179,14 @@ sprig [
|
|
166
179
|
]
|
167
180
|
```
|
168
181
|
|
169
|
-
##Configuration
|
182
|
+
## Configuration
|
170
183
|
|
171
184
|
When Sprig conventions don't suit, just add a configuration block to your seed file.
|
172
185
|
|
173
186
|
```ruby
|
174
187
|
Sprig.configure do |c|
|
175
188
|
c.directory = 'seed_files'
|
189
|
+
c.shared_directory = 'shared'
|
176
190
|
end
|
177
191
|
```
|
178
192
|
|
@@ -186,3 +200,11 @@ details on usage.
|
|
186
200
|
## License
|
187
201
|
|
188
202
|
This project rocks and uses MIT-LICENSE.
|
203
|
+
|
204
|
+
***
|
205
|
+
|
206
|
+
<a href="http://code.viget.com">
|
207
|
+
<img src="http://code.viget.com/github-banner.png" alt="Code At Viget">
|
208
|
+
</a>
|
209
|
+
|
210
|
+
Visit [code.viget.com](http://code.viget.com) to see more projects from [Viget.](https://viget.com)
|
@@ -9,12 +9,16 @@ module Sprig
|
|
9
9
|
|
10
10
|
def create_enviroment_directories
|
11
11
|
empty_directory 'db/seeds'
|
12
|
-
|
12
|
+
create_shared_directory
|
13
13
|
envs.each { |env| empty_directory "db/seeds/#{env}" }
|
14
14
|
end
|
15
15
|
|
16
16
|
private
|
17
17
|
|
18
|
+
def create_shared_directory
|
19
|
+
empty_directory 'db/seeds/shared'
|
20
|
+
end
|
21
|
+
|
18
22
|
def envs
|
19
23
|
arg_envs ? arg_envs : default_envs
|
20
24
|
end
|
data/lib/sprig.rb
CHANGED
@@ -19,7 +19,7 @@ module Sprig
|
|
19
19
|
autoload :Seed, 'sprig/seed'
|
20
20
|
|
21
21
|
class << self
|
22
|
-
attr_writer :adapter
|
22
|
+
attr_writer :adapter, :shared_seeding
|
23
23
|
|
24
24
|
def adapter
|
25
25
|
@adapter ||= :active_record
|
@@ -36,6 +36,10 @@ module Sprig
|
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
+
def shared_seeding
|
40
|
+
@shared_seeding ||= false
|
41
|
+
end
|
42
|
+
|
39
43
|
def configuration
|
40
44
|
@@configuration ||= Sprig::Configuration.new
|
41
45
|
end
|
data/lib/sprig/configuration.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
module Sprig
|
2
2
|
class Configuration
|
3
3
|
|
4
|
-
attr_writer :directory, :logger
|
4
|
+
attr_writer :directory, :shared_directory, :logger
|
5
5
|
|
6
6
|
def directory
|
7
|
-
Rails.root.join(@directory || default_directory,
|
7
|
+
Rails.root.join(@directory || default_directory, seeds_directory)
|
8
8
|
end
|
9
9
|
|
10
10
|
def logger
|
@@ -16,5 +16,22 @@ module Sprig
|
|
16
16
|
def default_directory
|
17
17
|
'db/seeds'
|
18
18
|
end
|
19
|
+
|
20
|
+
def seeds_directory
|
21
|
+
return shared_seeds_directory if Sprig.shared_seeding
|
22
|
+
env_seeds_directory
|
23
|
+
end
|
24
|
+
|
25
|
+
def env_seeds_directory
|
26
|
+
Rails.env
|
27
|
+
end
|
28
|
+
|
29
|
+
def shared_seeds_directory
|
30
|
+
@shared_directory || default_shared_directory
|
31
|
+
end
|
32
|
+
|
33
|
+
def default_shared_directory
|
34
|
+
'shared'
|
35
|
+
end
|
19
36
|
end
|
20
37
|
end
|
data/lib/sprig/helpers.rb
CHANGED
@@ -5,9 +5,13 @@ module Sprig
|
|
5
5
|
end
|
6
6
|
|
7
7
|
def sprig(directive_definitions)
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
Sprig.shared_seeding = false
|
9
|
+
plant_records(directive_definitions)
|
10
|
+
end
|
11
|
+
|
12
|
+
def sprig_shared(directive_definitions)
|
13
|
+
Sprig.shared_seeding = true
|
14
|
+
plant_records(directive_definitions)
|
11
15
|
end
|
12
16
|
|
13
17
|
def sprig_record(klass, seed_id)
|
@@ -19,5 +23,13 @@ module Sprig
|
|
19
23
|
def sprig_file(relative_path)
|
20
24
|
File.new(seed_directory.join('files', relative_path))
|
21
25
|
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def plant_records(directive_definitions)
|
30
|
+
hopper = []
|
31
|
+
DirectiveList.new(directive_definitions).add_seeds_to_hopper(hopper)
|
32
|
+
Planter.new(hopper).sprig
|
33
|
+
end
|
22
34
|
end
|
23
35
|
end
|
data/lib/sprig/version.rb
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe "Configurating Sprig" do
|
3
|
+
RSpec.describe "Configurating Sprig" do
|
4
4
|
before do
|
5
5
|
stub_rails_root '~'
|
6
6
|
stub_rails_env 'development'
|
7
7
|
end
|
8
8
|
|
9
9
|
it "can set the directory" do
|
10
|
-
Sprig.configuration.directory.to_path.
|
10
|
+
expect(Sprig.configuration.directory.to_path).to eq('~/db/seeds/development')
|
11
11
|
|
12
12
|
Sprig.configure do |c|
|
13
13
|
c.directory = 'seed_files'
|
14
14
|
end
|
15
15
|
|
16
|
-
Sprig.configuration.directory.to_path.
|
16
|
+
expect(Sprig.configuration.directory.to_path).to eq('~/seed_files/development')
|
17
17
|
end
|
18
18
|
|
19
19
|
it "can reset the configuration" do
|
@@ -21,10 +21,10 @@ describe "Configurating Sprig" do
|
|
21
21
|
c.directory = 'seed_files'
|
22
22
|
end
|
23
23
|
|
24
|
-
Sprig.configuration.directory.to_path.
|
24
|
+
expect(Sprig.configuration.directory.to_path).to eq('~/seed_files/development')
|
25
25
|
|
26
26
|
Sprig.reset_configuration
|
27
27
|
|
28
|
-
Sprig.configuration.directory.to_path.
|
28
|
+
expect(Sprig.configuration.directory.to_path).to eq('~/db/seeds/development')
|
29
29
|
end
|
30
30
|
end
|
Binary file
|
@@ -0,0 +1 @@
|
|
1
|
+
{"records":[{"sprig_id":1,"title":"Json title","content":"Json content"}]}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'generators/sprig/install_generator'
|
3
3
|
|
4
|
-
describe Sprig::Generators::InstallGenerator, type: :generator do
|
4
|
+
RSpec.describe Sprig::Generators::InstallGenerator, type: :generator do
|
5
5
|
destination File.expand_path("../../tmp", __FILE__)
|
6
6
|
|
7
7
|
before do
|
@@ -14,6 +14,10 @@ describe Sprig::Generators::InstallGenerator, type: :generator do
|
|
14
14
|
assert_directory "db/seeds"
|
15
15
|
end
|
16
16
|
|
17
|
+
it "creates empty shared directory" do
|
18
|
+
assert_directory "db/seeds/shared"
|
19
|
+
end
|
20
|
+
|
17
21
|
it "creates empty seed environment directories" do
|
18
22
|
[
|
19
23
|
:development,
|
@@ -29,7 +33,7 @@ end
|
|
29
33
|
# Generator arguments are set on a class basis. We need to open
|
30
34
|
# a new describe block to make these examples work.
|
31
35
|
|
32
|
-
describe Sprig::Generators::InstallGenerator, type: :generator do
|
36
|
+
RSpec.describe Sprig::Generators::InstallGenerator, type: :generator do
|
33
37
|
context "with arguments" do
|
34
38
|
destination File.expand_path("../../tmp", __FILE__)
|
35
39
|
arguments %w(development test integration)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Sprig::Configuration do
|
3
|
+
RSpec.describe Sprig::Configuration do
|
4
4
|
before do
|
5
5
|
stub_rails_root '~'
|
6
6
|
stub_rails_env 'development'
|
@@ -8,29 +8,29 @@ describe Sprig::Configuration do
|
|
8
8
|
|
9
9
|
describe "#directory" do
|
10
10
|
it "returns db/seeds/:env by default" do
|
11
|
-
subject.directory.to_path.
|
11
|
+
expect(subject.directory.to_path).to eq('~/db/seeds/development')
|
12
12
|
end
|
13
13
|
|
14
14
|
it "returns a custom directory" do
|
15
15
|
subject.directory = 'seed_files'
|
16
16
|
|
17
|
-
subject.directory.to_path.
|
17
|
+
expect(subject.directory.to_path).to eq('~/seed_files/development')
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
21
|
describe "#logger" do
|
22
22
|
it "returns an stdout logger by default" do
|
23
23
|
logger = double('Logger')
|
24
|
-
Logger.
|
24
|
+
allow(Logger).to receive(:new).with($stdout).and_return(logger)
|
25
25
|
|
26
|
-
subject.logger.
|
26
|
+
expect(subject.logger).to eq(logger)
|
27
27
|
end
|
28
28
|
|
29
29
|
it "returns a custom logger" do
|
30
30
|
logger = double('Logger')
|
31
31
|
subject.logger = logger
|
32
32
|
|
33
|
-
subject.logger.
|
33
|
+
expect(subject.logger).to eq(logger)
|
34
34
|
end
|
35
35
|
end
|
36
36
|
end
|