sprig-reap 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -7
- data/lib/sprig/reap/configuration.rb +28 -10
- data/lib/sprig/reap/model.rb +1 -1
- data/lib/sprig/reap/seed_file.rb +1 -1
- data/lib/sprig/reap/version.rb +1 -1
- data/lib/sprig/reap.rb +7 -3
- data/spec/db/activerecord.db +0 -0
- data/spec/lib/sprig/reap/configuration_spec.rb +36 -10
- data/spec/lib/sprig/reap/model_spec.rb +17 -1
- data/spec/lib/sprig/reap/seed_file_spec.rb +1 -1
- data/spec/lib/sprig/reap_spec.rb +21 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aefd592ca49ff880defe79c87d202f9309d19155
|
4
|
+
data.tar.gz: 87c0a8f35ae8fc00915194443bc6e63d6d2b41f0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a8f4110f827135c3674aff16b1b621db501bbd945c20619ec109ce2ccd3c3692462731e027961a578bd14e8309ae2db0059b189bc154f719bbb7cfe8b31022f3
|
7
|
+
data.tar.gz: 5dfb7babbff4b81964232a81110ad19a0f52b231ad1b69d3914f469f9d82e33a23f70920b6a4b65b94755389559eb953fd54150e97b0f94c26fe92f9dd0074d2
|
data/README.md
CHANGED
@@ -1,10 +1,21 @@
|
|
1
1
|
# Sprig::Reap
|
2
2
|
|
3
|
-
|
3
|
+
[![Code Climate](https://codeclimate.com/github/vigetlabs/sprig-reap.png)](https://codeclimate.com/github/vigetlabs/sprig-reap) [![Build Status](https://travis-ci.org/vigetlabs/sprig-reap.png?branch=master)](https://travis-ci.org/vigetlabs/sprig-reap) [![Gem Version](https://badge.fury.io/rb/sprig-reap.png)](http://badge.fury.io/rb/sprig-reap)
|
4
4
|
|
5
|
-
|
5
|
+
Don't want to write Sprig seed files from scratch? No problem! Sprig::Reap can create them for
|
6
|
+
you. Sprig::Reap enables automatic capture and output of your application's data state to
|
7
|
+
Sprig-formatted seed files.
|
6
8
|
|
7
|
-
|
9
|
+
## Installation
|
10
|
+
```
|
11
|
+
# Command Line
|
12
|
+
gem install sprig-reap
|
13
|
+
|
14
|
+
# Gemfile
|
15
|
+
gem 'sprig-reap'
|
16
|
+
```
|
17
|
+
|
18
|
+
## Usage
|
8
19
|
|
9
20
|
Via a rake task:
|
10
21
|
```
|
@@ -24,15 +35,17 @@ after the STI base model. STI sub-type records will all be written to that file
|
|
24
35
|
|
25
36
|
### Additional Configuration
|
26
37
|
|
27
|
-
Don't like the defaults when reaping Sprig::Reap records? You may specify the environment
|
28
|
-
target folder)
|
38
|
+
Don't like the defaults when reaping Sprig::Reap records? You may specify the target environment
|
39
|
+
(`db/seeds` target folder), models (`ActiveRecord::Base.subclasses`-only) you want seed files for,
|
40
|
+
or any ignored attributes you don't want to show up in any of the seed files.
|
29
41
|
|
30
42
|
```
|
31
43
|
# Rake Task
|
32
|
-
rake db:seed:reap
|
44
|
+
rake db:seed:reap TARGET_ENV=integration MODELS=User,Post IGNORED_ATTRS=created_at,updated_at
|
33
45
|
|
34
46
|
# Rails Console
|
35
|
-
Sprig::Reap.reap(
|
47
|
+
Sprig::Reap.reap(target_env: 'integration', models: [User, Post], ignored_attrs: [:created_at,
|
48
|
+
:updated_at])
|
36
49
|
```
|
37
50
|
|
38
51
|
### Adding to Existing Seed Files (`.yaml` only)
|
@@ -2,13 +2,13 @@ module Sprig::Reap
|
|
2
2
|
class Configuration
|
3
3
|
VALID_CLASSES = ActiveRecord::Base.subclasses
|
4
4
|
|
5
|
-
def
|
6
|
-
@
|
5
|
+
def target_env
|
6
|
+
@target_env ||= Rails.env
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
10
|
-
parse_valid_env_from given_env do |
|
11
|
-
@
|
9
|
+
def target_env=(given_env)
|
10
|
+
parse_valid_env_from given_env do |target_environment|
|
11
|
+
@target_env = target_environment
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
@@ -22,17 +22,25 @@ module Sprig::Reap
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
+
def ignored_attrs
|
26
|
+
@ignored_attrs ||= []
|
27
|
+
end
|
28
|
+
|
29
|
+
def ignored_attrs=(input)
|
30
|
+
@ignored_attrs = parse_ignored_attrs_from(input)
|
31
|
+
end
|
32
|
+
|
25
33
|
private
|
26
34
|
|
27
35
|
def parse_valid_env_from(input)
|
28
36
|
return if input.nil?
|
29
|
-
|
30
|
-
create_seeds_folder(
|
31
|
-
yield
|
37
|
+
target_environment = input.strip.downcase
|
38
|
+
create_seeds_folder(target_environment)
|
39
|
+
yield target_environment
|
32
40
|
end
|
33
41
|
|
34
|
-
def create_seeds_folder(
|
35
|
-
folder = Rails.root.join('db', 'seeds',
|
42
|
+
def create_seeds_folder(target_env)
|
43
|
+
folder = Rails.root.join('db', 'seeds', target_env)
|
36
44
|
FileUtils.mkdir_p(folder) unless File.directory? folder
|
37
45
|
end
|
38
46
|
|
@@ -57,5 +65,15 @@ module Sprig::Reap
|
|
57
65
|
end
|
58
66
|
end
|
59
67
|
end
|
68
|
+
|
69
|
+
def parse_ignored_attrs_from(input)
|
70
|
+
return if input.nil?
|
71
|
+
|
72
|
+
if input.is_a? String
|
73
|
+
input.split(',').map(&:strip)
|
74
|
+
else
|
75
|
+
input.map(&:to_s).map(&:strip)
|
76
|
+
end
|
77
|
+
end
|
60
78
|
end
|
61
79
|
end
|
data/lib/sprig/reap/model.rb
CHANGED
data/lib/sprig/reap/seed_file.rb
CHANGED
@@ -11,7 +11,7 @@ module Sprig::Reap
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def path
|
14
|
-
Rails.root.join('db', 'seeds', Sprig::Reap.
|
14
|
+
Rails.root.join('db', 'seeds', Sprig::Reap.target_env, "#{model.to_s.tableize.gsub('/', '_')}.yml")
|
15
15
|
end
|
16
16
|
|
17
17
|
def exists?
|
data/lib/sprig/reap/version.rb
CHANGED
data/lib/sprig/reap.rb
CHANGED
@@ -11,8 +11,9 @@ module Sprig::Reap
|
|
11
11
|
class << self
|
12
12
|
def reap(options = {})
|
13
13
|
configure do |config|
|
14
|
-
config.
|
15
|
-
config.classes
|
14
|
+
config.target_env = options[:target_env] || options['TARGET_ENV']
|
15
|
+
config.classes = options[:models] || options['MODELS']
|
16
|
+
config.ignored_attrs = options[:ignored_attrs] || options['IGNORED_ATTRS']
|
16
17
|
end
|
17
18
|
|
18
19
|
Model.all.each { |model| SeedFile.new(model).write }
|
@@ -22,7 +23,10 @@ module Sprig::Reap
|
|
22
23
|
|
23
24
|
cattr_reader :configuration
|
24
25
|
|
25
|
-
delegate :
|
26
|
+
delegate :target_env,
|
27
|
+
:classes,
|
28
|
+
:ignored_attrs,
|
29
|
+
to: :configuration
|
26
30
|
|
27
31
|
def configuration
|
28
32
|
@@configuration ||= Configuration.new
|
data/spec/db/activerecord.db
CHANGED
Binary file
|
@@ -7,28 +7,28 @@ describe Sprig::Reap::Configuration do
|
|
7
7
|
stub_rails_root
|
8
8
|
end
|
9
9
|
|
10
|
-
describe "#
|
10
|
+
describe "#target_env" do
|
11
11
|
context "from a fresh configuration" do
|
12
|
-
its(:
|
12
|
+
its(:target_env) { should == Rails.env }
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
describe "#
|
16
|
+
describe "#target_env=" do
|
17
17
|
context "when given nil" do
|
18
|
-
it "does not change the
|
19
|
-
subject.
|
18
|
+
it "does not change the target_env" do
|
19
|
+
subject.target_env = nil
|
20
20
|
|
21
|
-
subject.
|
21
|
+
subject.target_env.should_not == nil
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
25
|
context "given a non-nil value" do
|
26
26
|
let(:input) { ' ShaBOOSH' }
|
27
27
|
|
28
|
-
it "formats the given value and then sets the environment" do
|
29
|
-
subject.
|
28
|
+
it "formats the given value and then sets the target environment" do
|
29
|
+
subject.target_env = input
|
30
30
|
|
31
|
-
subject.
|
31
|
+
subject.target_env.should == 'shaboosh'
|
32
32
|
end
|
33
33
|
|
34
34
|
context "and the corresponding seeds folder does not yet exist" do
|
@@ -37,7 +37,7 @@ describe Sprig::Reap::Configuration do
|
|
37
37
|
end
|
38
38
|
|
39
39
|
it "creates the seeds folder" do
|
40
|
-
subject.
|
40
|
+
subject.target_env = input
|
41
41
|
|
42
42
|
File.directory?('./spec/fixtures/db/seeds/shaboosh').should == true
|
43
43
|
end
|
@@ -96,4 +96,30 @@ describe Sprig::Reap::Configuration do
|
|
96
96
|
end
|
97
97
|
end
|
98
98
|
end
|
99
|
+
|
100
|
+
describe "#ignored_attrs" do
|
101
|
+
context "from a fresh configuration" do
|
102
|
+
its(:ignored_attrs) { should == [] }
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe "#ignored_attrs=" do
|
107
|
+
context "when given nil" do
|
108
|
+
before { subject.classes = nil }
|
109
|
+
|
110
|
+
its(:ignored_attrs) { should == [] }
|
111
|
+
end
|
112
|
+
|
113
|
+
context "when given an array of ignored_attrs" do
|
114
|
+
before { subject.ignored_attrs = [:shaka, ' laka '] }
|
115
|
+
|
116
|
+
its(:ignored_attrs) { should == ['shaka', 'laka'] }
|
117
|
+
end
|
118
|
+
|
119
|
+
context "when given a string" do
|
120
|
+
before { subject.ignored_attrs = ' shaka, laka' }
|
121
|
+
|
122
|
+
its(:ignored_attrs) { should == ['shaka', 'laka'] }
|
123
|
+
end
|
124
|
+
end
|
99
125
|
end
|
@@ -39,9 +39,25 @@ describe Sprig::Reap::Model do
|
|
39
39
|
end
|
40
40
|
|
41
41
|
describe "#attributes" do
|
42
|
+
let(:attrs) { %w(boom shaka laka) }
|
43
|
+
|
42
44
|
subject { described_class.new(User) }
|
43
45
|
|
44
|
-
|
46
|
+
before do
|
47
|
+
User.stub(:column_names).and_return(attrs)
|
48
|
+
end
|
49
|
+
|
50
|
+
context "when there are no ignored attributes" do
|
51
|
+
its(:attributes) { should == attrs }
|
52
|
+
end
|
53
|
+
|
54
|
+
context "when there are ignored attributes" do
|
55
|
+
before do
|
56
|
+
Sprig::Reap.stub(:ignored_attrs).and_return(['laka'])
|
57
|
+
end
|
58
|
+
|
59
|
+
its(:attributes) { should == ['boom', 'shaka'] }
|
60
|
+
end
|
45
61
|
end
|
46
62
|
|
47
63
|
describe "#dependencies" do
|
data/spec/lib/sprig/reap_spec.rb
CHANGED
@@ -21,17 +21,17 @@ describe Sprig::Reap do
|
|
21
21
|
end
|
22
22
|
|
23
23
|
context "when passed an environment in the options hash" do
|
24
|
-
context "in :
|
24
|
+
context "in :target_env" do
|
25
25
|
it "sets the environment" do
|
26
|
-
subject.reap(:
|
27
|
-
subject.
|
26
|
+
subject.reap(:target_env => 'dreamland')
|
27
|
+
subject.target_env.should == 'dreamland'
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
-
context "in '
|
31
|
+
context "in 'TARGET_ENV'" do
|
32
32
|
it "sets the environment" do
|
33
33
|
subject.reap('ENV' => ' Dreamland')
|
34
|
-
subject.
|
34
|
+
subject.target_env.should == 'dreamland'
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|
@@ -51,5 +51,21 @@ describe Sprig::Reap do
|
|
51
51
|
end
|
52
52
|
end
|
53
53
|
end
|
54
|
+
|
55
|
+
context "when passed a list of ignored attributes in the options hash" do
|
56
|
+
context "in :ignored_attrs" do
|
57
|
+
it "sets ignored attributes" do
|
58
|
+
subject.reap(:ignored_attrs => [:willy, :nilly])
|
59
|
+
subject.ignored_attrs.should == ['willy', 'nilly']
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context "in IGNORED_ATTRS" do
|
64
|
+
it "sets ignored attributes" do
|
65
|
+
subject.reap('IGNORED_ATTRS' => ' willy, nilly')
|
66
|
+
subject.ignored_attrs.should == ['willy', 'nilly']
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
54
70
|
end
|
55
71
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sprig-reap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Stenberg
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|