sprig 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +33 -11
  3. data/lib/generators/sprig/install_generator.rb +5 -1
  4. data/lib/sprig.rb +5 -1
  5. data/lib/sprig/configuration.rb +19 -2
  6. data/lib/sprig/helpers.rb +15 -3
  7. data/lib/sprig/version.rb +8 -1
  8. data/spec/feature/configurations_spec.rb +5 -5
  9. data/spec/fixtures/seeds/shared/comments.yml +4 -0
  10. data/spec/fixtures/seeds/shared/files/cat.png +0 -0
  11. data/spec/fixtures/seeds/shared/invalid_users.yml +4 -0
  12. data/spec/fixtures/seeds/shared/legacy_posts.yml +4 -0
  13. data/spec/fixtures/seeds/shared/posts.csv +2 -0
  14. data/spec/fixtures/seeds/shared/posts.json +1 -0
  15. data/spec/fixtures/seeds/shared/posts.md +5 -0
  16. data/spec/fixtures/seeds/shared/posts.yml +4 -0
  17. data/spec/fixtures/seeds/shared/posts_delete_existing_by.yml +7 -0
  18. data/spec/fixtures/seeds/shared/posts_find_existing_by_missing.yml +7 -0
  19. data/spec/fixtures/seeds/shared/posts_find_existing_by_multiple.yml +8 -0
  20. data/spec/fixtures/seeds/shared/posts_find_existing_by_single.yml +7 -0
  21. data/spec/fixtures/seeds/shared/posts_missing_dependency.yml +5 -0
  22. data/spec/fixtures/seeds/shared/posts_missing_record.yml +5 -0
  23. data/spec/fixtures/seeds/shared/posts_partially_dynamic_value.yml +4 -0
  24. data/spec/fixtures/seeds/shared/posts_with_cyclic_dependencies.yml +4 -0
  25. data/spec/fixtures/seeds/shared/posts_with_files.yml +5 -0
  26. data/spec/fixtures/seeds/shared/posts_with_habtm.yml +7 -0
  27. data/spec/fixtures/seeds/shared/tags.yml +5 -0
  28. data/spec/lib/generators/sprig/install_generator_spec.rb +6 -2
  29. data/spec/lib/sprig/configuration_spec.rb +6 -6
  30. data/spec/lib/sprig/directive_list_spec.rb +4 -4
  31. data/spec/lib/sprig/directive_spec.rb +19 -9
  32. data/spec/lib/sprig/null_record_spec.rb +2 -2
  33. data/spec/lib/sprig/parser/base_spec.rb +1 -1
  34. data/spec/lib/sprig/process_notifier_spec.rb +1 -1
  35. data/spec/lib/sprig/seed/entry_spec.rb +3 -3
  36. data/spec/lib/sprig/seed/record_spec.rb +3 -3
  37. data/spec/lib/sprig/source_spec.rb +2 -2
  38. data/spec/lib/sprig_spec.rb +8 -8
  39. data/spec/spec_helper.rb +24 -8
  40. data/spec/sprig_shared_spec.rb +467 -0
  41. data/spec/sprig_spec.rb +37 -37
  42. data/spec/support/helpers/logger_mock.rb +2 -1
  43. metadata +89 -58
  44. data/lib/sprig/data.rb +0 -6
  45. data/spec/db/activerecord.db +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d8c76050cd88f10b7aa5734b205c1bdc5afbc5ea
4
- data.tar.gz: ffeebeb96d52c7fa0cd0ba39c9c0fdf0d337c247
3
+ metadata.gz: 2a451bb75a9f2b2b04daafc72e40c2a92340d155
4
+ data.tar.gz: eb289195cee146a5b58709e215e2cc10e9c7916d
5
5
  SHA512:
6
- metadata.gz: a35c299bee763d65c441323002da0b3f74405a2ecd9a831bfa5e108c033b3a3c129f151649e878293ea3f27d060265030054ec65ee0e252411fb86cffb3a6bb7
7
- data.tar.gz: 99024ba29c6b31096a3a18332b31e1ba1594d062009cdba4731c592d39edc64b5bb4a1ceeff2632dbb2fd3bdf9d28b1adc9dc14912073b696cb5c89961a22a4d
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
- Todo: [Support for shared seed files]
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: 2
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: 1
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: 1
123
+ - sprig_id: 'bio'
111
124
  name: 'Biology'
112
- - sprig_id: 2
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
@@ -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
@@ -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, Rails.env)
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
@@ -5,9 +5,13 @@ module Sprig
5
5
  end
6
6
 
7
7
  def sprig(directive_definitions)
8
- hopper = []
9
- DirectiveList.new(directive_definitions).add_seeds_to_hopper(hopper)
10
- Planter.new(hopper).sprig
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
@@ -1,3 +1,10 @@
1
1
  module Sprig
2
- VERSION = "0.2.0"
2
+
3
+ #:nocov:
4
+ VERSION = [
5
+ 0, # major
6
+ 3, # minor
7
+ 0 # patch
8
+ ].join('.')
9
+ #:nocov:
3
10
  end
@@ -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.should == '~/db/seeds/development'
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.should == '~/seed_files/development'
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.should == '~/seed_files/development'
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.should == '~/db/seeds/development'
28
+ expect(Sprig.configuration.directory.to_path).to eq('~/db/seeds/development')
29
29
  end
30
30
  end
@@ -0,0 +1,4 @@
1
+ records:
2
+ - sprig_id: 1
3
+ post_id: "<%= sprig_record(Post, 1).id %>"
4
+ body: "Yaml Comment body"
@@ -0,0 +1,4 @@
1
+ records:
2
+ - sprig_id: 1
3
+ first_name: "Clark"
4
+ # intentionally leaving out last_name so record fails to create
@@ -0,0 +1,4 @@
1
+ records:
2
+ - sprig_id: 'l_1'
3
+ title: 'Legacy yaml title'
4
+ content: 'Legacy yaml content'
@@ -0,0 +1,2 @@
1
+ sprig_id,title,content
2
+ 1,Csv title,Csv content
@@ -0,0 +1 @@
1
+ {"records":[{"sprig_id":1,"title":"Json title","content":"Json content"}]}
@@ -0,0 +1,5 @@
1
+ # Posts
2
+
3
+ - Post 1
4
+ - Post 2
5
+ - Post 3
@@ -0,0 +1,4 @@
1
+ records:
2
+ - sprig_id: 1
3
+ title: 'Yaml title'
4
+ content: 'Yaml content'
@@ -0,0 +1,7 @@
1
+ options:
2
+ delete_existing_by: 'title'
3
+
4
+ records:
5
+ - sprig_id: 1
6
+ title: 'Such Title'
7
+ content: 'New content'
@@ -0,0 +1,7 @@
1
+ options:
2
+ find_existing_by: 'unicorn'
3
+
4
+ records:
5
+ - sprig_id: 1
6
+ title: 'Existing title'
7
+ content: 'Updated content'
@@ -0,0 +1,8 @@
1
+ options:
2
+ find_existing_by: ['title', 'content']
3
+
4
+ records:
5
+ - sprig_id: 1
6
+ title: 'Existing title'
7
+ content: 'Existing content'
8
+ published: true
@@ -0,0 +1,7 @@
1
+ options:
2
+ find_existing_by: 'title'
3
+
4
+ records:
5
+ - sprig_id: 1
6
+ title: 'Existing title'
7
+ content: 'Updated content'
@@ -0,0 +1,5 @@
1
+ records:
2
+ - sprig_id: 1
3
+ title: 'Yaml title'
4
+ content: 'Yaml content'
5
+ comment_id: "<%= sprig_record(Comment, 42).id %>" # This sprig record does not exist.
@@ -0,0 +1,5 @@
1
+ records:
2
+ - sprig_id: 1
3
+ title: 'Yaml title'
4
+ content: 'Yaml content'
5
+ user_id: "<%= sprig_record(User, 1).id %>" # This sprig record did not save.
@@ -0,0 +1,4 @@
1
+ records:
2
+ - sprig_id: 1
3
+ title: '<%= "Partially" %> <%= "Dynamic" %> Title'
4
+ content: 'Yaml content'
@@ -0,0 +1,4 @@
1
+ records:
2
+ - sprig_id: 1
3
+ title: "Title"
4
+ content: "<%= sprig_record(Comment, 1) %>"
@@ -0,0 +1,5 @@
1
+ records:
2
+ - sprig_id: 1
3
+ title: 'Staging yaml title'
4
+ content: 'Staging yaml content'
5
+ photo: '<%= sprig_file("cat.png") %>'
@@ -0,0 +1,7 @@
1
+ records:
2
+ - sprig_id: 1
3
+ title: 'Yaml title'
4
+ content: 'Yaml content'
5
+ tag_ids:
6
+ - '<%= sprig_record(Tag, 1).id %>'
7
+ - '<%= sprig_record(Tag, 2).id %>'
@@ -0,0 +1,5 @@
1
+ records:
2
+ - sprig_id: 1
3
+ name: 'Botany'
4
+ - sprig_id: 2
5
+ name: 'Biology'
@@ -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.should == '~/db/seeds/development'
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.should == '~/seed_files/development'
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.stub(:new).with($stdout).and_return(logger)
24
+ allow(Logger).to receive(:new).with($stdout).and_return(logger)
25
25
 
26
- subject.logger.should == 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.should == logger
33
+ expect(subject.logger).to eq(logger)
34
34
  end
35
35
  end
36
36
  end